Problem:


Score : 300 points

Problem Statement

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 1

3
1 2 3

Sample Output 1

11

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

Sample Input 2

4
141421356 17320508 22360679 244949

Sample Output 2

437235829



Sample input:
3
1 2 3


Sample output:
11

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
m=10**9+7
s=0
for c in range(n):
    s=(s+a[c])%m
s=pow(s,2,m)
for c in range(n):
    s=(s-pow(a[c],2,m))%m
if s%2==0:
    print(s//2)
else:
    print((s+m)//2)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
mod = (10 ** 9) + 7
answer = 0
sum_A = sum(A)

for i in range(N):
    sum_A -= A[i]
    answer += A[i] * sum_A % mod
print(answer % mod)

Submission status: Accepted

Solution code:
#: Author - Soumya Saurav
import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

n = int(input())
a = list(map(int,input().split()))
s = sum(a)
mod = 10**9+7

res = 0

for e in a:
    s -= e
    res = (res+s*e)%mod
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))


p = 10**9+7

ans = 0
s = sum(a)%p
ans = (s**2)%p
for i in range(n):
    ans -= (a[i]**2)%p
t = ans%p
if t%2==0:
    print(t//2)
else:
    print((t+p)//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 1000000007
n = int(input())
a = [int(_) for _ in input().split()]
p = [0] * (n + 1)
for i in range(n - 1, -1, -1):
    p[i] = p[i + 1] + a[i]
ans = 0
for i in range(n):
    add = a[i] * (p[i] - a[i])
    add = add % MOD
    ans += add
    ans = ans % MOD
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))

    mod = 10**9 + 7

    tmp = [0] * N

    s = 0
    for i in range(N):
        tmp[- i - 1] = s
        s += A[-i - 1]

    # print(tmp)

    ans = 0
    for i in range(N):
        ans += A[i] * tmp[i]

    print(ans % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int, input().split()))

mod = 10 ** 9 + 7

a.reverse()
cumsum = a[0]
sp = 0

for i in a[1:]:
    sp += (cumsum * i) % mod
    sp %= mod

    cumsum += i
    cumsum %= mod

print(sp)

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
a = [int(i) for i in input().split()]
mod = 10 ** 9 + 7
a_acc = list(accumulate(a))
ans = 0
for i in range(n):
    ans += (a[i] * (a_acc[-1] - a_acc[i])) % mod
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

A_sum = sum(A)
B = [0]*N

for i in range(N):
  B[i] = A[i]**2
  

ans = (A_sum**2 - sum(B))//2%(10**9+7)

print(ans)

Submission status: Accepted

Solution code:
mod = 10**9+7

N = int(input())
A = list(map(int, input().split()))

s = 0
for i in range(N):
    s += A[i]
ans = 0
for i in range(N):
    ans += (A[i]*(s-A[i]))%mod
    s -= A[i]
    ans = ans % mod

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))

Asum = sum(As)

ans = 0
for index, A in enumerate(As):
    ans += (A * (Asum - A))
    ans = ans % (10**9+7)
    Asum -= A
#    print(
#    print(As)

print(ans)

Submission status: Accepted

Solution code:
def main():
        
    import numpy as np

    N = int(input())
    numbers = list(map(int, input().split()))

    np_numbers = np.array(numbers, np.int64)

    module = 10 ** 9 + 7

    cumsum = np.cumsum(np_numbers)
    cumsum.tolist()
    summ = cumsum[-1]
    ans = 0

    for i in range(N):
        tmp = (summ - cumsum[i]) % module
        ans += ((np_numbers[i] % module) * tmp) % module
        
        ans %= module
        
        
        

    ans %= module

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
arr1 = list(map(int,input().split()))
arr2 = [0 for _ in range(N)]
sum = 0
for i in range(N):
    arr2[i] = sum+arr1[i]
    sum = arr2[i]

k = 10**9+7

count = 0
for i in range(N-1):
    count += arr2[i]*arr1[i+1]
    count %= k

