Let Epi(n,i) be the number of surjective functions from a set with n elements to a set with i elements. It is not hard to see that S(n,i)=i!Epi(n,i). Indeed, a surjective function from {1,…,n} to {1,…,i} is the same thing as a partition of {1,…,n} into i parts, together with a numbering of the blocks of the partition, from 1 to i. For every partition there exists i! numberings, and therefore S(n,i)⋅i!=Epi(n,i).
When 1≤i≤p−1, gcd(p,i!)=1. It follows that for 1≤i≤p−1, S(n,i)≡S(m,i)(modp) if and only if Epi(n,i)≡Epi(m,i)(modp). So it is enough to prove that if p−1∣m−n then Epi(n,i)≡Epi(m,i)(modp) for all 1≤i≤p−1.
There is a formula for Epi(n,i):
Epi(n,i)=j=0∑i−1(−1)j(i−j)n(ji).
This formula is based on nothing more than the inclusion-exclusion principle. The number of surjective functions is the number of all functions −i times the number of functions that miss one particular element +(2i) times the number of functions that miss two particular elements, etc. The number of functions that miss j specified elements is (i−j)n.
Now suppose m, n are positive integers satisfying p−1∣m−n. Then am≡an(modp) for all integers a. When a is not divisible by p this follows from Fermat's little theorem. When a is divisible by p, both sides are zero mod p. But we only need the cases when a=1,…,p−1 anyway.
It follows that if p−1∣m−n then Epi(n,i)≡Epi(m,i)(modp) for all 1≤i≤p−1, and we are done.
Lemma:
For every i>1 and n>i:
S(n,i)=i⋅S(n−1,i)+S(n−1,i−1)
Proof. Select an element x. There are S(n−1,i) ways to partition the remaining elements into i different sets. By adding x to any of them, we get a partition of the original set. All the partitions obtained this way are different and the only partitions that cannot be obtained this way are the ones where x ends up alone. But in that case the n−1 other elements must form i−1 sets, yielding another S(n−1,i−1) partitions. □
Lemma:
For every p prime, p>i≥1 and λ≥0:
S(i+λ(p−1),i)=1(modp).
Proof. As S(n,1)=1 and S(i,i)=1, this is obvious for i=1 or λ=0. We proceed by induction over i and λ. Using the induction hypothesis and the previous lemma:
S(i+λ(p−1),i)=i⋅S(i−1+λ(p−1),i)+S(i−1+λ(p−1),i−1)=i⋅S(i−1+λ(p−1),i)+1mod p
Otherwise applying this p−1 times gives
S(i+λ(p−1),i)=ip−1⋅S(i+(λ−1)(p−1),i)+j=0∑p−2ijmod p=ip−1⋅1+i−1ip−1−1mod p=1⋅1+0mod p.
Where the last equality holds by Fermat's little theorem as p>i>1 implies i=0,1(modp). □
We can now proceed to proving the problem statement by induction: If i=1 the statement is obvious. If m=i or n=i, we have the statement of the second lemma. So assume i>1 and n>i, m>i and that the problem statement holds for all smaller values. Then
S(n,i)−S(m,i)=i⋅(S(n−1,i)−S(m−1,i))++S(n−1,i−1)−S(m−1,i−1)=i⋅0+0mod p=0mod p.