<s>[INST] 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.
 [/INST] It never hurts to gather some numerical data. For your first problem:
$$\begin{array}{cc|cc|cc}
n&\left(\sum_{k=1}^{n-1}k^2\right)\bmod n&n&\left(\sum_{k=1}^{n-1}k^2\right)\bmod n&n&\left(\sum_{k=1}^{n-1}k^2\right)\bmod n\\ \hline
2&1&8&4&14&7\\
3&2&9&6&15&10\\
4&2&10&5&16&8\\
5&0&11&0&17&0\\
6&1&12&2&18&3\\
7&0&13&0&19&0
\end{array}$$
Examination of that table should suggest that
$$\left(\sum_{k=1}^{n-1}k^2\right)\bmod n=\begin{cases}
\frac{n}2,&\text{if }n\equiv 2\!\!\!\pmod 6\\\\
\frac{2n}3,&\text{if }n\equiv 3\!\!\!\pmod 6\\\\
\frac{n}2,&\text{if }n\equiv 4\!\!\!\pmod 6\\\\
0,&\text{if }n\equiv 5\!\!\!\pmod 6\\\\
\frac{n}6,&\text{if }n\equiv 0\!\!\!\pmod 6\\\\
0,&\text{if }n\equiv 1\!\!\!\pmod 6\;;
\end{cases}\tag{1}$$
since you know that $$\sum_{k=1}^{n-1}k^2=n\left(\frac{n^2}3-\frac{n}2+\frac16\right)=\frac16n(n-1)(2n-1)\;,$$ it shouldn’t be too hard to prove $(1)$.
Added: For the second problem, did you try calculating 
$$\left(\sum_{k=1}^{n-1}k^3\right)\bmod n=\frac{n^2(n-1)^2}4\bmod n$$
for $n=2,3,4,\dots$ as I did for the first problem? Doing so should let you guess right away that the value is $0$ for odd $n$, and discovering the mathematical reason for this isn’t hard. If $n$ is odd, then $\frac{(n-1)^2}4$ is an integer, and clearly $n^2\frac{(n-1)^2}4\bmod n=0$.
If $n$ is even, then 
$$\frac{n^2(n-1)^2}4\equiv\left(\frac{n}2\right)^2(n-1)^2\equiv\left(\frac{n}2\right)(-1)^2\equiv\left(\frac{n}2\right)^2\pmod n\;.$$
Now make a table for $n=2,4,6,\dots$:
$$\begin{array}{rccc}
n:&2&4&6&8&10&12&14&16\\ \hline
\left(\frac{n}2\right)^2\bmod n:&1&0&3&0&5&0&7&0
\end{array}$$
The pattern is pretty obvious: it definitely appears that
$$\left(\frac{n}2\right)^2\bmod n=\begin{cases}
0,&\text{if }n\equiv 0\!\!\!\pmod 4\\\\
n/2,&\text{if }n\equiv 2\!\!\!\pmod 4\;,
\end{cases}$$
and you shouldn’t have too much trouble proving that this is actually the case.
</s>