print(count)

Submission status: Accepted

Solution code:
def main():
	N = int(input())
	A = [int(a) for a in input().split(" ")]
	p = 1000000007
	S = 0
	Asq = 0
	for i in range(len(A)):
		x = A[i]
		S += x % (2 * p)
		Asq += (x * x) % (2 * p)
	t = ((S * S) % (2 * p) - Asq) % (2 * p)
	print(int(t / 2))

main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))

ALL = sum(A)
res = 0

for a in A: 
    ALL -= a    
    res = (res + ALL *a) % (10**9+7)

print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

reversed_cumsum = []
tmp = 0
for i in range(N):
    tmp += A[N - 1 - i]
    reversed_cumsum.append(tmp)
cumsum = reversed_cumsum[::-1]

ans = 0
for i in range(N - 1):
    ans = (ans + A[i] * cumsum[i + 1]) % (10 ** 9 + 7)
print(ans)

Submission status: Accepted

Solution code:
def f(A, n): 
    array_sum = 0
    for i in range(0, n, 1): 
        array_sum += A[i]
    array_sum_square = (array_sum * array_sum)
    individual_square_sum = 0
    for i in range(0, n, 1): 
        individual_square_sum += (A[i] * A[i]) 
    return (array_sum_square - individual_square_sum) // 2 
n = int(input())
a = [int(x) for x in input().split()]
MOD = 10**9 +7

print(int(f(a,n))%MOD)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

answer = 0
mod = 10**9 + 7
total_sum = sum(a)
for idx in range(n - 1):
    total_sum -= a[idx]
    answer += a[idx] * total_sum

print(answer % mod)

Submission status: Accepted

Solution code:
n = int(input())
numbers = [int(x) for x in input().split()]
r = 10**9 + 7

cum = [0] * n
cum[0] = numbers[0]
for i in range(1, n):
    cum[i] += cum[i - 1] + numbers[i]
    cum[i] %= r
   
ans = 0
for i in range(n - 1):
    tmp = numbers[i] * (cum[n - 1] - cum[i])
    tmp %= r
    ans += tmp
    ans %= r

print(ans)

Submission status: Accepted

Solution code:
mod = pow(10, 9)+7

n = int(input())
a_list = list(map(int, input().split()))
a_sum = sum(a_list)
a_sq = sum(map(pow, a_list, [2]*n))

