Solution (source code)

= Solution

For a <primitive Dirichlet character> the previous formula gives $|\widehat\chi(a)|=|\chi(a)|\,|\widehat\chi(1)|$. The <Plancherel theorem> for the unitary finite transform yields
$$
\varphi(q)=\sum_{n\bmod q}|\chi(n)|^2
=\sum_{a\bmod q}|\widehat\chi(a)|^2
=\varphi(q)|\widehat\chi(1)|^2.
$$
Thus $|\widehat\chi(1)|=1$, the normalized <Gauss sum of a Dirichlet character> magnitude.

For an imprimitive character modulo $p^k$ with $k\ge2$, its values on residues are periodic modulo $p^{k-1}$: descent preserves the values on units, and divisibility by $p$ is unchanged by that shift. Splitting the sum into these <residue classes> gives a factor $\sum_{j=0}^{p-1}e(-j/p)=0$. Hence $\widehat\chi(1)=0$. If $k=1$, the only imprimitive character is principal, and its Gauss sum is $\sum_{n=1}^{p-1}e(-n/p)=-1$, giving $|\widehat\chi(1)|=p^{-1/2}$. All cases satisfy the required bound.