Let $p$ be an odd prime number. Let $S$ be the sum of all primitive roots modulo $p$. Show that if $p-1$ isn't squarefree (i. e., if there exist integers $k$ and $m$ with $k>1$ and $p-1=k^2m$), then $S \equiv 0 \mod p$.

If not, then what is $S$ congruent to $\mod p$ ?
To show that if \( p-1 \) isn't squarefree, then \( S \equiv 0 \mod p \), we proceed as follows:

1. **Identify a prime divisor \( q \) such that \( q^2 \mid p-1 \):**
   Let \( q \) be a prime such that \( q^2 \mid p-1 \). This means \( p-1 = k^2 m \) for some integers \( k > 1 \) and \( m \), and \( q \mid k \).

2. **Consider a residue \( x \) modulo \( p \) with order \( q \):**
   Let \( x \) be a residue modulo \( p \) such that \( x^q \equiv 1 \mod p \) and \( q \) is the smallest integer with this property. This means \( x \) has order \( q \).

3. **Assume \( a \) is a primitive root modulo \( p \):**
   Let \( a \) be a primitive root modulo \( p \). This means the order of \( a \) is \( p-1 \).

4. **Show that \( xa \) is also a primitive root:**
   Assume \( xa \) is not a primitive root. Then there exists \( t \) such that \( t \mid p-1 \) and \( (xa)^t \equiv 1 \mod p \). This implies:
   \[
   (xa)^t \equiv 1 \mod p \implies x^t a^t \equiv 1 \mod p
   \]
   Since \( x \) has order \( q \), \( x^q \equiv 1 \mod p \). Therefore, \( t \) must be a multiple of \( q \), say \( t = qk \). Then:
   \[
   (xa)^{qk} \equiv 1 \mod p \implies a^{qk} \equiv 1 \mod p
   \]
   Since \( a \) is a primitive root, the smallest \( t \) such that \( a^t \equiv 1 \mod p \) is \( p-1 \). Thus, \( qk \) must be a multiple of \( p-1 \). Given \( q^2 \mid p-1 \), we have \( qk = p-1 \), which implies \( t = \frac{p-1}{q} \).

5. **Partition the set of primitive roots:**
   Since \( t = \frac{p-1}{q} \) is divisible by \( q \), we can write:
   \[
   (x^{-1})^t \equiv 1 \mod p
   \]
   This implies \( a^t \equiv 1 \mod p \), which contradicts the fact that \( a \) is a primitive root. Therefore, \( xa \) must also be a primitive root.

6. **Sum of primitive roots:**
   The set of primitive roots can be partitioned into sets of \( q \) elements each, where each set is of the form \( \{a, xa, x^2a, \ldots, x^{q-1}a\} \). The sum of each such set modulo \( p \) is:
   \[
   a(1 + x + x^2 + \cdots + x^{q-1}) \equiv 0 \mod p
   \]
   because \( x \) is a \( q \)-th root of unity modulo \( p \), and the sum of all \( q \)-th roots of unity is zero.

7. **Conclusion:**
   Since the sum of each partitioned set is zero modulo \( p \), the sum \( S \) of all primitive roots modulo \( p \) is also zero modulo \( p \).

The final answer is \( \boxed{ S \equiv 0 \mod p } \).