a = (a_sum**2//2)%mod
b = (a_sq//2%mod)
print((a-b)%mod)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7

all_item = sum(a)

ans = 0
for i in range(n):
    all_item = (all_item - a[i])%mod
    ans = (ans + all_item*a[i])%mod
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
import sys
n = int(input())
a = list(map(int, input().split()))
ans = 0
all = 0
for i in a:
    all += i
for i in a:
    all -= i
    ans += i*all
    if(ans > 10**9+7):
        ans %= 10**9+7
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    n = int( input() )
    A = list( map( int , input().split() ) )
    A_sum = sum(A)
    A_sum -= A[0]
    ans = 0

    for i in range( n - 1 ) :
        a_i = A[i]
        ans += ( (A_sum * a_i) % (10 ** 9 + 7 ) )
        A_sum -= A[ i + 1 ]
        ans %= ( 10 ** 9 + 7 )

    print(ans)



main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

from itertools import accumulate
N = int(input())
A = list(map(int, input().split()))
B = list(accumulate(A))
mod = 10**9+7
ans = 0
sumA = sum(A)

for i in range(N-1):
    ans += A[i] * (sumA-B[i]) % mod
    ans = ans%mod
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sumlist = [0]*(n-1)
cursum = 0
for i in range(n-1):
  cursum += a[n-1-i]
  sumlist[n-2-i] = cursum
totalsum = 0
for j in range(n-1):
  totalsum += a[j]*sumlist[j]
print(totalsum%((10**9)+7))

Submission status: Accepted

Solution code:
mod=10**9+7

n=int(input())
a=list(map(int,input().split()))
a=[0]+a
ruiseki=[0]*(n+1)

for i in range(n+1):
  ruiseki[i]=(ruiseki[i-1]+a[i])

ans=0
for i in range(1,n+1):

  ans+=(a[i]*(ruiseki[n]-ruiseki[i]))
print(ans%mod)

Submission status: Accepted

Solution code:
n = int(input())
line = list(map(int, input().split()))
ans = 0
x = sum(line)
mod = 10**9 + 7
for i in range(n):
  x -= line[i]
  ans += line[i] * x
  if ans > mod:
    ans = ans % mod
print(ans)

Submission status: Accepted

Solution code:
inpl = lambda: list(map(int,input().split()))
MOD = 10**9+7
N = int(input())
A = inpl()
S = sum(A) % MOD
T = sum(a*a % MOD for a in A) % MOD
U = S*S - T
if U % 2:
    U += MOD
ans = (U//2) % MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7

p = sum(A)

ans = 0
for i in range(N):
    p -= A[i]
    ans += (A[i] * p) % MOD
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
data=list(map(int,input().split()))
num=0
ans=0
for i in range(len(data)):
    num+=data[i]
for i in range(len(data)):
    num-=data[i]
    temp=(num*data[i])
    ans+=temp
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
import math
n  = int(input())
arr = list(map(int , input().split()))
pre =[0]*(n+1)
mod = 10**9+7
pre[0]=arr[0]
for i in range(1 , n):
	pre[i]=pre[i-1]+arr[i]
	pre[i]=pre[i]%mod
ans = 0
for i in range(0 , n):
	ans = ans + (arr[i])*(pre[n-1]-pre[i])
	ans = ans%mod
print(ans)

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	l = list(map(int, input().split()))
	a = sum(l)
	a *= a
	b = sum([i * i for i in l])
	a = (a - b) // 2
	mod = 10**9 + 7
	print(a % mod)

main()

Submission status: Accepted

Solution code:
N = int(input()) 
A = list(map(int, input().split()))

biggest = 0
humidai = 0

result = 0

tmp_list = []

tmp=sum(A)

for i in range(N-1):

    # for j in range(i+1,N):
        # tmp += A[j]
    tmp -= A[i]
    
    result += A[i]*tmp

    if result>10**9+7:
        result = result%(10**9+7)

result = result%(10**9+7)

print(result)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))

mod = int(1e9+7)

S = sum(A)

ans = 0
for i in range(N):
  S -= A[i]
  ans += A[i] * S
  ans %= mod

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
MOD = 10**9 + 7
A_s = list(map(int, input().split()))
ruiseki = [] 
for i, A in enumerate(A_s):
    if i == 0:
        ruiseki.append(A)
        continue
    ruiseki.append(ruiseki[i-1]+A)
answer = 0
for i, r in enumerate(ruiseki):
    answer += (A_s[i] * (ruiseki[N-1]-ruiseki[i]))
    answer %= MOD
print(answer)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
p = 10**9 + 7

acmlator = sum(a[1:]) % p
ans = (a[0] * acmlator) % p
if n == 2:
    print(ans)
else:
    for i in range(1, n - 1):
        acmlator = (acmlator - a[i]) % p
        ans = (ans + a[i] * acmlator) % p
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

mod = 10**9 + 7
tot = sum(A)
ans = 0

for i in range(N):
    tot -= A[i]
    lin = tot * A[i]
    ans += lin % mod
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
lst=list(map(int,input().strip().split()))
s1=sum(lst)
num=s1*s1
denom=0 
mod=1000000007
for i in range(0,n):
        denom+=(lst[i]*lst[i])
print(((num-denom)//2)%mod)

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    A = [0] + input_int_list()  # 1-indexed
    ans = 0
    cusum = [0] * (n + 1)
    MOD = (10**9) + 7

    for i in range(1, n + 1):
        cusum[i] = (A[i] + cusum[i - 1]) % MOD

    ans = 0

    for i in range(1, n):
        ans += (A[i] * (cusum[n] - cusum[i]))
        ans %= MOD
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
li = list(map(int,input().split()))#[1, 2, 3]
snum = sum(li[1:])
#li.append(0)
#print(snum)
mod = (10**9+7)
res = 0
ren = 1

for v in li:
    res += (v*snum)
    if ren < len(li):
        snum-=li[ren]
        ren+=1
print(res%mod)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))

listsum=[]
s=0
for i in range(N):
  s+=A[N-1-i]
  listsum.append(s)
  
S=0
for i in range(N-1):
  S+=A[i]*listsum[N-2-i]
  S=S%(10**9+7)
  
print (S)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
mod = 10**9+7
ans = 0
A_sum = sum(A)
for n in range(0,N-1):
    A_sum -= A[n]
    temp = A[n] * A_sum % mod
    ans += temp
    ans = ans % mod
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, stdout  
  
n = stdin.readline()
arr = [int(x) for x in stdin.readline().split()] 

summation = 0
for x in arr: summation += x
sqsum = 0
for x in arr: sqsum += (x*x)

ans = (summation*summation - sqsum) // 2
ans = (ans % 1000000007)

stdout.write(str(ans))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))

