I just solved a question from a mathematical competition, which asked to find all positive integers $n$ such that $$\left(\frac{n(n+1)}{2}\right)^2 \equiv 17 \pmod {(n+5)}.$$ We know $n\equiv -5 \pmod {(n+5)}$, so this simplifies to $$332 \equiv 0 \pmod {(n+5)}.$$
The values of $n$ that solve this that are larger than $0$ are $78, 161,327$ however only $78$ and $161$ work in the original equation. My question is, if all the steps are logically valid, why would not all the solutions work? Why does the last value of $n$ not work in the original equation?