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:
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
import copy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


def main():
    n = I()
    a = list(LI())
    res = [0]*len(a)
    res[0] = a[n-1]
    ans = 0
    for i in range(len(a)-1):
        res[i+1] = res[i] + a[n-i-2]
    for i in range(len(a)-1):
        ans += a[i] * res[n-i-2]
    print(ans % mod)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
A = [int(i) for i in input().split()]
arrsum = 0
indSum = 0
for i in A:
    arrsum += i
    indSum += i*i
arrsum = arrsum ** 2
print(((arrsum-indSum)//2)%1000000007)

Submission status: Accepted

Solution code:
N =int(input())
A = list(map(int,input().split()))
suma = sum(A)
Ans = 0
for a in A:
    suma -= a
    Ans += suma*a
    Ans = Ans % (10**9+7)
print(Ans)

Submission status: Accepted

Solution code:
from collections import deque

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

Submission status: Accepted

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

total=sum(a)

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

print(wa%(1000000000+7))

Submission status: Accepted

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

mod = 10**9+7
s = 0

for i in a:
    s += i
    s %= mod

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

s = s*(5*(10**8)+4)

print(s%mod)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
MOD = 10 ** 9 + 7
S = 0
sq = 0

for a in A:
    S = (a + S) % (2*MOD)
    sq = (a ** 2 + sq) % (2*MOD)

ans = (S ** 2 - sq) % (2*MOD)

print(ans//2%MOD)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import bisect
from sys import stdin,stdout
from math import gcd,floor,sqrt,log
from collections import defaultdict as dd
from bisect import bisect_left as bl,bisect_right as br

sys.setrecursionlimit(100000000)

ii     =lambda: int(input())
si     =lambda: input()
jn     =lambda x,l: x.join(map(str,l))
sl     =lambda: list(map(str,input().strip()))
mi     =lambda: map(int,input().split())
mif    =lambda: map(float,input().split())
lii    =lambda: list(map(int,input().split()))

ceil   =lambda x: int(x) if(x==int(x)) else int(x)+1
ceildiv=lambda x,d: x//d if(x%d==0) else x//d+1

flush  =lambda: stdout.flush()
stdstr =lambda: stdin.readline()
stdint =lambda: int(stdin.readline())
stdpr  =lambda x: stdout.write(str(x))

mod=1000000007


#main code
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(0, n, 1): 
        individual_square_sum += A[i] * A[i] 
    return (array_sum_square - 
            individual_square_sum)//2
n=ii()
arr=lii()
print(findProductSum(arr,n)%mod)

Submission status: Accepted

Solution code:
def findProductSum(A, n): 
    array_sum = sum(A)
    array_sum_square = array_sum ** 2
    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 = input()
arr = list(map(int, input().split()))
res = findProductSum(arr, len(arr)) % (10**9 + 7)
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(n-1):
    a_sum.append((a_sum[i]+a[i+1])%1000000007)

    ans += a_sum[i]*(a[i+1]%1000000007)

print(ans%1000000007)

Submission status: Accepted

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

da = [a[n-1] for i in range(n)]
for i in range(n-2, -1, -1):
    da[i] = da[i+1] + a[i]
sum = 0
for i in range(n-1):
    # print (str(a[i]) + " : " + str(da[i+1]))
    sum += a[i] * da[i+1]
    sum %= 1000000007
# print(da)
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def solve():
    n = int(input())
    As = list(map(int, input().split()))

    mod = 10**9+7
    sums = [0] * (n+1)

    tmp = 0
    for i, a in enumerate(As[::-1]):
        tmp += a
        tmp %= mod
        sums[-i-1] = tmp

    total = 0
    for i, a in enumerate(As[:-1]):
        total += a * (sums[i+2])
        total %= mod

    return total


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(C)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    Acum = list(accumulate(A))
    ans = 0
    for i in range(N-1):
        ans += A[i] * (Acum[N-1] - Acum[i]) %MOD
        ans %= MOD
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
ans=0

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
key = 1000000007
sum = 0
sum2 = 0
for i in range(N):
    sum += A[i]
    #if sum >= key:
     #   sum %= key

for i in range(N):
    sum2 += A[i]**2
    #if sum2 >= key:
     #   sum2 %= key
        
ans = (sum*sum - sum2)//2%key
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum1 = 0
sum2 = 0
u = 0
for i in range(n):
    u = a[i]
    sum1 = (sum1+u)%(10**9+7)
    sum2 = (sum2+(u**2))%(10**9+7)

sum1 = sum1**2 % (10**9+7)
ans = (10**9+8)//2 * (sum1-sum2) % (10**9+7)
print(ans)

Submission status: Accepted

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

N = int(readline().strip())
A = list(map(int, readline().split()))
L = 10**9 + 7

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

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint


def ceil(a, b): return (a + b - 1) // b


inf = float('inf')
mod = 10 ** 9 + 7


def pprint(*A):
    for a in A:     print(*a, sep='\n')


def INT_(n): return int(n) - 1


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


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [I() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    N = I()
    A = LI()
    ans = 0
    tl = 0
    for a in A:
        tl += a
    for i in range(N):
        tl -= A[i]
        ans += (A[i] * tl) % mod
    ans %= mod
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

sumA=As[-1]
MOD=10**9+7
ans=0
for n in range(N-1):
    Ai=As[N-2-n]
    ans+=sumA*Ai
    ans%=MOD
    sumA+=Ai
    sumA%=MOD
print(ans)

Submission status: Accepted

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

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    As = list(map(int, input().split()))
    ans = 0
    kei = 0
    for i in range(len(As)-1):
        kei += As[-1-i]
        ans += kei*As[-2-i]



    return ans % (10**9 +7)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans = (mod*mod)%(10**9+7)
for i in range(N):
    ans -= A[i]*A[i]
    ans = ans%(10**9+7)
ans = ans*pow(2, -1, 10**9+7)
ans = ans %(10**9+7)
print(ans)

Submission status: Accepted

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

#互いに素なa,bについて、a*x+b*y=1の一つの解
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

# aの逆元(mod m)を求める。(aとmは互いに素であることが前提)
def mod_inv(a,m):
    x = extgcd(a,m)[0]
    return (m+x%m)%m

mod = 10**9 + 7
total = 0
for a in A:
    total += a
    total %= mod

sq = 0
for a in A:
    sq += a**2
    sq %= mod

inv = mod_inv(2, mod)

ans = (total**2 - sq) * inv
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys
N=int(sys.stdin.readline().strip())
A=map(int, sys.stdin.readline().split())
mod=10**9+7
RUISEKI=[0]
for a in A:
    RUISEKI.append((RUISEKI[-1]+a)%mod)

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

Submission status: Accepted

Solution code:
from sys import stdin,stdout
from collections import defaultdict
import math
from bisect import bisect
#input=stdin.readline
#def print(x):stdout.write(str(x)+'\n')
n=int(input())
arr=list(map(int,input().split()))
result = 0
su=sum(arr)
s=[]
s1=0
for i in range(n):
    s1+=arr[i]
    s.append(su-s1)

for i in range(0, n): 
    result+=(arr[i]*s[i])
print((result)%(10**9+7))

Submission status: Accepted

Solution code:

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 標準入力を取得
N = int(input())
A = list(map(int, input().split()))

# 求解処理
ans = 0
sum_A_j = sum(A)
for A_i in A:
    sum_A_j -= A_i
    ans += A_i * sum_A_j
    ans %= 10**9 + 7

# 結果出力
print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
arr = raw_input()  
v = list(map(int,arr.split(' ')))
su=0
mod=1000000007
for i in v:
    su+=i
su=su*su
x=0
for i in v:
    x+=i*i
ans=(su-x)/2
ans=(ans + mod)%mod
print ans

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    arr = list(map(int, input().split()))

    pref = []

    dup = arr.copy()
    dup.reverse()

    pref.append(dup[0]);
    
    for i in range(1,n):
       pref.append(pref[i-1]+dup[i]);
    
    pref.reverse()

    ans = 0

    for i in range(n-1):
        ans += arr[i] * pref[i+1];

    ans %= 1000000007;

    print(ans)


if __name__ == '__main__':
    main();

Submission status: Accepted

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

# from typing import Optional
# from typing import List
# from typing import Dict
# from typing import Tuple
# from typing import Sequence


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

    sum = 0
    mod = 1000000000 + 7

    sumJ = 0
    for j in range(N):
        sumJ += A[j]

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

    print(sum)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
tmp_A = []
tmp_A.append(A[N-1])
for i in range(N-2):
    A0 = tmp_A[i]+A[N-2-i]
    tmp_A.append(A0)
tmp_A.reverse()
sum_A = 0
for i in range(N-1):
    sum_A += A[i]*tmp_A[i]
print(sum_A%(1000000007))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def read():
    N = int(input())
    As = list(map(int, input().split(' ')))
    return N, As


def func(N, As):
    ans = 0
    s = As[-1]
    for i in range(N - 1):
        t = As[-1 * (i+2)]
        ans += s * t
        s += t
    return ans % (10 ** 9 + 7)

if __name__ == '__main__':
    N, As = read()
    print(func(N, As))

Submission status: Accepted

Solution code:
import itertools   # accumulate, compress, permutations(nPr), combinations(nCr)
# import bisect      # bisect_left(insert位置), bisect_right(slice用)
# import math        # factorical（階乗) # hypot(距離)
# import heapq
# from fractions import gcd # >=Python3.5 # lcm（最小公倍数） = (a*b)//gcd(a,b)
# from fractions import Fraction
# from math import gcd # <Python3.5
# --------------------------------------------------------------

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

lis = list(itertools.accumulate(a))

total = 0

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

print(total % MOD)

Submission status: Accepted

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

tmp = [0]*N
tmp2 = [0]*N
tmp2[0] = A[0]

for i in range(1,N):
    tmp2[i] = A[i] + tmp2[i-1]
sum_A = sum(A)

for i in range(N):
    tmp[i] = sum_A - tmp2[i]

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
left =0
right = 0
tmp = 0
for i in range(0,N-1):
    left = A[N-2-i]
    right = right + A[N-1-i]
    tmp += left * right

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

a = list(map(int, input().split()))
a = sorted(a, reverse=True)

sum = 0
mult = 0

for i in a:
  mult += (sum * i) 
  sum += i

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate

MOD = 10 ** 9 + 7

n = int(input())

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

suffix_sum = list(accumulate(a[::-1]))[::-1]

req_arr = [x -  y for x, y in zip(suffix_sum, a)]

result = sum([(x * y) % MOD for x, y in zip(req_arr, a)])

print(result % MOD)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
from decimal import *
import heapq
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
a = LIST()
b = [0 for _ in range(n)]
b[0] = a[0]
for i in range(1, n):
    b[i] = b[i - 1] + a[i]
    b[i] %= mod
ans = 0
for i in range(n - 1):
    ans += a[i] * (b[-1] - b[i])
    ans %= mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum_ = sum(a_list)
result = 0
for a in a_list:
  sum_ -= a
  result += ((sum_ % mod) * a) % mod
  result = result % mod

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


def resolve():
    # s = input()
    n = int(input())
    a = list(map(int, input().split()))

    sum_a = sum(a)
    ans = 0

    for aa in a:
        sum_a -= aa
        ans += aa * sum_a

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


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
1 2 3"""
        output = """11"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """4
141421356 17320508 22360679 244949"""
        output = """437235829"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

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

sum = 0
multiplicand=A[0]
for a in A[1:]:
  sum+=multiplicand*a
  multiplicand+=a
print(sum%((10)**9+7))

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest
import copy

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    #n,m,k=map(int, readline().rstrip().split())
    mod=10**9+7
    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    arr2=copy.copy(arr)
    for i in range(1,n):
        arr2[i]=(arr2[i]+arr2[i-1])%mod
    base=arr2[n-1]+mod
    ans=0
    for i in range(n-1):
        ans=(ans+arr[i]*((base-arr2[i])%mod))%mod
    print(ans)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

Solution code:
from itertools import accumulate

N = int(input())
A = list([int(x) for x in input().split()])
check = list(accumulate(A))

MOD = 10**9 + 7

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

print(result)

Submission status: Accepted

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

s1=0
for i in range(0,n):
    s1+=a[i]

s3=s1*s1

s2=0
for i in range(0,n):
    s2+=(a[i]*a[i])

print (((s3-s2)//2)%mod)

Submission status: Accepted

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

MOD = (10**9)+7
a_sum = 0
ans = 0
for i in range(n-1):
    a_sum += a_list[n-i-1]
    ans += a_sum * a_list[n-i-2]
    ans = ans % MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

L = sorted(L,reverse=True)

#print(L)
t = 0
a = 0
for i in range(1,n):
    t += L[i-1]
    a += (t * L[i]) #% (10**9 + 7)
#    print(t,a)

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

Submission status: Accepted

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

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

print(ans%1000000007)

Submission status: Accepted

Solution code:
n = int(input())
array = [int(x) for x in input().split(' ')]
s = 0
sums = []
for i in range(n-1, -1, -1):
    if i == n-1:
        sums.append(array[i])
    else:
        sums.append(sums[-1] + array[i])

sums.pop()
for i in range(n-1):
    s += array[i] * sums.pop()

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

Submission status: Accepted

Solution code:
MOD = 10**9 +7

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

ans = 0
sumA = 0
sumA_2 = 0
for i in A:
    sumA = (sumA + i) % MOD
    sumA_2 = (sumA_2 + i**2 ) % MOD

ans = (sumA * sumA - sumA_2) % MOD

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

input = sys.stdin.readline
mod = 1000000007


def ST():
    return input().rstrip()


def I():
    return int(input())


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


def LI():
    return list(MI())


N = I()
A = LI()

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

print(ans)

Submission status: Accepted

Solution code:
from math import *
from collections import *
from itertools import *
from functools import *
from bisect import *
from heapq import *
from operator import *
from sys import *
setrecursionlimit(100000000)
MAX=float('inf')
MIN=float('-inf')
MOD=1000000007

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

Submission status: Accepted

Solution code:

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


b = [None]*n
b[0] = a[0]

for i in range(n-1):
    b[i+1] = (b[i] + a[i+1]) % 1000000007;

con = 0;

for v in range(1,n):
    x = (a[-v] * b[-v-1]) % 1000000007;
    con = (con + x) % 1000000007;

print(con)

Submission status: Accepted

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

Submission status: Accepted

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

ans=0
wa=0

for i in range(n-2,-1,-1):
    wa+=list[i+1]
    ans+=list[i]*wa

print(ans%1000000007)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_list = list(map(int, input().split()))

    sum_list = [0 for _ in range(n)]
    sum_list[0] = sum(a_list)
    ans = 0

    for i in range(1, n):
        sum_list[i] = sum_list[i - 1] - a_list[i - 1]

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():

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

    mod = pow(10, 9) + 7
    k1, k2 = 0, 0
    l1, l2 = 0, 0
    for i in range(N):
        k1 += A[i] // mod
        k2 += A[i] % mod
        l1 += (l2 + A[i] * A[i]) // mod
        l2 = (l2 + A[i] * A[i]) % mod
    # print(k1, k2, l1, l2)

    u = (k2*k2 - l2)
    v = (k1 * k1 - l1) * mod
    return ((u + v) // 2) % mod

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Tail = a[N-1]
R = 0
m = 7+10**9
for i in range(N-2,-1,-1):

    R = (R+a[i]*Tail)%m
    Tail +=a[i]
    
print(R)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

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

import numpy as np
r=0
s=sum(a)-a[0]
m=7+10**9
for ii in range(n-1):
  r+=(a[ii]*s)%m
  s-=a[ii+1]

#print(m)
print(r%m)

Submission status: Accepted

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

Submission status: Accepted

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

    ans = 0
    Asum = sum(A)%MOD
    for a in A:
        Asum-=a
        ans += a*Asum
        ans %=MOD
    print(ans)
main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N = I()
A = LI()

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

ans *= pow(2,mod-2,mod)
ans %= mod
print(ans)

Submission status: Accepted

Solution code:
mod=1000000007
n=int(input())
#n,k=map(int,input().split())
a=[int(i) for i in input().split()]
t=[0]*(n+1)
for i in range(n):
    t[i]=t[i-1]+a[i]
ans=chk=0
#print(t)
for i in range(n-1):
#    print(-(i+1))
    x=-1*(i+1)
#    print(x)
#    print(a[x],t[x-2])
    ans+=a[x]*t[x-2]
    ans%=mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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