x=sum(a)-a[0]
ans=0

for i in range(n):
    ans = (ans + a[i]*x)%(10**9 + 7)
    if i!=n-1:
        x-=a[i+1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(T) for T in input().split()]
RevA = A[::-1]
SumA = [0]*(N+1)
for TN in range(0,N):
    SumA[TN+1] = SumA[TN]+RevA[TN]
SumA = SumA[::-1]
Sum = 0
Mod = 10**9+7
for TA in range(1,N):
    Sum += (A[TA-1]*SumA[TA])%Mod
print(Sum%Mod)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = [0]

mod = 10**9 + 7

for i in range(N-1):
  S.append(S[-1] + A[-1-i])

S.reverse()

ans = 0

for i in range(N-1):
  ans += A[i] * S[i] % mod
  ans %= mod

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
mod = 1000000007
pref = [arr[0]]

for i in arr[1:]:
	pref += [(i + pref[-1]) % mod]

res = 0
for i in range(n - 1):
	res = (res + (arr[i] * (pref[-1] - pref[i])) % mod) % mod
	
print(res)

Submission status: Accepted

Solution code:
#ここまでテンプレ
from sys import stdin
from sys import setrecursionlimit
setrecursionlimit(10 ** 7)

n = int(stdin.readline().rstrip())
a = list(map(int,stdin.readline().rstrip().split()))

point = 0
mod = 10**9+7

from itertools import accumulate

li = list(accumulate(a))

for i,j in enumerate(a):
    point += j*(li[-1]-li[i])%mod

print(point%mod)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
N = int(input())
arr = list(map(int, input().split()))
arr_sum = sum(arr)
arr2 = [0]*N
for i in range(N):
    arr2[i] = arr_sum - arr[i]
ans = 0
for i in range(N):
    ans += arr2[i]*arr[i]
