Solution (source code)

= Solution

<Fermat's little theorem> states that for a <prime> $p$ and an integer $a$,
$$
a^p\equiv a\pmod p;
\qquad p\nmid a\ \Longrightarrow\ a^{p-1}\equiv1\pmod p.
$$
<Wilson theorem> states that for a <prime> $p$,
$$
\boxed{(p-1)!\equiv-1\pmod p.}
$$
All the following <modular congruences> are taken modulo the indicated <prime>.