Prove the following:
$$ (n− 1)! \equiv \begin{cases} -1 \pmod n & \text{if $n$ is prime} \\ \\ 2 \pmod n & \text{if $n=4$} \\ \\ 0 \pmod n & \text{for all other cases} \end{cases} $$
Let's consider each case in turn.
Case $n$ prime
If $n$ is prime, then by Wilson's Theorem $(n-1)! \equiv -1 \pmod n$.
Case $n=4$
If $n=4$, then $(n-1)! \equiv 6 \equiv 2 \pmod 4$. This is equivalent to $(n-1)! \equiv 2 \pmod n$ if $n=4$.
Case Other $n$
The remaining case is $n$ composite and $n>4$. Since $n$ is composite, it has two non-trivial factors less than or equal to $(n-1)$. Let's call these $d_1$ and $d_2$. By definition, $n = d_1 \times d_2$.
There are two cases to consider here: $d_1 \ne d_2$, and $d_1 = d_2$.
Case $d_1 \ne d_2$.
Since both $d_1$ and $d_2$ are both less than or equal to $(n-1)$, they both divide $(n-1)!$. This is because they each appear once in the sequence of multiplicands of $(n-1)!$. And so $n=d_1 \times d_2$ divides $(n-1)!$, which gives us the desired $(n-1)! \equiv 0 \pmod n$.
Case $d_1 = d_2$.
In this case $n=d_1^2 = d_2^2$. Clearly $d_1$ is a factor of $(n-1)!$ since it appears once in the sequence of multiplicands of $(n-1)!$. We need to show that $d_1$ also appears as a factor of another multiplicand. Let's consider $2d_1$. Does this appear in the sequence of multiplicands? It does if $2 d_1 \le (n-1)$, that is $2 d_1 < n$.
We know $2 d_1 < n = d_1 \times d_1$ if $2 < d_1$.This is true for $n >4$, which is the case here. And so $2 d_1$ appears in the sequence of multiplicands.
We have shown $d_1$ appears as a factor at least twice in $(n-1)!$. That is $n = d_1^2$ divides $(n-1)!$, which gives us the desired $(n-1)! \equiv 0 \pmod n$.
Note: The author's solution doesn't consider the case that $n$ is a perfect square, $n=d_1^2$.