print((ans//2)%MOD)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
INF = 10 ** 9 + 7
s = A[0] * A[1] % INF
tmp = A[0]
for n in range(2,N):
  tmp += A[n-1] % INF
  s = (s + A[n] * tmp) % INF
print(s)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
Ans = 0
S = sum(A)

for i in range(N-1):
    S -= A[N-i-1]
    Ans += A[N-i-1]*S

print(Ans%((10**9)+7))

Submission status: Accepted

Solution code:
n=int(input())
mod=10**9+7
A=list(map(int,input().split()))

a=sum(A)
a=(a**2)

b=0
for i in range(n):
  b+=(A[i])**2
if a-b<0:
  c=a-b
  while c<=0:
    c+=mod
  
  ans=c//2
  ans=ans%mod

else:
  ans=(a-b)//2
  ans=ans%mod

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
total = 0
MOD = 10**9+7
back= sum(a)

for i in range(n-1):
    back -= a[i]
    total += a[i] * back
    total = total % MOD
print(total)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
mod = 10**9+7

x = int(input())

s = list(map(int, input().split()))


def bnp(bs, pw):
    res = 1
    while pw:
        if pw % 2:
            res = (res*bs) % mod
        bs = (bs*bs) % mod
        pw //= 2

    return res


sm = 0
for n in s:
    sm = (sm+n) % mod
sm %= mod

sm = (sm*sm) % mod

sqr = 0

for n in s:
    p = (n*n) % mod
    sqr = (sqr+p) % mod

res = (sm-sqr+mod) % mod
res = (res*bnp(2, mod-2)) % mod

print(res % mod)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

ans = 0
m = pow(10, 9)+7
total = sum(a)
for i in range(n-1):
    total -= a[i]
    ans += a[i]%m * total%m
print(ans%m)

Submission status: Accepted

Solution code:
mod = 10**9+7
input()
a = map(int, input().split())
a = [x%mod for x in a]
def sum(arr):
    res = 0
    for e in arr:
        res += e
        res %= mod
    return res
print((sum(a)**2 - sum(x**2%mod for x in a)) * (10**9//2+4) % mod)

Submission status: Accepted

Solution code:
n=int(input())
A=[int(_) for _ in input().split()]
full=0
sumsum=0
for a in range(len(A)-1):
    sumsum=(sumsum+A[len(A)-a-1])%(10**9+7)
    full=(full+sumsum*A[len(A)-a-2])%(10**9+7)

print(full)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))


def mod(x):
    return x%(10**9+7)


sum_num = 0
ans = 0
for i in range(len(a)-1):
    idx = len(a)-i-1
    sum_num += a[idx]
    sum_num = mod(sum_num)
    ans += sum_num*a[idx-1]
    ans = mod(ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9 + 7

ans = 0
x = a[-1]
for i in reversed(range(n-1)):
   ans += a[i] * x
   ans %= mod
   x += a[i]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(a) for a in input().split()]

summ = sum(arr)
ans = 0
for i in range(n-1):
    a = arr[i]
    summ -= arr[i]
    b = summ
    ans += (a*b)%(int(1e9+7))

print(ans%(int(1e9+7)))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

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

s.reverse()
ans = 0
for i in range(n-1):
  #print (a[n-1-i],s[i+1])
  ans += a[n-1-i] *s[i+1]
  ans %= mod
print (ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7

sqsum = 0
asum = 0
for a in A:
  sqsum += a**2
#  sqsum %= MOD
  asum += a
#  asum %= MOD

ans = ((asum ** 2) - sqsum) // 2
ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))

MOD = 10**9+7

ans = sum(A)%MOD
ans *= sum(A)
ans %= MOD

temp = 0
for i in range(N):
    X = A[i]%MOD
    Y = X**2
    Y %= MOD
    temp += Y
    

    
print(((ans-temp)*pow(2, MOD-2, MOD)%MOD))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
MOD=10**9+7
def findProductSum(A, n): 
    array_sum = 0
    for i in range(0, n, 1): 
        array_sum=array_sum+A[i]
    array_sum_square = array_sum * array_sum 
    individual_square_sum = 0
    for i in range(n): 
        individual_square_sum += A[i] * A[i]
    return (array_sum_square - individual_square_sum)//2

print(findProductSum(a,n)%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
from collections import Counter
import queue
import copy
import bisect
import heapq
import itertools


sys.setrecursionlimit(10**7)
#N, Q = map(int, input().split())
#G = [list(input()) for i in range(H)]
#INF = V * 10001
#A = [int(i) for i in input().split()]
#AB = [list(map(int, input().split())) for _ in range(K)]

N = int(input())
A = [int(i) for i in input().split()]
ans = 0
MO = 10**9+7
SA = [0]*N
SA[N-1] = A[N-1]
for i in reversed(range(1, N)):
    SA[i-1] = SA[i]+A[i-1]

for i in range(N-1):
    ans += A[i]*SA[i+1]%MO

print(ans%MO)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

def main():
    n = int(input())
    a_arr = list(map(int, input().split()))
    mod = 10**9 + 7

    a_sum = sum(a_arr)

    ans = 0
    for i in range(n):
        a = a_arr[i]
        a_sum -= a
        ans += a * a_sum % mod
        ans %= mod

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))

