# Closed form for $(p-n)!\pmod{p}$ where $p$ is prime

Does $(p-n)!\pmod{p}$ have a closed form for any $n>2$ when $p$ is prime?

• $(p-0)!=0 \pmod{p}$

• $(p-1)!=-1\pmod{p}$

• $(p-2)!=1\pmod{p}$

-
from $(p-1)!$ to $\Big(p-(p-1)\Big)$, we just traverse the reduced residue system modulo $p$. Kind of hard to say which permutation of it is encountered. –  Nikhil Bellarykar Jan 17 '12 at 15:57
You'd need to find a formula for the multiplication inverse of $n!\pmod p$. Then $(p-n)! \equiv (-1)^{n} (n-1)!^{-1}\pmod p$ or something like that. –  Thomas Andrews Jan 17 '12 at 20:03
–  lhf Oct 3 '13 at 1:14

Since $(p-1)! \equiv -1 \mod p$, $$(p-n)! \equiv \frac{(p-1)!}{\prod_{j=1}^{n-1} (p-j)} \equiv (-1)^{n-1} ((n-1)!)^{-1} \mod p$$

-

The only interesting case is $n=1$, which is Wilson's theorem.

The case $n=2$ follows from $n=1$ directly.

For the case $n=3$ one needs to know the multiplicative inverse of $p-2 \bmod p$, which is $(p-1)/2$. So, $(p-3)! \equiv (p-1)/2 \bmod p$.

For the case $n=4$ one needs to know the multiplicative inverse of $p-3 \bmod p$. Now $p=3t\pm1$, assuming $p\ne3$. Then $(p-4)! \equiv (p\mp 1)(p-1)/6\bmod p$.

I guess you could carry on but it seems to get messier...

-
You can write the inverse of $p-3$ as $\frac{p^2-1}3$ if you want to eliminate the $\pm 1$. –  Thomas Andrews Jan 17 '12 at 17:29
@ThomasAndrews, good point. –  lhf Jan 17 '12 at 19:57