AtCoder Beginner Contest 177 - Sum of product of pairs

Given are N integers A_1,\ldots,A_N.
Find the sum of A_i \times A_j over all pairs (i,j) such that 1\leq i < j \leq N, modulo (10^9+7).

-----Constraints-----
 - 2 \leq N \leq 2\times 10^5
 - 0 \leq A_i \leq 10^9
 - All values in input are integers.

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 \ldots A_N

-----Output-----
Print \sum_{i=1}^{N-1}\sum_{j=i+1}^{N} A_i A_j, modulo (10^9+7).

-----Sample Input-----
3
1 2 3

-----Sample Output-----
11

We have 1 \times 2 + 1 \times 3 + 2 \times 3 = 11.

Difficulty: EASY

Starter code:

Solutions:
N = int(input())
nums = list(map(int, input().split()))
print((sum(nums) ** 2 - sum([i ** 2 for i in nums])) // 2 % (10 ** 9 + 7))


input()
mod = 10 ** 9 + 7
A = list(map(int, input().split()))
S = 0
ans = 0
for a in A:
	ans = (ans + S * a) % mod
	S += a
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = [0] * (n + 1)
mod = 10 ** 9 + 7
for i in range(1, n + 1):
	s[i] = s[i - 1] + a[i - 1]
ans = 0
for j in range(1, n):
	ans += a[j - 1] * (s[n] - s[j])
print(ans % mod)


(n, *a) = map(int, open(0).read().split())
s = sum(a)
ans = 0
for i in a:
	s -= i
	ans += i * s
	ans %= 10 ** 9 + 7
print(ans)


CONST = 10 ** 9 + 7
N = int(input())
numbers = list(map(int, input().split()))
r = 0

def cumsum(xs):
	result = []
	for i in range(len(xs)):
		if i == 0:
			result.append(xs[0])
		else:
			result.append(result[-1] + xs[i])
	return result
numbers.reverse()
num_sum = cumsum(numbers)
num_sum.reverse()
numbers.reverse()
del num_sum[0]
for i in range(N - 1):
	r += numbers[i] * num_sum[i] % CONST
print(r % CONST)


n = int(input())
a = list(map(int, input().split()))
ss = sum(a)
s = 0
for i in range(len(a)):
	ss = ss - a[i]
	s += a[i] * ss
print(s % (10 ** 9 + 7))


n = int(input())
a = list(map(int, input().split()))
ans = 0
num = 0
for i in range(n - 1):
	num += a[i]
	ans += a[i + 1] * num
print(ans % 1000000007)


N = int(input())
A = [int(a) for a in input().split()]
mod = 10 ** 9 + 7
L = [0] * (N + 1)
for i in range(N):
	L[i + 1] = (L[i] + A[i]) % mod
ans = 0
for i in range(N):
	ans += A[i] * (L[-1] - L[i + 1])
	ans %= mod
print(ans)


N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
ans = 0
sums = [0] * N
sums[0] = A[0]
for i in range(1, N):
	sums[i] = sums[i - 1] + A[i]
for j in range(N):
	ans += A[j] * (sums[-1] - sums[j])
print(ans % mod)


N = int(input())
A = [int(a) for a in input().split()]
mod = 10 ** 9 + 7
print((sum(A) ** 2 - sum([i ** 2 for i in A])) // 2 % mod)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
ans = 0
for i in a:
	s -= i
	ans += i * s
mod = ans % (10 ** 9 + 7)
print(mod)


mod = 1000000000 + 7
n = int(input())
As = list(map(int, input().split()))
sum_ = sum(As)
ret = 0
for i in range(n - 1):
	sum_ -= As[n - 1 - i]
	ret += sum_ * As[n - 1 - i]
	ret %= mod
print(ret)


n = int(input())
arr = input()
ar = list(map(int, arr.split(' ')))
sum = 0
s = 0
mod = 1000000007
for i in range(0, n):
	sum = sum + ar[i] * ar[i]
	s = s + ar[i]
s = s * s
print((s - sum) // 2 % mod)


n = int(input())
A = list(map(int, input().split()))
paA = [0 for _ in range(n - 1)]
paA[0] = A[n - 1]
mod = 10 ** 9 + 7
for i in range(1, n - 1):
	paA[i] = paA[i - 1] + A[n - 1 - i]
suma = 0
for i in range(n - 1):
	suma += A[i] * paA[n - i - 2]
	suma = suma % mod
print(suma)


MOD = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
sum_of_all = sum(A) ** 2
sum_of_diagonal = 0
for i in range(N):
	sum_of_diagonal += A[i] ** 2
print((sum_of_all - sum_of_diagonal) // 2 % MOD)


n = int(input())
a = list(map(int, input().split()))
s = [0]
for i in range(n):
	s.append(s[i] + a[i])
ans = sum((a[i] * (s[n] - s[i + 1]) for i in range(n)))
print(ans % (10 ** 9 + 7))


import math
prime = [True for i in range(10 ** 6 + 1)]

def SieveOfEratosthenes(n):
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1

def __starting_point():
	N = int(input())
	L = list(map(int, input().split()))
	MOD = 10 ** 9 + 7
	sum_L = sum(L)
	ans = 0
	for i in range(N - 1):
		sum_L = sum_L - L[i]
		ans = (ans + sum_L * L[i]) % MOD
	print(ans)
__starting_point()


_ = input()
MOD = 10 ** 9 + 7
ans = b = 0
for a in map(int, input().split()):
	ans += a * b
	ans %= MOD
	b += a
print(ans)


p = 10 ** 9 + 7
n = int(input())
aa = list(map(int, input().split()))
s = 0
for a in aa:
	s += a ** 2
sa = sum(aa)
b = sa * sa
ans = (b - s) // 2
print(ans % p)


n = int(input())
c = list(map(int, input().split()))
c_ans = []
ans = 0
for i in range(n):
	ans += c[i]
	c_ans.append(ans)
re_ans = 0
for i in range(n):
	re_ans += c[i] * (c_ans[n - 1] - c_ans[i])
re_ans = re_ans % (10 ** 9 + 7)
print(re_ans)


n = input()
a = list(map(int, input().split()))
sum = 0
msum = 0
for i in a:
	sum += i
	msum += i * i
ans = int((sum * sum - msum) // 2)
print(int(ans % 1000000007))


N = int(input())
A = list(map(int, input().split()))
tot = sum(A)
ans = 0
for a in A:
	ans += (tot - a) * a
print(ans // 2 % 1000000007)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
ss = s ** 2
c = 0
for i in range(n):
	c += l[i] * l[i]
p = (ss - c) // 2
print(p % 1000000007)


N = int(input())
A = list(map(int, input().split()))
S = sum(A)
S2 = sum(map(lambda x: x ** 2, A))
print((S ** 2 - S2) // 2 % (1000000000 + 7))


N = int(input())
A = list(map(int, input().split()))
x = 0
x = sum(A) ** 2
for i in A:
	x -= i ** 2
print(x // 2 % 1000000007)


import numpy as np
import itertools as it
n = int(input())
a = list(map(int, input().split()))
_sum = 0
ans = 0
mod = 1000000007
for i in range(n):
	_sum += a[i]
	_sum %= mod
for i in range(n):
	_sum -= a[i]
	if _sum < 0:
		_sum += mod
	ans += a[i] * _sum
	ans %= mod
print(ans)


import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())), dtype=np.int64)
MOD = 10 ** 9 + 7
B = np.cumsum(A[N - 1:0:-1])[::-1]
B %= MOD
ans = 0
for (i, j) in zip(A[:N - 1], B):
	ans += i * j
	ans %= MOD
print(ans)


n = int(input())
a = list(map(int, input('').split()))
o = 0
aa = []
for i in a:
	aa.append(i ** 2)
s = sum(a)
o = (s ** 2 - sum(aa)) // 2
o %= 10 ** 9 + 7
print(o)


n = int(input())
lst = list(map(int, input().split()))
s = (sum(lst) ** 2 - sum([z ** 2 for z in lst])) // 2
print(s % (pow(10, 9) + 7))


n = int(input())
a = list(map(int, input().split()))
x = sum(a)
y = sum([a[i] ** 2 for i in range(n)])
print((x ** 2 - y) // 2 % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
s = sum((a * a for a in A))
ans = sumA * sumA - s
print(ans // 2 % (10 ** 9 + 7))


n = int(input())
a = list(map(int, input().split()))
b = sum(a)
c = 0
for v in range(n - 1):
	b -= a[v]
	c += a[v] * b
print(c % (10 ** 9 + 7))


import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
n = int(input())
A = [int(i) for i in input().split()]
tmp = 0
res = 0
res = sum(A) ** 2
for i in range(n):
	res -= A[i] ** 2
res = res // 2 % MOD
print(res)


mod = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
ans = sum(A) ** 2
for x in A:
	ans -= x ** 2
ans = ans // 2
print(ans % mod)


n = int(input())
a = list(map(int, input().split()))
sum = 0
f = 0
for i in range(n - 1):
	f = f + a[i]
	sum += f * a[i + 1]
print(sum % (10 ** 9 + 7))


n = int(input())
a = list(map(int, input().split()))
ans = 0
x = 0
for i in range(n):
	ans += a[i] * x
	x += a[i]
print(ans % (10 ** 9 + 7))


n = int(input())
li = list(map(int, input().split()))
mod = 10 ** 9 + 7
sm = sum(li)
mul = sum([i ** 2 for i in li])
ans = (sm ** 2 - mul) // 2 % mod
print(ans)


N = int(input())
num = list(map(int, input().split(' ')))
ans = 0
total = sum(num)
MOD = 10 ** 9 + 7
for a in num:
	total -= a
	ans += a * total
	ans %= MOD
ans %= MOD
print(ans)


N = int(input())
A = list(map(int, input().split()))
s = sum(A)
ss = s
z = 0
for i in range(0, N - 1):
	ss -= A[i]
	z += A[i] * ss
print(z % 1000000007)


N = int(input())
(*A,) = map(int, input().split())
ans = (sum(A) ** 2 - sum((a * a for a in A))) // 2
mod = 10 ** 9 + 7
print(ans % mod)


N = int(input())
b = 0
a = list(map(int, input().split()))
c = 0
for i in range(0, N):
	c = c + a[i]
for i in range(0, N):
	c = c - a[i]
	b = (b + a[i] * c) % (10 ** 9 + 7)
print(b)


n = int(input())
a = list(map(int, input().split()))
k = 0
for i in a:
	k += i ** 2
s = sum(a) ** 2
print((s - k) // 2 % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
sm = [0]
for i in range(N):
	sm.append(sm[i] + A[i])
ans = 0
for j in range(N):
	ans += A[j] * (sm[N] - sm[j + 1])
print(ans % (10 ** 9 + 7))


n = int(input())
A = [*map(int, input().split())]
s = sum(A)
s *= s
for a in A:
	s -= a * a
s //= 2
print(s % (10 ** 9 + 7))


import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())), dtype=np.int64)
MOD = 10 ** 9 + 7
B = np.cumsum(A[N - 1:0:-1])[::-1]
B %= MOD
print(np.sum(A[:N - 1] * B % MOD) % MOD)


N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
S = 0
M = 0
for i in A:
	S += i
for i in A:
	S -= i
	M += S * i % mod
print(M % mod)


N = int(input())
A = list(map(int, input().split()))
ans = 0
MOD = 10 ** 9 + 7
sum_A = sum(A)
for i in range(N):
	sum_A -= A[i]
	ans += sum_A * A[i]
print(ans % MOD)


n = int(input())
A = list(map(int, input().split()))
k = sum(A)
c = 10 ** 9 + 7
ans = 0
for i in range(n - 1):
	k -= A[i]
	ans = (ans + k * A[i]) % c
print(ans)


N = int(input())
A = list(map(int, input().split()))
c = 0
for i in range(N):
	c = c + A[i] ** 2
print((sum(A) ** 2 - c) // 2 % (10 ** 9 + 7))


n = int(input())
A = list(map(int, input().split()))
S = sum(A)
S2 = sum(map(lambda x: x ** 2, A))
M = 10 ** 9 + 7
print((S ** 2 - S2) // 2 % M)


N = int(input())
A = list(map(int, input().split()))
S = sum(A)
a = 0
for x in A:
	S -= x
	a += S * x
MOD = 10 ** 9 + 7
print(int(a % MOD))


M = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
print((sum(A) ** 2 - sum(map(lambda x: x * x, A))) // 2 % M)


n = int(input())
al = list(map(int, input().split()))
ans = sum(al) ** 2
for a in al:
	ans -= a ** 2
mod = 10 ** 9 + 7
print(ans // 2 % mod)


n = int(input())
l = list(map(int, input().split()))
sm = sum(l)
ans = 0
for i in l:
	sm -= i
	ans += i * sm
	ans %= 1000000007
print(ans)


import sys
input = sys.stdin.readline
n = int(input())
a = [int(_) for _ in input().split()]
mod = 10 ** 9 + 7
ans = 0
s = [0]
a_ = a[::-1]
for i in range(len(a_) - 1):
	s.append(s[i] + a_[i])
s = s[::-1]
s.pop(len(s) - 1)
for i in range(len(a) - 1):
	ans += a[i] * s[i]
	ans = ans % mod
print(ans)


n = int(input())
a = list(map(int, input().split()))
print((sum(a) ** 2 - sum([e ** 2 for e in a])) // 2 % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
sum_list = []
sum_ = 0
for i in range(len(A) - 1):
	sum_ += A[i + 1]
	sum_list.append(sum_)
len_ = 0
for i in range(len(A) - 1):
	len_ += A[i + 1] * (sum_list[-1] - sum_list[i]) % mod
print((len_ + A[0] * sum_list[-1]) % mod)


n = int(input())
a = list(map(int, input().split()))
suma = sum(a)
ans = 0
for i in range(len(a)):
	suma = suma - a[i]
	ans += suma * a[i]
ans = ans % (10 ** 9 + 7)
print(ans)


n = int(input())
A = list(map(int, input().split()))
s = sum(A)
ans = 0
mod = 10 ** 9 + 7
for i in range(n - 1):
	s = s - A[i]
	ans = (ans + A[i] * s) % mod
print(ans)


import sys

def input():
	return sys.stdin.readline().strip()
sys.setrecursionlimit(20000000)
MOD = 10 ** 9 + 7
INF = float('inf')

def main():
	N = int(input())
	A = list(map(int, input().split()))
	for i in range(N):
		A[i] %= MOD
	S = sum(A)
	answer = 0
	for i in range(N):
		S = S - A[i]
		answer += A[i] * S
		answer %= MOD
	print(answer)

def __starting_point():
	main()
__starting_point()


from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
sa = sum(A)
ans = 0
for i in range(N):
	sa -= A[i]
	ans += A[i] * sa
	ans %= 10 ** 9 + 7
print(ans)


n = int(input())
a = list(map(int, input().split()))
sum1 = sum(a)
ans = 0
for x in a:
	sum1 -= x
	ans += x * sum1
print(ans % 1000000007)


n = int(input())
a = list(map(int, input().split()))
s = []
c = 0
ans = 0
for i in a:
	c += i
	s.append(c)
for i in range(n - 1):
	ans += a[i] * (s[n - 1] - s[i]) % 1000000007
print(ans % 1000000007)


N = int(input())
l = list(map(int, input().split()))
ans = 0
i = 0
S = sum(l)
while i < len(l) - 1:
	S = S - l[i]
	ans += l[i] * S
	i += 1
print(ans % 1000000007)


N = int(input())
a = list(map(int, input().split()))
s = 0
res = 0
m = 10 ** 9 + 7
for ai in a:
	res += s * ai % m
	s += ai
print(res % m)


a = int(input())
b = list(map(int, input().split()))
b.sort()
S = 0
c = sum(b)
for i in range(0, a):
	c = c - b[i]
	S = S + b[i] * c
print(S % (10 ** 9 + 7))


n = int(input())
l = list(map(int, input().split()))
suml = sum(l)
num = 0
for i in range(n):
	num += l[i] ** 2
print((suml ** 2 - num) // 2 % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
A = A[::-1]
P = 0
C = 0
for i in range(N):
	P += A[i] * C
	C += A[i]
print(P % (10 ** 9 + 7))


def main():
	N = int(input())
	CONST = 10 ** 9 + 7
	Output = 0
	lis = list(map(int, input().split()))
	lis.sort()
	SumLis = [0] * N
	for (i, item) in enumerate(lis):
		SumLis[i] += SumLis[i - 1] + item
	for (i, item) in enumerate(lis):
		Output += lis[i] * (SumLis[-1] - SumLis[i])
		Output %= CONST
	print(Output)

def __starting_point():
	main()
__starting_point()


N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
t = sum(A) ** 2
d = sum((a * a for a in A))
print((t - d) // 2 % MOD)


n = int(input())
li = list(map(int, input().split()))
s = sum(li)
c = 0
for i in range(n - 1):
	s -= li[i]
	c += li[i] * s
print(c % (10 ** 9 + 7))


n = int(input().strip())
l = list(map(int, input().strip().split()))
s = sum(l)
ans = 0
MOD = 1000000007
for i in range(n - 1):
	ans += l[i] * (s - l[i])
	s -= l[i]
print(ans % MOD)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
t = sum([x * x for x in a])
print((s * s - t) // 2 % (10 ** 9 + 7))


n = int(input())
A = [int(x) for x in input().split()]
mod = 10 ** 9 + 7
a_cum = [0] * (n + 1)
for i in range(n):
	a_cum[i + 1] = a_cum[i] + A[i]
ans = 0
for i in range(len(A) - 1):
	ans += A[i] * (a_cum[n] - a_cum[i + 1]) % mod
ans %= mod
print(ans)


mod = 10 ** 9 + 7
n = int(input())
l = list(map(int, input().split()))
ans = []
a = sum(l)
for i in range(len(l)):
	a = a - l[i]
	ans.append(l[i] * a)
print(sum(ans) % mod)


n = int(input())
A = list(map(int, input().split()))
S = sum(A)
T = sum(map(lambda x: x ** 2, A))
print((S ** 2 - T) // 2 % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
s = 0
for i in range(N):
	s += A[i]
ans = 0
for i in range(N - 1):
	s -= A[i]
	ans += A[i] * s
	ans %= MOD
print(ans)


import numpy as np
n = int(input())
a = sorted(map(int, input().split()))
b = np.cumsum(a)
c = 0
mod = 10 ** 9 + 7
for i in range(n - 1):
	c += int(a[i]) * (int(b[-1]) - int(b[i]))
	c %= mod
print(c)


n = int(input())
a = list(map(int, input().split()))
s = 0
t = 0
for i in range(n):
	s += a[i]
	t += a[i] * a[i]
u = (s * s - t) // 2
print(u % 1000000007)


(n, *l) = map(int, open(0).read().split())
print((sum(l) ** 2 - sum((i ** 2 for i in l))) // 2 % (10 ** 9 + 7))


n = int(input())
A = [int(x) for x in input().split()]
A2 = [int(x) ** 2 for x in A]
print((sum(A) ** 2 - sum(A2)) // 2 % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
B = []
for i in range(N):
	B.append(A[i] ** 2)
ans = (sum(A) ** 2 - sum(B)) // 2
print(ans % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
proA = map(lambda x: x ** 2, A)
S = (sum(A) ** 2 - sum(proA)) // 2
print(S % (10 ** 9 + 7))


n = int(input())
lst = list(map(int, input().split()))
val = sum(lst)
prev = 0
ans = 0
for i in lst:
	prev += i
	ans += i * (val - prev)
print(ans % 1000000007)


n = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
x = 0
for i in range(n - 1):
	sumA -= A[i]
	x += A[i] * sumA
print(x % (10 ** 9 + 7))


n = int(input())
a = input()
a = [int(n) for n in a.split()]
c = 0
aa = sum(a)
for i in range(n):
	aa -= a[i]
	c += a[i] * aa
	c = c % (10 ** 9 + 7)
print(c)


N = int(input())
A = list(map(int, input().split()))
ans = 0
S = sum(A)
p = 10 ** 9 + 7
for i in range(N):
	S -= A[i]
	ans += A[i] * S % p
print(ans % p)


n = int(input())
a = list(map(int, input().split()))
v = sum(a) ** 2
for x in a:
	v -= x ** 2
v = v // 2
print(v % (10 ** 9 + 7))


n = int(input())
a = [int(i) for i in input().split()]
s = sum(a)
mod = 10 ** 9 + 7
res = 0
for i in a:
	s -= i
	res += i * s % mod
print(res % mod)


N = int(input())
a = list(map(int, input().split()))
M = 7 + 10 ** 9
ans = 0
s = 0
for i in range(N - 1):
	s += a[i]
	ans += s * a[i + 1]
	ans %= M
print(ans)


N = int(input())
A = list(map(int, input().split()))
s = 0
s2 = sum(A)
for i in range(N - 1):
	s2 -= A[i]
	s += A[i] * s2
answer = s % (10 ** 9 + 7)
print(answer)


n = int(input())
a = [int(i) for i in input().split()]
b = [i * i for i in a]
print((sum(a) ** 2 - sum(b)) // 2 % (10 ** 9 + 7))


(N, *D) = [list(map(int, s.split())) for s in open(0)]
V = D[0]
A = V.pop(N[0] - 1)
M = 1000000000 + 7
R = 0
for value in reversed(V):
	R = R + A * value % M
	A = A + value
print(R % M)


MOD = 10 ** 9 + 7
n = int(input())
a = list(map(int, input().split()))
a1 = sum(a)
a2 = sum([x * x for x in a])
print((a1 ** 2 - a2) // 2 % MOD)


N = int(input())
l = list(map(int, input().split()))
b = 10 ** 9 + 7
d = sum(l) ** 2
c = 0
for i in range(N):
	c = c + l[i] ** 2
print((d - c) // 2 % b)


MOD = 1000000007
N = input()
A = list(map(int, input().split()))
l = len(A)
s = 0
B = sum(A)
for i in range(l):
	B -= A[i]
	s += A[i] * B
print(s % MOD)


_ = input()
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
s = 0
a = 0
for _a in A:
	s += _a * a
	a = (a + _a) % mod
print(s % mod)


N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
S = sum(A)
S2 = sum(map(lambda x: x * x, A))
print((S * S - S2) // 2 % mod)


n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
ans = 0
m = 10 ** 9 + 7
c = 0
for i in arr:
	c += i
	ans += i * (s - c)
	ans %= m
print(ans)


MOD = 10 ** 9 + 7
N = int(input())
AN = list(map(int, input().split()))
cumsum = [0] * (len(AN) + 1)
for i in range(1, N + 1):
	cumsum[i] = cumsum[i - 1] + AN[i - 1]
ans = 0
for i in range(1, N):
	ans += AN[i] * cumsum[i]
	ans %= MOD
print(ans)


n = int(input())
l = list(map(int, input().split()))
s = sum(l)
s = s ** 2
for i in range(n):
	s = s - l[i] ** 2
s = s // 2
print(int(s % (7 + 10 ** 9)))


n = int(input())
l = list(map(int, input().split()))
A = sum(l) - l[0]
ans = 0
for i in range(len(l)):
	ans += l[i] * A
	if A == l[-1]:
		break
	else:
		A -= l[i + 1]
print(ans % (10 ** 9 + 7))


import sys
from collections import deque, Counter, defaultdict
import bisect
import heapq
import itertools
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
inf = 10 ** 18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))
mod = 998244353
n = ri()
a = rl()
cs = 0
li = []
for i in a:
	cs += i
	li.append(cs)
sum_ = sum(a)
ans = 0
for i in range(len(a)):
	ans += a[i] * (sum_ - li[i]) % MOD
print(ans % MOD)


N = int(input())
A = list(map(int, input().split()))
s = sum(A)
ans = 0
for ai in A:
	ans += ai * (s - ai)
print(ans // 2 % (10 ** 9 + 7))


n = int(input())
a = list(map(int, input().split()))
m = 1000000007
print((sum(a) ** 2 - sum(map(lambda x: x ** 2, a))) // 2 % m)


N = int(input())
s = [int(x) for x in input().split()]
area = sum(s) * sum(s)
for i in s:
	area -= i * i
ans = area // 2
ans = ans % (10 ** 9 + 7)
print(ans)


n = int(input())
v = pow(10, 9) + 7
arr = list(map(int, input().split()))
s = sum(arr)
it = 0
for item in arr:
	it += item ** 2
print((s ** 2 - it) // 2 % v)


n = int(input())
xs = [int(x) for x in input().split()]
F = 10 ** 9 + 7
print((sum(xs) * sum(xs) - sum([x ** 2 for x in xs])) // 2 % F)


KK = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
X = 0
K = sum(A) % KK
for x in A:
	K -= x
	X += K * x
	X %= KK
print(X)


n = int(input())
a = list(map(int, input().split()))
am = sum(a)
sm = 0
for i in range(len(a)):
	am -= a[i]
	sm = (sm + a[i] * am) % (10 ** 9 + 7)
print(sm)


N = int(input())
A = list(map(int, input().split()))
S = sum(A)
S2 = sum(map(lambda x: x * x, A))
print((S * S - S2) // 2 % 1000000007)


n = int(input())
a = list(map(int, input().split()))
a_sum = sum(a)
ans = 0
mod = 10 ** 9 + 7
for i in range(n - 1):
	a_sum = a_sum - a[i]
	ans += a[i] * a_sum
print(ans % mod)


import itertools
n = int(input())
a = list(map(int, input().split()))
ans = 0
lis = list(itertools.accumulate(reversed(a)))
for i in range(n - 1):
	ans += a[i] * lis[n - 2 - i]
print(ans % (10 ** 9 + 7))


n = int(input())
l = [int(j) for j in input().split()]
mod = 10 ** 9 + 7
ll = [i ** 2 for i in l]
ans = (sum(l) ** 2 - sum(ll)) // 2
print(ans % mod)


n = int(input())
a = list(map(int, input().split()))
d = [0] * n
for i in range(n):
	d[i] = a[i] ** 2
print((sum(a) ** 2 - sum(d)) // 2 % (10 ** 9 + 7))


n = int(input())
l = list(map(int, input().split()))
summ = 0
for i in l:
	summ += i
ans = 0
for i in l:
	summ -= i
	ans += i * summ
print(ans % 1000000007)


N = int(input())
X = list(map(int, input().split()))
s = 0
t = 0
for i in range(N):
	s += X[i]
	t += X[i] * X[i]
print((s * s - t) // 2 % 1000000007)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
t = sum(map(lambda x: x ** 2, a))
ans = (s ** 2 - t) // 2 % (10 ** 9 + 7)
print(ans)


import sys
n = int(input())
a = [int(x) for x in input().split()]
s = 0
for i in range(n):
	s += a[i]
c = 0
sum = 0
for k in range(n - 1):
	c += a[k]
	co = s - c
	sum += a[k] * co
print(sum % (10 ** 9 + 7))


n = int(input())
L = list(map(int, input().split()))
cnt = 0
for i in range(n):
	cnt += L[i] ** 2
ans = (sum(L) ** 2 - cnt) // 2
mod = pow(10, 9) + 7
print(ans % mod)


N = int(input())
A = list(map(int, input().split()))
result = sum(A) ** 2
for i in A:
	result -= i ** 2
print(result // 2 % 1000000007)


n = int(input())
li = list(map(int, input().split()))
s = [li[0]]
ans = 0
m = 10 ** 9 + 7
for i in range(n - 1):
	s.append(li[i + 1] + s[i])
for j in range(n - 1):
	ans += li[j] * (s[n - 1] - s[j]) % m
print(ans % m)


n = int(input())
l = list(map(int, input().split()))
a = sum(l[1:])
m = 10 ** 9 + 7
s = 0
for (v, u) in zip(l[:-1], l[1:]):
	s += v * a
	a -= u
print(s % m)


n = int(input())
a_list = list(map(int, input().split()))
sum = 0
s_list = []
ans = 0
for i in range(n):
	sum += a_list[i]
	s_list.append(sum)
for i in range(n - 1):
	ans += a_list[i] * (s_list[n - 1] - s_list[i])
print(ans % (7 + 10 ** 9))


a = s = 0
for x in [*open(0)][1].split():
	x = int(x)
	a += s * x
	s += x
print(a % (10 ** 9 + 7))


mod = 10 ** 9 + 7
n = int(input())
l = list(map(int, input().split()))
sum_l = sum(l)
ans = 0
for i in range(n):
	sum_l -= l[i]
	ans += sum_l * l[i]
print(ans % mod)


(N, *A) = map(int, open(0).read().split())
Asq = [a ** 2 for a in A]
S = (sum(A) ** 2 - sum(Asq)) // 2
print(S % (10 ** 9 + 7))


N = int(input())
A = list(map(int, input().split()))
rsum = [0 for _ in range(N)]
ans = 0
for i in range(N):
	if i == 0:
		continue
	rsum[-(i + 1)] = rsum[-i] + A[-i]
for i in range(N):
	ans += A[i] * rsum[i]
ans %= 10 ** 9 + 7
print(ans)


a = int(input())
b = list(map(int, input().split()))
c = 0
d = b[0]
for i in range(1, a):
	c += d * b[i]
	d += b[i]
print(c % 1000000007)


N = int(input())
A = [int(r) for r in input().split()]
ans = 0
x = 0
mod = 10 ** 9 + 7
for i in range(N):
	ans = (ans + A[i] * x) % mod
	x = (x + A[i]) % mod
print(ans)


n = int(input())
A = list(map(int, input().split()))
s = sum(A)
all = 0
for i in A:
	s -= i
	all += i * s
ans = all % (10 ** 9 + 7)
print(ans)


n = int(input())
l = [int(i) for i in input().split()]
ans = 0
mod = 1000000007
sql = sum(l) ** 2
for i in l:
	ans += i * i
print((sql - ans) // 2 % mod)


mod = 10 ** 9 + 7
n = int(input())
s = list(map(int, input().split()))
s2 = sum(map(lambda x: x ** 2, s))
ans = (sum(s) ** 2 - s2) // 2 % mod
print(ans)


N = int(input())
A = list(map(int, input().split()))
print((sum(A) ** 2 - sum((a ** 2 for a in A))) // 2 % (10 ** 9 + 7))


n = int(input())
l = list(map(int, input().split()))
c = 0
for i in l:
	c += i ** 2
print((sum(l) ** 2 - c) // 2 % (10 ** 9 + 7))


n = int(input())
mod = 10 ** 9 + 7
ls = list(map(int, input().split()))
di = [i ** 2 for i in ls]
print((sum(ls) ** 2 - sum(di)) // 2 % mod)


M = 10 ** 9 + 7
n = int(input())
l = [*map(int, input().split())]
print((sum((i for i in l)) ** 2 - sum((i ** 2 for i in l))) // 2 % M)


n = int(input())
a = [int(x) for x in input().split()]
x = sum(a) ** 2
y = sum([x ** 2 for x in a])
z = (x - y) // 2
mod = 10 ** 9 + 7
print(z % mod)


n = int(input())
aa = list(map(int, input().split()))
b = 0
c = 0
for a in aa:
	b += a
	c += a ** 2
x = (b ** 2 - c) // 2
print(x % (10 ** 9 + 7))


import sys
N = int(input())
A = list(map(int, input().split()))
sum = [A[N - 1]]
ans = 0
for i in range(1, N):
	ans += A[N - i - 1] * sum[-1] % (10 ** 9 + 7)
	ans %= 10 ** 9 + 7
	sum.append((sum[-1] + A[N - i - 1]) % (10 ** 9 + 7))
print(ans)


N = int(input())
a = [int(i) for i in input().split()]
mod = 10 ** 9 + 7
S = sum(a) % mod
T = sum([i * i for i in a]) % mod
print(pow(2, mod - 2, mod) * (S * S - T) % mod)


n = int(input())
a = list(map(int, input().split()))
ans = sum(a) ** 2
for i in a:
	ans -= i ** 2
print(ans // 2 % (10 ** 9 + 7))


(N,) = map(int, input().split())
A = list(map(int, input().split()))
su = A[N - 1]
suu = su * A[N - 2]
for i in reversed(range(1, N - 1)):
	su += A[i]
	suu += su * A[i - 1]
print(suu % (10 ** 9 + 7))


def main():
	N = int(input())
	mod = 10 ** 9 + 7
	a = list(map(int, input().split()))
	ans = 0
	s = 0
	for i in range(1, N):
		s += a[i]
	for i in range(1, N):
		ans += a[i - 1] * s % mod
		s -= a[i]
	print(ans % mod)

def __starting_point():
	main()
__starting_point()


MOD = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
ans = sum(A) ** 2
ans -= sum(map(lambda x: x ** 2, A))
ans //= 2
print(ans % MOD)


N = int(input())
A = [0] * N
S = [0] * (N + 1)
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
for i in range(N):
	S[i + 1] = (S[i] + A[i]) % mod
cnt = 0
for j in range(N - 1):
	cnt += A[j] * (S[N] - S[j + 1]) % mod
print(cnt % mod)


N = int(input())
A = [int(x) for x in input().split()]
sumA = sum(A)
ans = sum((a * (sumA - a) for a in A)) // 2 % 1000000007
print(ans)


N = int(input())
A = list(map(int, input().split()))
M = 10 ** 9 + 7
Ans = 0
T = sum(A)
for i in range(N - 1):
	T -= A[i]
	Ans += A[i] * T
	Ans %= M
print(Ans)


l = int(input())
k = list(map(int, input().split()))
s = 0
mod = 10 ** 9 + 7
o = sum(k)
p = o ** 2
s = 0
for i in k:
	s = s + i ** 2
print((p - s) // 2 % mod)


n = int(input())
A = list(map(int, input().split()))
ao = 10 ** 9 + 7
zt = sum(A) ** 2
for _ in A:
	zt -= _ ** 2
zt = zt // 2 % ao
print(zt)


n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
temp = sum(a) ** 2
for x in a:
	temp -= x ** 2
print(temp // 2 % mod)


n = int(input())
alst = list(map(int, input().split()))
mod = 10 ** 9 + 7
lst_sm = sum(alst)
sm = lst_sm ** 2
for i in range(n):
	sm -= alst[i] ** 2
sm //= 2
print(sm % mod)


n = int(input())
a = list(map(int, input().split()))
b = [0]
s = 0
for i in range(n):
	b += [b[-1] + a[i]]
	s += b[-2] * a[i]
print(s % (10 ** 9 + 7))


from functools import reduce
from operator import add
N = int(input())
A = list(map(int, input().split()))

def cum(total, a):
	total.append(a + total[-1])
	return total
cumsum = reduce(cum, A, [0])
cum_last = cumsum[-1]
p = 1000000007

def i_fixed(i):
	return A[i] * (cum_last - cumsum[i + 1]) % p

def solve():
	return map(i_fixed, range(N - 1))
print(sum(solve()) % p)


n = int(input())
aa = list(map(int, input().split()))
su = sum(aa)
new = [n ** 2 for n in aa]
sa = sum(new)
x = 10 ** 9 + 7
print((su ** 2 - sa) // 2 % x)
sa


n = int(input())
a = list(map(int, input().split()))
lim = 10 ** 9 + 7
ans = 0
temp = a[-1]
for i in range(2, n + 1):
	ans += a[-i] * temp
	temp += a[-i]
print(ans % lim)


n = int(input())
a = list(map(int, input().split()))
s = 0
v = 0
for i in range(n):
	s += a[i]
	v += a[i] ** 2
s_2 = s ** 2
print((s_2 - v) // 2 % (10 ** 9 + 7))


mod = 10 ** 9 + 7
N = int(input())
A = list(map(int, input().split()))
S1 = sum(A) ** 2
S2 = sum((a * a for a in A))
print((S1 - S2) // 2 % mod)


N = int(input())
A = list(map(int, input().split()))
sm = 0
mod = 10 ** 9 + 7
s = sum(A)
for i in A:
	s -= i
	sm += i * s
	sm %= mod
print(sm)


input()
a = (*map(int, input().split()),)
print((sum(a) ** 2 - sum((x * x for x in a))) // 2 % (10 ** 9 + 7))


def __starting_point():
	N = int(input())
	A = list(map(int, input().lstrip().split(' ')))
	cumA = [A[0]]
	for i in range(1, N):
		cumA.append(cumA[-1] + A[i])
	count = 0
	for i in range(N - 1):
		count += A[i] * (cumA[N - 1] - cumA[i])
	print(count % (10 ** 9 + 7))
__starting_point()


n = int(input())
a = list(map(int, input().split()))
x = sum(a)
y = sum((a[i] ** 2 for i in range(n)))
z = (x * x - y) // 2
print(int(z % 1000000007))


N = int(input())
A = list(map(int, input().split()))
A2 = [A[0]]
mod = 10 ** 9 + 7
res = 0
for i in range(1, N):
	A2.append((A2[i - 1] + A[i]) % mod)
for i in range(N - 1):
	res = (res + A[i] * (A2[N - 1] - A2[i])) % mod
print(res)


N = int(input())
A = list(map(int, input().split()))
p = 10 ** 9 + 7
sumA = sum(A)
sumA_2 = sum([a ** 2 for a in A])
print((sumA ** 2 - sumA_2) // 2 % p)


n = int(input())
a = list(map(int, input().split()))
a.sort()
s = sum(a)
x = [i ** 2 for i in a]
x = sum(x)
ans = (s ** 2 - x) // 2
mod = 10 ** 9 + 7
print(ans % mod)


n = int(input())
(*a,) = map(int, input().split())
b = [x ** 2 for x in a]
mod = 10 ** 9 + 7
print((sum(a) ** 2 - sum(b)) // 2 % mod)


n = int(input())
mod = int(1000000000.0 + 7)
(ans, t) = (0, 0)
for a in list(map(int, input().split())):
	ans = (ans + a * t) % mod
	t += a
print(ans)


N = int(input())
mod = 10 ** 9 + 7
A = list(map(int, input().split()))
Asum = sum(A)
ans = 0
for a in A:
	Asum -= a
	ans += a * Asum % mod
print(ans % mod)


N = int(input())
A = list(map(int, input().split()))
SUM = sum(A)
res = 0
for i in range(N):
	SUM -= A[i]
	res += A[i] * SUM
print(res % (10 ** 9 + 7))


import itertools
n = int(input())
A = list(map(int, input().split()))
A2 = []
for i in range(n):
	A2.append(A[n - 1 - i])
acc_A2 = list(itertools.accumulate(A2))
mod = 10 ** 9 + 7
goukei = 0
for i in range(n - 1):
	goukei += A[i] * acc_A2[n - 2 - i]
print(goukei % mod)


n = int(input())
a = list(map(int, input().split()))
suma = sum(a)
c = 0
for num in a:
	c += num ** 2
print((suma ** 2 - c) // 2 % (10 ** 9 + 7))


n = int(input())
A = list(map(int, input().split()))
total = sum(A) ** 2
for i in range(n):
	total -= A[i] ** 2
ans = total // 2 % (10 ** 9 + 7)
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
s2 = sum(map(lambda x: x * x, a))
print((s ** 2 - s2) // 2 % (10 ** 9 + 7))


n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
S = sum(a)
S2 = sum(map(lambda x: x * x, a))
print((S * S - S2) // 2 % mod)


def cusum(a):
	cusum = [a[0]]
	for i in range(len(a) - 1):
		cusum.append(cusum[i] + a[i + 1])
	return cusum

def solve1(n, a):
	ans = 0
	mod = 10 ** 9 + 7
	cusum_a = cusum(a)
	for i in range(n):
		ans += a[i] * (cusum_a[-1] - cusum_a[i])
		ans %= mod
	return ans
n = int(input())
a = list(map(int, input().split(' ')))
print(solve1(n, a))


n = int(input())
A = list(map(int, input().split()))
S = sum(A) ** 2 - sum(map(lambda x: x ** 2, A))
print(S // 2 % 1000000007)


N = int(input())
As = list(map(int, input().split()))
out = (sum(As) ** 2 - sum([a ** 2 for a in As])) // 2
print(out % (10 ** 9 + 7))


k = 10 ** 9 + 7
N = int(input())
L = list(map(int, input().split()))
S = sum(L) ** 2
for i in L:
	S -= i ** 2
print(S // 2 % k)


def cumsum(xs):
	result = [xs[0]]
	for x in xs[1:]:
		result.append(result[-1] + x)
	return result
N = int(input())
A = list(map(int, input().split()))
ans = 0
MOD = 10 ** 9 + 7
A.reverse()
ruisekiwa = cumsum(A)
ruisekiwa.reverse()
A.reverse()
for i in range(N - 1):
	ans = (ans + A[i] * ruisekiwa[i + 1]) % MOD
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = sum(a)
ans = 0
sub = 0
for i in range(n):
	ans += a[i] * s
for j in range(n):
	sub += a[j] ** 2
print((ans - sub) // 2 % (10 ** 9 + 7))


n = int(input())
a = list(map(int, input().split()))
mod = 1000000007
sum = 0
ans = 0
for i in range(n):
	ans = (ans + a[i] * sum) % mod
	sum += a[i]
print(ans)


N = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
A_sum = sum(A)
result = 0
for i in range(N):
	A_sum = A_sum - A[i]
	result += A[i] * A_sum % mod
print(result % mod)


input()
a = list(map(int, input().split()))
c = 1000000007
print((sum(a) ** 2 - sum(map(lambda x: x ** 2, a))) // 2 % c)


n = int(input())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7
sum_a = sum(A)
ans = 0
for a in A:
	ans += a * (sum_a - a)
print(ans // 2 % mod)


n = int(input())
x = list(map(int, input().split()))
edge = sum(x)
print((edge ** 2 - sum([X * X for X in x])) // 2 % (10 ** 9 + 7))


s = 0
r = 0
n = int(input())
l = list(map(int, input().split()))
for e in l:
	r += e * s
	s += e
mod = int(1000000000.0 + 7)
print(r % mod)


N = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
c = 0
for a in A:
	c += a ** 2
print((sumA ** 2 - c) // 2 % (10 ** 9 + 7))


def cin():
	return list(map(int, input().split()))
N = int(input())
A = cin()
total = 0
store = 0
for i in range(N - 1):
	store += A[N - i - 1]
	total += A[N - i - 2] * store
print(total % (pow(10, 9) + 7))


N = int(input())
A = [int(i) for i in input().split()]
A_sum = sum(A)
A2 = sum([i * i for i in A])
print((A_sum * A_sum - A2) // 2 % (10 ** 9 + 7))


N = int(input())
K = [int(n) for n in input().split()]
S = sum(K)
total = 0
for i in range(N):
	S -= K[i]
	total += K[i] * S
print(total % (10 ** 9 + 7))


mod = 10 ** 9 + 7
(N, *A) = map(int, open(0).read().split())
S1 = sum(A)
S2 = sum((a * a for a in A))
print((S1 ** 2 - S2) // 2 % mod)


n = int(input())
a = [int(i) for i in input().split()]
d = sum(a) ** 2
e = 0
for i in range(0, len(a)):
	e = e + a[i] * a[i]
print((d - e) // 2 % 1000000007)


(n, *a) = map(int, open(0).read().split())
b = sum(map(lambda x: x * x, a))
mod = 10 ** 9 + 7
ans = (sum(a) ** 2 - b) // 2
print(ans % mod)


N = int(input())
A = list(map(int, input().split()))
mysum = sum(A)
total = 0
for i in range(N - 1):
	mysum -= A[i]
	total += A[i] * mysum
print(total % 1000000007)