res = 0
mod = 10**9 + 7

sum_A = 0
for i in range(N):
  sum_A += A[i]

for i in range(N-1):
  sum_A -= A[i]
  res += sum_A * A[i]
  
print(res % mod)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))

# sum_A[j] := A[0]～A[j-1]までの和
sum_A = [0 for _ in range(n)]
for j in range(1, n):
    sum_A[j] = sum_A[j - 1] + A[j - 1]

ans = 0
for j in range(1, n):
    ans += A[j] * sum_A[j]
    ans = ans % (10**9 + 7)
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]
    mod = 10**9+7
    sumA = sum(a)
    sumB = 0
    count = 0

    for i in range(n-1):
        sumB+=a[i]
        count += ((sumA-sumB)*a[i])%mod
    print(count%mod)

Submission status: Accepted

Solution code:
n = int(input())
yokolist = list(map(int, input().split()))

sum = ((sum(yokolist) % (10**9+7))**2 )% (10**9+7)

sum_2 = 0
for i in range(n):
    sum_2 += (yokolist[i]*yokolist[i] % (10**9+7))
    sum_2 = sum_2 % (10**9+7)

answer = (sum - sum_2) % (10**9+7)

if answer % 2 == 0:
    print(answer//2)
else:
    answer = answer + 10**9+7
    print(answer//2)

Submission status: Accepted

Solution code:
I=lambda:list(map(int,input().split()))
mod=10**9+7
n,=I()
l=I()
pre=l.copy()
suf=l.copy()
for i in range(n-2,-1,-1):
	suf[i]+=suf[i+1]
ans=0
#print(suf)
for i in range(n-1):
	ans+=(l[i]*suf[i+1])
	ans%=mod
print(ans)

Submission status: Accepted

Solution code:
#-------------------------------------------------------------------
MOD = 10**9+7
#-------------------------------------------------------------------

N = int(input())
A = [int(i) for i in input().split()]

from itertools import accumulate
B = list( accumulate(A) )

ans = 0
for i in range(N-1):
  ans += ((B[-1]-B[i]) % MOD) * (A[i] % MOD)
  ans %= MOD

print(ans % MOD)

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
n_sums = [0 for _ in range(N)]
n_sums[0] = nums[N-1]
MOD = 10**9 + 7
for i in range(N-1):
  n_sums[i+1] = n_sums[i] + nums[N-i-2]
  answer = 0
for i in range(N-1):
  answer += n_sums[N-i-2] * nums[i]
  answer = answer%MOD
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]

a_sum=sum(a)
a_sq=[x**2 for x in a]
a_sum_sq=sum(a_sq)
ans=(a_sum**2-a_sum_sq)//2
ans%=10**9+7
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7
a0 = sum(a[1:])
ans = 0

for i in range(n-1):
    ans += (a0*a[i])%mod
    ans %= mod
    a0 -= a[i+1]
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
input = sys.stdin.readline


def readstr():
    return input().strip()


def readint():
    return int(input())


def readnums():
    return map(int, input().split())


def readstrs():
    return input().split()


def main():
    N = readint()
    A = list(readnums())
    a = list(accumulate(A))
    ans = 0
    MOD = 10 ** 9 + 7
    for i in range(N - 1):
        ans += A[i] * (a[-1] - a[i])
        ans %= MOD

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#C
N=int(input())
A=list(map(int,input().split()))
mod=10**9+7

L=[0 for i in range(N)]
for i in range(N-2,-1,-1):
    L[i]+=A[i+1]+L[i+1]

ans=0
for i in range(N-1):
    ans+=A[i]*L[i]
    ans%=mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))

