How to solve $ \sum_{i=1}^{n-1} i^2 \equiv \;? \pmod n$ I solved expressions below . it is ok?
My attempt:
$$1)\;\;\;\;\; ?\cong\pmod n\sum_{i=1}^{n-1} i^2 \stackrel{?}=   {(n-1)^3\over3}+{(n-1)^2\over2}+{(n-1)\over6}={n^3\over3}-{n^2\over6}+{n\over6}=n\left({n^2\over3}-{n\over2}+{1\over6}\right)
 $$
(continue?)how to solve for all n?
$$2)\;\;\;\;\;?\cong\pmod n \sum_{i=1}^{n-1} i^3 = \left( {(n-1)n\over2} \right)^2$$
(continue?)how to solve for all n?
Thank you.

You know that 
First, since $n^2 =0 \pmod n$ and $n^3 =0 \pmod n$, you can calculate the sum up to $n$.
(i)
$$\sum_{i=1}^{n-1} i^2 =\sum_{i=1}^{n} i^2 =\frac{n(n+1)(2n+1)}{6} \pmod n$$
Thus, you have to decide which of the terms is divisible by 2,3. CRT tells you this is a problem modulo 6.
Now a case by case analysis solves it:
Case 1: $n=6k$. Then
$$ \frac{n(n+1)(2n+1)}{6} \equiv k (n+1)(2n+1) \equiv k \equiv \frac{n}{6}  \pmod n$$
Case 2: $n=6k+1$. Then $n+1=2(3k+1)$ and $n+2=3(2k+1)$
$$ \frac{n(n+1)(2n+1)}{6} \equiv n(3k+1)(2k+1) \equiv 0 \pmod n$$
the other cases are similar.
(ii) This is easier
$$\sum_{i=1}^{n-1} i^3 =\sum_{i=1}^{n} i^3 =\frac{n^2(n+1)^2}{4} \pmod n$$
We only have two cases now, odd or even.
Case 1: $n=2k$. Then
$$ \frac{n^2(n+1)^2}{4} \equiv k^2 (n+1)^2 \equiv k^2 \equiv \frac{n^2}{4}  \pmod n$$
Case 2: $n=2k+1$. Then $n+1=2(k+1)$ 
$$ \frac{n^2(n+1)^2}{2} \equiv n^2(k+1)^2 \equiv 0 \pmod n$$
