Pairwise Product

Problem Author:Pavlo Tsytsi
Problem Prepared by:Pavlo Tsytsi
Editorial by:Pavlo Tsytsi
$$$(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.