Let S be the set of all sequences (b1,b2,…,bp) of numbers from the set {0,1,2,…,p−1} such that b1+b2+⋯+bp is not divisible by p. We show that ∣S∣=pp−pp−1. For let b1,b2,…,bp−1 be an arbitrary sequence of numbers chosen from {0,1,2,…,p−1}. There are exactly p−1 choices for bp such that b1+b2+⋯+bp−1+bp≡0(modp), and therefore ∣S∣=pp−1(p−1)=pp−pp−1.
Now it will be shown that the number of good sequences in S is p1∣S∣. For a sequence B=(b1,b2,…,bp) in S, define the sequence Bk=(a1,a2,…,ap) by
ai=bi−b1+kmodp
for 1≤i≤p. Now note that B in S implies that
a1+a2+⋯+ap≡(b1+b2+⋯+bp)−pb1+pk≡(b1+b2+⋯+bp)≡0(modp)
and therefore Bk is in S for all non-negative k. Now note that Bk has first element k for all 0≤k≤p−1 and therefore the sequences B0,B1,…,Bp−1 are distinct.
Now define the cycle of B as the set {B0,B1,…,Bp−1}. Note that B is in its own cycle since B=Bk where k=b1. Now note that since every sequence in S is in exactly one cycle, S is the disjoint union of cycles.
Now it will be shown that exactly one sequence per cycle is good. Consider an arbitrary cycle B0,B1,…,Bp−1, and let B0=(b1,b2,…,bp) where b0=0, and note that Bk=(b1+k,b2+k,…,bp+k) mod p. Let u=b1+b2+⋯+bp, and v=b1b2+b2b3+⋯+bpb1 and note that (b1+k)(b2+k)+(b2+k)(b3+k)+⋯+(bp+k)(b1+k)=u+2kv(modp) for all 0≤k≤p−1. Since 2v is not divisible by p, there is exactly one value of k with 0≤k≤p−1 such that p divides u+2kv and it is exactly for this value of k that Bk is good. This shows that exactly one sequence per cycle is good and therefore that the number of good sequences in S is p1∣S∣, which is pp−1−pp−2.