wa = sum(A)
ans = 0
for i in range(n):
    wa -= A[i]
    ans += wa * A[i]

print(ans % ((10**9)+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def findProductSum(A, n): 
      
    # calculating array sum (a1 + a2 ... + an) 
    array_sum = 0
    for i in range(0, n, 1): 
        array_sum += A[i]
  
    # calcualting square of array sum 
    # (a1 + a2 + ... + an)^2 
    array_sum_square = (array_sum * array_sum )
  
    # calcualting a1^2 + a2^2 + ... + an^2 
    individual_square_sum = 0
    for i in range(0, n, 1): 
        individual_square_sum += (A[i] * A[i]) 
  
    # required sum is (array_sum_square - 
    # individual_square_sum) / 2 
    return (array_sum_square - individual_square_sum) // 2 
n = int(input())
a = [int(x) for x in input().split()]
MOD = 10**9 +7

print(int(findProductSum(a,n))%MOD)

# print(s%MOD)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))

ans = 0
tmp = 0
for i in range(N-2, -1, -1):
    tmp += A[i+1]
    ans += A[i] * tmp

print(ans % 1000000007)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

n = int(input())
A = list(map(int, input().split()))

s =  sum(A)
result = 0
for i in range(n):
    s -= A[i]
    result += A[i] * s
    result %= MOD

print(result)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))

mod=10**9+7
import numpy as np

cum=[]

prev=0
for i in range(n):
    tmp = prev%mod +a[i]%mod
    cum.append(tmp)
    prev = cum[-1]


ans=0
for i in range(n-1):
    p=a[i]
    t = cum[-1] - cum[i]

    ans+=(p*t)%mod

print(ans%mod)

Submission status: Accepted

Solution code:
import sys
n = int(sys.stdin.readline().rstrip("\n"))
a = [int(s) for s in sys.stdin.readline().rstrip("\n").split()]
p = 10**9+7
s = 0
s_2 = 0
for i in a:
    s += i
    s = s % p
    s_2 += i*i
    s_2 = s_2 % p
ans = ((s**2 - s_2) % p)*(2**(p-2)) % p
print(ans)

Submission status: Accepted

Solution code:
n, *aa = map(int, open(0).read().split())
MOD = 10**9+7
print(
    (
        (sum(aa)%MOD) ** 2 % MOD
        - sum(a**2%MOD for a in aa) % MOD
    )
    * pow(2, MOD-2, MOD)
    % MOD
)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

bs = 10**9 + 7
s = sum(a)
sm = 0

for i, ai in enumerate(a):
    s -= ai
    sm += (ai * s) % bs
    sm = sm % bs

print(sm)

Submission status: Accepted

Solution code:
import math,sys,bisect,heapq
from collections import defaultdict,Counter,deque
from itertools import groupby,accumulate
#sys.setrecursionlimit(200000000)
int1 = lambda x: int(x) - 1
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda: map(int,input().split())
alele = lambda: list(map(int, input().split()))
ilelec = lambda: map(int1,input().split())
alelec = lambda: list(map(int1, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
MOD = 1000000000 + 7
def Y(c):  print(["NO","YES"][c])
def y(c):  print(["no","yes"][c])
def Yy(c):  print(["No","Yes"][c])

def findProductSum(A, n): 
    T = list(accumulate(A[::-1]))[::-1]
    #print(T)
    Ans = 0
    for i in range(n-1):
        x = A[i]*T[i+1]
        x %= MOD
        Ans += x
        Ans%= MOD
    return Ans
    

N= int(input())
A  = alele()
print(int(findProductSum(A,N) % MOD))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import accumulate
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a_sum = (accumulate(reversed(a)))
    a_sum = list(reversed(list(a_sum)))
    sum = 0
    mod = 1000000007
    for i in range(n-1):
        sum += a[i]*a_sum[i+1]
    print(sum%mod)

if __name__ == '__main__':
    main()