Question on application of Wilson's theorem. Determine all positive integers n having the property that there exists a permutation $a_1, a_2, … , a_n$ of $0,1,2,…,n-1$ such that when divided
by n, the remainders of $a_1, a_1a_2, … ,a_1a_2⋯a_n$ are distinct.
I found this as a solved question in the magazine Mathematical Excalibur. I tried to read the solution but could understand only half of it. Any help to understand this problem would be appreciated. Thanks in advance.
 A: The answer is this works if n is prime or if n=4 or n=1. The n=1 case is trivial.
Firstly, we need to have $a_n = 0$.
Otherwise $a_1,a_1a_2,...,a_1a_2...a_n$ will be 0 for multiple values and hence the remainder will be 0 for multiple values.
Wilson's theorem says
$(n-1)! \equiv -1 \mod n$,  if and only if n is prime.
As an extension to Wilson's theorem if n is not prime then $(n-1)! \equiv 0 \mod n$ except when n=4.
For the case of $n=4$, we get $3! \equiv 2 \mod 4$
When $n=4$
we can have our sequence as: $1, 1 * 3, 1 * 3 * 2, 1 * 3 * 2 * 0$ which is $1,3,6,0$
We check the remainders when dividing by 4, we get 1,3,2,0. All distinct.
But for any other composite number we're going to have a repeat of 0 as a remainder.
$a_1a_2..a_{n-1} = (n-1)!$ which when divided by n gives 0. And the same with $a_1a_2..a_{n}$ since $a_n=0$
Only thing left to show is that this works when n is prime.
$a_1a_2..a_{p-1} = (p-1)! \equiv -1 \mod p$
The elements 1,2,... p-1 form a field mod p. This means we can do multiplication and division with them and still get an element in the set. Also every element has a multiplicative inverse.
We choose $a_1a_2..a_{p-2} \equiv -2 \mod p$
This means that:
$(-2)a_{p-1} \equiv -1 \mod p$
$a_{p-1} \equiv \frac{1}{2} \mod p$
This means that $a_{p-1}$ is the multiplicative inverse of 2 mod p. We know this solution exists since we're in a field.
By the same reasoning:
We choose $a_1a_2..a_{p-3} \equiv -3 \mod p$
We choose $a_1a_2..a_{p-4} \equiv -4 \mod p$
etc.
until we get to
$a_1 \equiv -(p-1) \mod p \equiv 1 \mod p$
This way all the products have different remainders as required by the problem.
We solve for the other terms in the sequence just like we did for $a_{p-1}$
$a_{p-2} \equiv \frac{2}{3} \mod p$
$a_{p-3} \equiv \frac{3}{4} \mod p$
And we can keep going till we get to $a_2$
$a_2 \equiv \frac{p-2}{p-1} \mod p$
and we already have
$a_1 \equiv 1 \mod p$
We still need to show that all these fractions (1/2,2/3 etc...) are distinct mod p:
Suppose
$\frac{a}{a+1} \equiv \frac{b}{b+1} \mod p$
Then cross-multipling
$$ab+a \equiv ab+b \mod p$$
$$ a \equiv b \mod p$$
So they are distinct.
