Prove that $(n-1)! \equiv -1 \pmod{n}$ iff $n$ is prime [Wilson's Theorem]

$$n\text{ is prime if }(n-1)! \equiv -1 \pmod n$$

This direction is easy. If $n$ is composite, then there exists $k|n$ and $k\lt n$. So $k|(n-1)!$ and $k \equiv 1 \pmod n$. This means $k$ needs to divide $1$. So $n$ must be prime (or $1$, but we can eliminate this by substitution).

$$(n-1)! \equiv -1\text{ if }n\text{ is prime}$$

Wikipedia contains two proofs of this result known as Wilson's theorem. The first proof only uses basic abstract algebra and so should be understandable with a good knowledge of modular arithmetic. Just in case, I prove below that each element $1, 2, ... n-1$ has a unique inverse $\mod n$.

They use the fact that integers $\mod p$ form a group and hence that each element $x$ not congruent $0$ has a multiplicative inverse (a number $y$ such that $xy \equiv 1 \mod n$. We show this as follows. Suppose $n \nmid x$, for $n$ prime. From the uniqueness of prime factorisations, $xn$ is the first product of $x$, after $0x$, divisible by $n$ (use prime factorisation theorem). If we look at the series $kn \mod n$, this cycles and must have cycle length $n$. Therefore, each element $x, 2x,... nx$ must be different modulo $n$, including one, $y$, with $xy \equiv 1 \mod n$. Furthermore, due to the cycle length being $n$, each only one of those elements will be an inverse. So every element has a unique inverse (although 1 and -1 are their own inverses).


Hint $\ (p\!-\!1)!\bmod p\,$ is the product of all elts of $\, {\mathbb F}_p^*.\,$ The map $ n \mapsto n^{-1}$ is a permutation of $\:{\mathbb F}_p^*\:$ of order $2$ so it decomposes into cycles of length $1$ or $2,$ which partition the product. Each $2$-cycle $ (n, n^{-1})$ has product $1$ so is deletable, leaving only the product of $1$-cycles $ (n)$. They satisfy $\: n^{-1}\! = n \Rightarrow n^2 = 1 \Rightarrow n = \color{#0a0}{-1}\,$ or $\color{#c00}1,\,$ by ${\mathbb F}_p$ a field. So the product reduces to $\,\color{#0a0}{-1}\cdot\color{#c00}1 = -1$.

Remark $ $ See here for a generalization to invertibles in $\Bbb Z_n\! = \Bbb Z\bmod n.\,$ Wilson generalizes further: if a finite abelian group has a unique element of order $2$ then it equals the product of all the elements; otherwise the product is $1$, e.g. see here for hints (this is the group-theoretic Wilson Theorem).

Notice how we've exploited the existence of a symmetry - here an involution that induces a natural pairing of elts. Frequently involution and reflection symmetries lie at the heart of elegant proofs, e.g. see the elegant proof by Liouville, Heath-Brown and Zagier which shows every prime $\equiv 1 \pmod{\! 4}\,$ is a sum of $2$ squares, or the little-known beautiful reflective generation of the ternary tree of primitive Pythagorean triples due to Aubry.


Here are a couple possible proofs of Wilson's theorem for $p>2$ ($p=2$ is easily checked):

  1. We have that $x^{p-1}-1$ has roots $1,2,\ldots,p-1$ over $\mathbb{Z}/p\mathbb{Z}$ (by Fermat's Little Theorem). But as $\mathbb{Z}/p\mathbb{Z}$ is a field, we have unique factorization of polynomials, so that $x^{p-1}-1=(x-1)(x-2)\ldots(x-(p-1))$. Comparing constant terms wields Wilson's theorem.

  2. Let $g$ be a primitive root modulo $p$. Then $(p-1)!\equiv g\times g^2\times \ldots \times g^{p-1}=g^{p\frac{p-1}{2}}\equiv g^{\frac{p-1}{2}}\bmod{p}$ by Fermat's Little Theorem, and $g^{\frac{p-1}{2}}\equiv -1 \bmod{p}$ because if $(g^{\frac{p-1}{2}})^2=g^{p-1}\equiv 1 \bmod{p}$ and $g^{\frac{p-1}{2}}\not \equiv 1 \bmod{p}$ by the definition of primitive root.