Wilson's Theorem

Statement
Positive integer $$p$$ is a prime if and only if $$(p-1)!\equiv -1\pmod{p}$$.

If part
First consider $$p$$ is a prime. Since modular inverse modulo $$p$$ is bijection in which only 1 and $$p-1$$ are mapped to themselves, numbers $$2,...,p-2$$ can be divided into pairs $$(a,b)$$, such that $$ab\equiv 1$$. Product of all these numbers is therefore 1. The expression $$(p-1)!$$ is product of 1,$$p-1$$ and the numbers listed above, because of that it is congruent to

$$1\cdot (p-1) \cdot 1\equiv -1\pmod{p}$$.

Only if part
Now consider $$p$$ is composed and $$q$$ is a prime such that $$q\mid p$$. Then both $$p$$ and $$(p-1)!$$ are divisible by $$q$$. If the congruence

$$(p-1)!\equiv -1\pmod{p}$$

was satisfied, we would have

$$(p-1)!\equiv -1\pmod{q}$$, which means $$0\equiv -1\pmod{q}$$

and that is imposible. Hence for $$p$$ composed the congruence $$(p-1)!\equiv -1\pmod{p}$$ cannot hold.