\begin{center}
\begin{tabular}{rl}
\textbf{Problem Author}: & \textbf{Pavlo Tsytsi} \\
\textbf{Problem Prepared by}: & \textbf{Pavlo Tsytsi} \\
\textbf{Editorial by}: & \textbf{Pavlo Tsytsi} 
\end{tabular} 
\end{center}
$(a_1+...+a_n)^2=a_1^2+...+a_n^2+2\cdot (a_1\cdot a_2+...+a_1\cdot a_n+a_2\cdot a_3+...+a_2\cdot a_n+...+a_{n-1}\cdot a_n)$. The part we need is in the second set of parentheses, so all we need is the square of the sum on the segment and the sum of squares on the segment. This can be found using prefix sum. After that, divide by 2 modulo.