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:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations, accumulate
from operator import add, mul, sub


import sys
# sys.setrecursionlimit(10**6)
# buff_readline = sys.stdin.buffer.readline
buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A):
    M = 10**9 + 7

    sa = [0] + list(accumulate(A))
    ans = 0
    for i, a in enumerate(A[:-1]):
        ans += a * (sa[-1] - sa[i+1])
        ans %= M

    return ans


def main():
    N = read_int()
    A = read_int_n()
    print(slv(N, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
left = 0
right = 0
for i in range(n):
  left += a[i]
  left %= (10**9+7)
for j in range(n):
  right += a[j]
  right %= (10**9+7)
c = (left*right)%(10**9+7)
l = [(k**2)%(10**9+7) for k in a]
print(((c+(10**9+7)-sum(l)%(10**9+7))*pow(2, 10**9+5, 10**9+7))%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import copy

N = int(input())
A = list(map(int, input().split()))
S,r,m = [0]*N,0,1000000007

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

for a,s in zip(A,S):
    r += (a*s)%m
    r %= m

print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
L=[int(x) for x in input().split()]
sum=0
product=0
mod=1000000007
for i in range(n):
    sum+=L[i]%mod
for i in range(n-1):
    sum-=(L[i]%mod)
    product+=sum*(L[i]%mod)
print(product%mod)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
# ((A合計**2)-ΣAそれぞれ**2)//2
mod = 10**9+7
AA = sum(A)
ans = (AA%mod)**2
ans %= mod
for a in A:
    ans -= a**2
    ans %= mod
ans %= mod
print(ans*pow(2,mod-2,mod)%mod)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
p = 10 ** 9 + 7

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

sum = 0
for i in range(0, N - 1):
	sum += (A[i] * (S[N - 1] - S[i])) % p
print(sum % p)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
m = 1000000007

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

print(ans % m)

Submission status: Accepted

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

b=[0]*n
b[0]=a[0]
for i in range(n-1):
  b[i+1]=b[i]+a[i+1]

c=0
for j in range(n-1):
  c+=a[j]*(b[n-1]-b[j])
  
print(c%(10**9+7))

Submission status: Accepted

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

X = sum(A)
x = 0
ans = 0
for i in range(N):
    x += A[i]
    ans += A[i] * (X-x)

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

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

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

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

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

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

s = sum(a) 

ans = 0

for i in a:
    s -= i
    s %= mod
    ans += i * s
    ans %= mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
mod = 10**9+ 7
a = list(map(int,input().split()))
ans = (sum(a)%mod)**2 - sum([x**2%mod for x in a])
ans *= 5*10**8+4
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

# [1,2,3,4,5,6]の時、累積和は、
# [0,1,3,6,10,15,21]
cumsum=[0]
def my_cumsum(alst):
  for i in range(len(alst)):
    cumsum.append(cumsum[i]+alst[i])
  return cumsum

n=int(input()) #3
alst=list(map(int,input().split()))

# 原因はnp.cumsum関数にある事が判明
# s=np.cumsum(alst)

s=my_cumsum(alst)

sum=0

for i in range(len(alst)-1):
  sum+=alst[i]*(s[len(s)-1]-s[i+1])

print(sum%MOD)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
def solve(n, a):
    t = sum(a)
    return sum((t-x)*x for x in a) // 2 % MOD

n = int(input())
a = list(map(int, input().split()))
print(solve(n, a))

Submission status: Accepted

Solution code:
n = input()
vals = list(map(int, input().split()))
s = sum(vals)
ret = 0
for i, val in enumerate(vals):
    ret += val * (s - val)

mod = 10 ** 9 + 7
print(int(ret//2) % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    MOD = 10**9 +7

    S = 0
    for i in range(len(A)-1):
        tmp = 0
        for j in range(i+1,len(A)):
            tmp+=A[j]
        S+= (tmp%MOD * A[i])%MOD


    print(S%MOD)



from itertools import accumulate

m = 10**9 + 7

N,*A = map(int,open(0).read().split())

result = 0

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




for i in range (N-1):
    result += A[i]*(b[N-1]-b[i])
    result %= m
print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = 0
S2 = 0
MOD = 1000000007
for i in range(N):
  S += A[i]
  S2 += A[i]*A[i]
print((S*S-S2)//2%MOD)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
mod = 1000000007
S = [0]*N
S[N-1] = A[N-1]
for i in range(N-2,0,-1):
    S[i] = int((S[i+1] + A[i])%mod)

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

sum = int(sum%mod)    
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
mod = 1000000007
S = 0
for i in range(N):
    S += A[i]

sum = 0
for i in range(N):
    S -= A[i]
    if S<0:
        S+= mod
    sum += int(A[i]*S%mod)
    

sum = int(sum%mod)    
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
inner_sum, total = 0, 0
for i in range(N):
    inner_sum += A[i]

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

Submission status: Accepted

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

X = 0
Y = 0
for a in A:
    X += a
    Y += a * a
X = (X * X) % mod
Y = Y % mod
Z = (X - Y + mod) % mod
ans = 0
if Z % 2 == 0:
    ans = Z / 2
else:
    ans = (Z + mod) / 2
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
sum_a = sum(a)
ans = 0

for i in a:
    sum_a -= i
    ans += i * sum_a
    ans %= 10**9 + 7
    
print(ans)

Submission status: Accepted

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

mod = 10**9 + 7
S = [0 for i in range(N)]
S[0] = A[0] 
for i in range(1,N):
    S[i] = (S[i-1]+ A[i])%mod

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

print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
p = 10**9+7
sumA = 0
sumsq = 0
for i in range(len(A)):
  a = A[i]
  sumA += a
  sumA = sumA%p
  sumsq += pow(a, 2, p)
  sumsq = sumsq%p
sqsum = pow(sumA, 2, p)
if sqsum < sumsq:
  sqsum += p
if (sqsum - sumsq)%2 != 0:
  sqsum += p
ans = (sqsum - sumsq)//2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

mod = 1000000007
s = 0
for i in range(n):
  s += a[i]
  s %= mod

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

Submission status: Accepted

Solution code:
import numpy as np
MOD = 10 ** 9 + 7

n=int(input()) #3
alst=list(map(int,input().split())) #[1,2,3]

# 累積和を求める

# 例
# [1,2,3,4,5,6]の時、累積和は、
# [0,0+1,0+1+2,0+1+2+3,0+1+2+3+4,0+1+2+3+4+5,0+1+2+3+4+5+6]

s=[0]
for i in range(len(alst)):
  s.append(s[i]+alst[i])

sum=0

for i in range(len(alst)-1):
  sum+=alst[i]*(s[len(s)-1]-s[i+1])

print(sum%MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10**9+7

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

s = sum(a)%mod
ans = 0

for i in a:
    s -=i
    s %= mod
    ans += s*i
    ans %=mod
ans %=mod
print(ans)

Submission status: Accepted

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

mo=10**9+7
an=0
wk=0
ru=[]
for i in a:
    wk+= i
    ru.append(wk%mo)
for i in range(n-1):
    an+=a[i]*(ru[n-1]-ru[i])
    an%=mo

print(an)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

n=int(input()) #3
alst=list(map(int,input().split())) #[1,2,3]

# 累積和を求める

# 例
# [1,2,3,4,5,6]の時、累積和は、
# [0,0+1,0+1+2,0+1+2+3,0+1+2+3+4,0+1+2+3+4+5,0+1+2+3+4+5+6]

s=[0]
for i in range(len(alst)):
  s.append(s[i]+alst[i])

sum=0

for i in range(len(alst)-1):
  sum+=alst[i]*(s[len(s)-1]-s[i+1])

print(sum%MOD)

Submission status: Accepted

Solution code:

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

MOD = 10 ** 9 + 7

sum = sum(a) % MOD
ans = 0

for i in range(len(a)):
    sum -= a[i]
    sum %= MOD
    ans += a[i] * sum
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
MOD = int(1e9+7)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    sm = 0
    for a in A:
        sm = (sm + a) % MOD
    ans = 0
    for a in A:
        ans += ((sm-a) % MOD)*a
        ans %= MOD
    if ans % 2 == 0:
        ans //= 2
    else:
        ans = (ans + MOD) // 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
MOD = 1000000007
N = int(input())
A = list(map(int, input().split()))
ans = 0
B = [0]*(N-1)
sumA = sum(A)
for i in range(1, N):
    sumA -= A[i-1]
    B[i-1] = sumA
for i in range(0, N-1):
    ans += A[i] * B[i]
    ans %= MOD
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

s=0
S=[]
for a in A:
    s += a
    s %= MOD 
    S.append(s)    

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

n = readint()
a = readints()

mod = 10**9+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())
a = [int(i) for i in input().split()]

res = sum(i for i in a)

ans = 0
for i in a:
    res -= i
    ans += i * res

print(ans % 1000000007)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
def Ii():return int(sys.stdin.readline().rstrip())
def Mi():return map(int,sys.stdin.readline().split())
def Li():return list(map(int,sys.stdin.readline().split()))
n = Ii()
a = Li()
sa = 0
ans = 0
mod = 10**9+7
for i in range(n-1):
  sa += a[i]
  ans += sa*a[i+1]
  ans %= mod
  
print(ans)

Submission status: Accepted

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

#累積和を計算
SumA = [0]*(N+1)
n = 1
for i in range(N):
	SumA[i+1] = SumA[i] + A[i]

#A*Aの全パターンを計算
AA = []
for j in range(N):
	AA.append(A[j]*(SumA[N]-SumA[j+1]))

#合計を計算
ans = sum(AA)%1000000007
print(ans)

Submission status: Accepted

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

s=sum(A)

ans=0
for a in A:
    ans+=a*(s-a)
print((ans//2)%mod)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split(" ")]

from itertools import combinations

mod = 1000000007  # 丁度いい大きさの素数だけで意味は特にない。

s = 0
ans = 0
for n in A:
    s += n
    s %= mod

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = 0
total = sum(A[1:])

for i in range(n-1):
    ans += A[i]*total
    total -= A[i+1]
    ans %= 1000000007

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


# 整数の入力
n = int(input())
# スペース区切りの整数の入力
zzz = list(map(int, input().split()))
b=sum(zzz)**2
a=0
for i in range(n):
  # print(i)
  x=zzz[i]**2
  # print(x)
  a +=x
# print(b)
# for a in zzz:
#   print(a)

# print(b)
# print(a)

ans = (b-a)//2%(10**9+7)
print(ans)

Submission status: Accepted

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

mod = 1000000007

ans = 0
tmp1, tmp2 = 0, 0

for m in A:
  tmp1 = (tmp1+m) %mod
  tmp2 = (tmp2+m*m%mod)%mod
  
ans = ((tmp1*tmp1)%mod-tmp2)%mod

if ans%2 ==0:
  ans = ans/2
else:
  ans =(ans+mod)/2

print(int(ans))

Submission status: Accepted

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

s = sum(a)
ans = 0

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

print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    
    MOD = 10 ** 9 + 7
    
    sum_a = sum(A)
    sum_squares = sum([a * a for a in A])
    
    ans = (sum_a ** 2 - sum_squares) // 2
    ans %= MOD

    print(ans)
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    ans = 0
    s = list(accumulate(a[::-1]))[::-1][1:]
    for i in range(n-1):
        ans += a[i]*s[i]%mod
        ans %= mod
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
'''
    Auther: ghoshashis545 Ashis Ghosh
    College: jalpaiguri Govt Enggineerin College

'''
import sys
from collections import deque,defaultdict as dd 
from bisect import bisect,bisect_left,bisect_right,insort,insort_left,insort_right
from itertools import permutations
from datetime import datetime
from math import ceil,sqrt,log,gcd
def ii():return int(input())
def si():return input()
def mi():return map(int,input().split())
def li():return list(mi())
abc='abcdefghijklmnopqrstuvwxyz'
abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod=1000000007
#mod=998244353
inf = float("inf")
vow=['a','e','i','o','u']
dx,dy=[-1,1,0,0],[0,0,1,-1]



def solve():
    
    
    
    # for _ in range(ii()):
    
    
    
    n = ii()
    a = li()
    suff = [0]*n
    for i in range(n-2,-1,-1):
        suff[i] = suff[i+1]
        suff[i] += a[i+1]
        suff[i] %= mod
    
    ans = 0
    for i in range(n):
        x = a[i]*suff[i]
        x %= mod
        ans += x
        ans %= mod
    print(ans)
    
        
    
    
if __name__ =="__main__":
    # read()
    solve()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = []
su = 0

la = len(a)
ele = a[la - 1]

for i in range(1, len(a) - 1):
    ele = ele + a[la - i - 1]
    b.append(ele)

lb = len(b)

for j in range(len(a) - 2):
    su = su + a[j] * b[lb - j - 1]

su = su + a[len(a) - 1] * a[len(a) - 2]

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

Submission status: Accepted

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

ans = sum(a)**2
for i in range(n):
    ans -= a[i]*a[i]
ans //= 2
ans %= MOD
print(ans)

Submission status: Accepted

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

su = sum(a)
sus = 0

for i in a:
    su = su - i
    sus = sus + i * su

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

Submission status: Accepted

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

for i in range(N):
  ak += A[i]
  ak_2 += A[i] ** 2
s = int(((ak ** 2) - ak_2) // 2)
print(s % mod)

Submission status: Accepted

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

tot = 0
s_tot=0

def modpow( a , n , m):
    ans=1
    n_bit = bin(n)[2:]
    l=len(n_bit)
    for i in list(range(l))[::-1]:
        if n_bit[i]=="1":
            ans = (ans * a) % m
        #print(i , n_bit[i] , a , ans)
        a=(a*a) %m
        #print(a,ans)
    return ans
inv_2 = modpow( 2 , M-2 , M)
for i in range(n):
    y=X[i]
    
    tot = (tot + y) % M
    s_tot = (s_tot + y**2%M ) % M

ans= ((tot ** 2 - s_tot )* inv_2 ) % M
print(ans)

Submission status: Accepted

Solution code:
MOD = int(1e9+7)


def main():
    n = int(input())    
    A = list(map(int,input().split()))
    sum_a = 0
    for a in A:
        sum_a = (sum_a + a) % MOD
        
    ans = 0
    for a in A:
        sum_a = (sum_a - a) % MOD
        ans = (ans + ((a*sum_a) % MOD)) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

# 累積和ベクトルbを作成
a.reverse()
b = [0]*n
b[0] = a[0]
for i in range(1, n):
    b[i] = b[i-1]+a[i]
a.reverse()
b.reverse()

ans = 0
for i in range(n-1):
    ans += a[i]*b[i+1]

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

Submission status: Accepted

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

A_sum = sum(A)

value = 0
ans = 0

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

ans = value % (1*10**9+7)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


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

    s = sum(A)
    ans = 0
    mod = 10**9 + 7
    for i in A:
        ans = (ans + i * (s - i))
    print((ans // 2) % mod)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

def solve(N, A):
    MOD = 10**9 + 7
    S = [0] * N
    for i in range(N-1)[::-1]:
        S[i] = (S[i+1]+A[i+1]) % MOD

    # N : 2 * 10 ** 5
    # A[i] * A[j] foreach i, j : i < j
    answer = 0
    for i in range(N):
        #print(A[i], S[i])
        answer += (A[i] * S[i]) % MOD

    return answer % MOD

print(solve(N, A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
B = [0] * (N+1)  # 累積和テーブル B[i] = sum(A[:i])
mod = 10**9 + 7
ans = 0

for i in range(N):
    B[i+1] = B[i] + A[i]  # 累積和

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

print(int(ans))

Submission status: Accepted

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

_sum = 0
x = 0
for i in range(len(As) - 1):
    _sum += As[i+1] * (x + As[i])
    x += As[i]

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

Submission status: Accepted

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

SU = [0] * (len(A)+1)

for i in range(N):
    SU[i+1] = SU[i] + A[i]

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

print(ans%MOD)

Submission status: Accepted

Solution code:
N = int(input())

ai_char = map(str, input().split(" "))
Ai = []
for i in ai_char:
    Ai.append(int(i))

mod_val = 1000000007
N1 = N-1
sum = 0

sum_tmp = 0
for j in range(0, N):
    sum_tmp += Ai[j]

for i in range(N1):
    sum_tmp -= Ai[i]
    sum += Ai[i] * sum_tmp

    if sum > mod_val:
        sum = sum % mod_val


print(int(sum))

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(a[1:])
ans = 0

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

Submission status: Accepted

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

# C - Sum of product of pairs
N = int(input())
A = list(map(int, input().split()))

sum_ = sum(A)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

# 累積和
a_acm = list(accumulate(a[::-1]))

ans = 0
mod = 10 ** 9 + 7
for i in range(n-1):
    ans += a[i] * a_acm[n-2-i]
    ans %= mod
print(ans)

Submission status: Accepted

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

sum = 0
for i in range(0,n):
    sum += li[i]
    sum %= mod

ans = 0
for i in range(0,n):
    sum -= li[i]
    if sum < 0:
        sum += mod
    ans += li[i] * sum
    ans %= mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

MOD = 10**9 + 7

sum_a = sum(A)
sum_squares = sum([a*a for a in A])

ans = (sum_a**2 - sum_squares)//2
ans %= MOD

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
c = list(map(int, input().rstrip().split()))
ans=0
b=10**9+7
sum1=0
for i in range(n):
    ans=(ans+c[i]*sum1)%b
    sum1=(sum1+c[i])%b

    
    
    
        
print(ans)

Submission status: Accepted

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

ans= 0
total = sum(s)

for i in range(len(s)-1):
  total -= s[i]
  ans += total * s[i]

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

# B_C = sorted(B_C, reverse=True, key=lambda x:x[1])
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

sum_A = sum(A)
A = np.array(A)

cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

# def factorization(n):
#     arr = []
#     temp = n
#     for i in range(2, int(-(-n**0.5//1))+1):
#         if temp%i==0:
#             cnt=0
#             while temp%i==0:
#                 cnt+=1
#                 temp //= i
#             arr.append([i, cnt])
#     if temp!=1:
#         arr.append([temp, 1])
#     if arr==[]:
#         arr.append([n, 1])
#     return arr



tot = 0
for i in range(N):
    tot += int(A[i]) * (sum_A - int(cum_A[i]))
        
print(tot % 1000000007)

Submission status: Accepted

Solution code:
#標準入力
N = int(input())
A = list(map(int, input().split()))

#累積和
S = [0]*N
for n in range(N):
  S[n] = S[n-1] + A[n]

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

#modulo
_sum = _sum % (1000000000+7)
  
print(_sum)