Question arising from combinatorial proof of Fermat's little theorem The proof in question can be found here: Combinatorial Proof Of A Number Theory Theorem--Confusion
In cases where we attempt the process described in this proof with some non-prime $p$ for the necklace length, the proof fails to work because some cyclic permutations of a necklace of composite length may be identical to each other. For example, with $p=6$, and with the colors $B$ and $W$, we have the necklace: $BWBBWB$, which does not correspond to six distinct permutations, but only to three. Thus, we do not end up showing that $6|(2^6-2)$, and indeed it does not. Great.
In a discussion at a related question (Why need prime number in Fermat's Little Theorem), a question arose. In the non-prime case, is there a nice way to quantify the extent to which the proof breaks down? If $p$ is composite, can we write down a nice formula that counts the number of necklaces that appear fewer than $p$ times, and thus write $a^p-a$ as the sum of a multiple of $p$ and some other terms that depend on $p$ and $a$? Does the resulting error term reveal some number theoretic property of $p$?
 A: In the non-prime case there are two related identities you can write down. First, as Lord Shark says in the comments, you can apply Burnside's lemma to count the total number of necklaces up to cyclic permutation, which for necklaces of length $n$ using $a$ colors is
$$\frac{1}{n} \sum_{d | n} \varphi(n/d) a^d.$$
where $\varphi(-)$ is the totient function. So you learn that $\sum_{d | n} \varphi(n/d) a^d$ is divisible by $n$; this reduces to FLT when $n$ is prime but gives an interesting generalization when $n$ is composite.
Another thing you can do is just count the number of aperiodic necklaces - the ones which are not equal to themselves under any nontrivial cyclic permutation - using Mobius inversion, which gives
$$\frac{1}{n} \sum_{d | n} \mu(n/d) a^d$$
where $\mu(-)$ is the Mobius function. These polynomials $M_n(a)$ in $a$ are called necklace polynomials, and famously when $a = q$ is a prime power they count the number of monic irreducible polynomials of degree $n$ in $\mathbb{F}_q$. In any case, here you learn that $\sum_{d | n} \mu(n/d) a^d$ is divisible by $n$. The necklace polynomials also appear in the cyclotomic identity
$$\frac{1}{1 - at} = \prod_{n \ge 1} \frac{1}{(1 - t^n)^{M_n(a)}}$$
which, when $a = q$ is a prime power, is a function field analogue of the Euler product factorization of the Riemann zeta function; see this blog post for a combinatorial interpretation for arbitrary $a$. 
I don't know if these congruences have a name; I call them the necklace congruences. They turn out to be equivalent, which is not obvious. A long time ago I wrote some blog posts about them which you can find here and here. 
(These congruences admit a neat generalization to matrices, as follows. Instead of letting $a$ be an integer, let $A$ be a square matrix of integers. Then $\sum_{d | n} \varphi(n/d) \text{tr}(A^d)$ and $\sum_{d | n} \mu(n/d) \text{tr}(A^d)$ are divisible by $n$. The combinatorial interpretation involves thinking of $A$ as the adjacency matrix of a graph and counting closed walks on $A$ up to cyclic permutation.)
