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_list = input().split()

a_list_reverse = a_list[::-1]
# print(a_list_reverse)

rusekiwa = [0] * (len(a_list_reverse) - 1)
sum_v = 0
for i in range(len(a_list_reverse) - 1):
    sum_v += int(a_list_reverse[i])
    rusekiwa[i] = sum_v
# print(rusekiwa)

ans = 0
for i in range(len(a_list) - 1):
    v = int(a_list[i]) * rusekiwa[-1 - (i)]
    # print(v)
    # v = v % (10 ** 9 + 7)
    ans += v
    ans = ans % (10 ** 9 + 7)

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

Submission status: Accepted

Solution code:
Q = 10**9+7
def main():
    N = int( input())
    A = list( map( int, input().split()))
    S = sum(A)%Q
    T = sum( [a*a for a in A])%Q
    print((S*S%Q-T)*pow(2,Q-2,Q)%Q)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split()))
sums = 0
moder = 1000000007
s=[0 for i in range(N-1)]
s[-1] = As[-1]
for i in range(N-2):
  s[-(i+2)] = s[-(i+1)]+As[-(i+2)]

for i in range(N-1):
  sums+= (As[i]*s[i])%moder
print(sums%moder)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    _ = int(input())
    A = [int(x) for x in input().split()]
    mod = 10 ** 9 + 7

    square_S = sum(A) ** 2 % mod
    diagonal = sum([x ** 2 % mod for x in A]) % mod
    #  By the Fermat's little theorem
    print(((square_S - diagonal) % mod * pow(2, mod - 2, mod)) % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A = tuple(map(int,input().split(" ")))
ans = 0

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

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

Submission status: Accepted

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

MOD=7+10**9
SUM=sum(A)
ans=0

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
MOD = 1000000007
SUM = sum(A) % MOD
ans = 0

for n in A:
    SUM -= n
    SUM %= MOD
    ans += SUM * n
    ans %= MOD

print(ans)

Submission status: Accepted

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

S = sum(A)
S_2 = sum(map(lambda x: x*x, A))

ans = (S*S - S_2)//2 #切り捨て除算にしないと結果をfloatで返してきやがるので

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

Submission status: Accepted

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

Submission status: Accepted

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

answer = 0
a_sum = sum(A)
mod = 10**9+7

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

Submission status: Accepted

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

ans = 0
for i, a in enumerate(reversed(A[1:])):
    ans += Acum[n-i-2]*a
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = []
    sum = 0             #longの気持ち
    ans = 0             #long long の気持ち
    mod = 1_000_000_000 + 7
    x = list(map(int, input().split()))
    for i in range(n):
        sum += x[i]           
        a.append(x[i])
    
    sum % mod
    
    for i in range(n):
        sum -= a[i]
        if sum < 0:
            sum += mod
        ans += sum * a[i]
        ans %= mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

edge = sum(x)
print((edge**2 - sum([X*X for X in x]))//2 % (10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
n = int(input())

a_sum = 0
sq_sum = 0
for a in map(int, input().split()):
  a_sum = (a_sum + a) % mod
  sq_sum = (sq_sum + a * a) % mod
res = (a_sum * a_sum - sq_sum) % mod
print(-(mod // 2) % mod * res % mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

mod = int(1e9+7)
suffix_sum = sum(a)
ans = 0

for ai in a:
  suffix_sum -= ai
  ans += ai*suffix_sum
  ans %= mod

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
l=list(map(lambda z:z**2,a))
x=sum(a)**2
y=sum(l)
t=(x-y)//2
if t>(10**9+7):
       print(t%(10**9+7))
else:
       print(t)

Submission status: Accepted

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

from itertools import accumulate
c=list(accumulate(a))
mod=10**9+7
ans=0
for i in range(n-1):
    ans+=a[i]*(c[n-1]-c[i])
    ans%=mod
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

sum = [0 for i in range(n+1)]
for i in range(n - 1, -1, -1):
    sum[i] = sum[i + 1] + a[i]
ans = 0
for i in range(n):
    ans = (ans+(a[i]*sum[i+1])) % 1000000007
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    arr = list(map(int, input().split()))
    mod = 10 ** 9 + 7
    sq = sum(num*num for num in arr)
    S = sum(arr)
    print(((S*S-sq)//2)%mod)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = []
    sum = 0             #longの気持ち
    ans = 0             #long long の気持ち
    mod = 1_000_000_000 + 7
    x = list(map(int, input().split()))
    for i in range(n):
        sum += x[i]           
        a.append(x[i])
    
    for i in range(n):
        sum -= a[i]
        ans += sum * a[i]
        ans %= mod
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans=0
total=0
t=sum(x)

for i in range(n):
 
  t=t-x[i]
  total=total+x[i]*(t)
  
   
    
print(total %1000000007)

Submission status: Accepted

Solution code:
# (a+b+c) mod n = (((a mod n)+b mod n )+c) mod n
from itertools import accumulate

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

    acc = list(accumulate(A))
    mod = 10**9 + 7

    ans = 0

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
b = [a[0]]
ans = 0
mod = 1000000007

for i in range(1,n):
  b.append(b[-1]+a[i])

for i in range(1,n):
  ans += a[n-i]*b[n-i-1]
  ans = 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 i in a[:n - 1]:
    s -= i
    ans += s * i
    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 = sum(a) ** 2
t = sum(ai ** 2 for ai in a)
ans = (s - t) * pow(2, MOD - 2, MOD)
ans %= MOD
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
result = 0
sum = arr[0]
for i in range(1,len(arr)):
    result += (sum*arr[i])
    sum+=arr[i]
print(result%1000000007)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

"""
allsum = 0
for i in range(n-1):
    allsum += a[i] * sum(a[i+1:])
print(allsum % (10**9+7))
"""
s, s2 = 0, 0
for i in range(n):
    s += a[i]
    s2 += a[i] * a[i]
print(((s*s - s2)//2) % (10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

a = list(accumulate(lis))

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

print(ans)

Submission status: Accepted

Solution code:
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

Submission status: Accepted

Solution code:
# C　Sum of product of pairs
N = int(input())
A = list(map(int, input().split()))
t = sum(A)
x = t - A[0]
mod = 10 ** 9 + 7
ct = 0
for i in range(N-1):
    ct += A[i] * x
    x = x - A[i+1]

print((ct % mod))
int((ct % mod))

Submission status: Accepted

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

Submission status: Accepted

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

suma = sum(A_list)

res = 0
for a in A_list:
    suma -= a
    res += suma * a

print(res%MOD)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
a_list = [int(i) for i in input().split(' ')]
mod = 1000000007
 
sum = 0
for i in a_list:
  sum += i
 
output_sum = 0
for idx, i in enumerate(a_list[:-1]):
  sum -= a_list[idx]
  output_sum += i*sum
  
print(output_sum%mod)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
al=sum(a)
ans=0
mod=10**9+7

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

Submission status: Accepted

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

area = sum(l) * sum(l)

for i in l:
    area -= i*i

ans = area//2
ans = ans % mod
print(ans)

Submission status: Accepted

Solution code:
mod = 10**9 + 7
n = int(input())
a = list(map(int,input().split()))
prefix = list()
sum = 0
for i in a:
	sum += i
	prefix.append(sum)

res = 0
for i in range(0,n):
	t = (a[i] * (prefix[n - 1] - prefix[i])) % mod
	res = (t + res) % mod

print(res)

Submission status: Accepted

Solution code:
N = input()
a_list = [int(i) for i in input().split(' ')]
mod = 1000000007

sum_list = []
sum = 0
for i in a_list:
  sum += i
  sum_list.append(sum)

sum = 0
for idx, i in enumerate(a_list[:-1]):
  sum += i*(sum_list[-1]-sum_list[idx])
  
print(sum%mod)

Submission status: Accepted

Solution code:

MOD = 10 ** 9 + 7

N = int(input())

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

# import random
# N = 200_000
# A = [random.randint(1, 2000) for i in range(N)]

res = 0

cum_sum = sum(A)

for i in range(N - 1):
    cum_sum -= A[i]
    res += (A[i] * cum_sum) % MOD

res %= MOD

print(res)

Submission status: Accepted

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

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 += A[i]
    s %= mod

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
res = 0
mod = int(1e9 + 7)
for i in arr:
  s -= i
  res += i * s
  res %= mod
print(res)

Submission status: Accepted

Solution code:
n=int(input())

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

Submission status: Accepted

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


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


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


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

Submission status: Accepted

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

res = 0
s = 0
for i in range(1, len(a)):
    s = (s+a[i-1])%mod
    res = (res+(s*a[i])%mod)%mod

print(res)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

area = sum(l) * sum(l)

for i in l:
    area -= i*i

ans = area//2
ans = ans % mod
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array([int(i) for i in input().split()])
sum = 0

#二乗和だけは1項ずつ求めてmodをとる
#for x in A:
#  sum += x
#  sum %= (10**9+7)
sum = A.sum() % (10**9+7)

sum2 = 0
for x in A:
  sum2 += x*x
  sum2 %= (10**9+7)
#sum2 = (A**2).sum() % (10**9+7)

ans = (sum**2 - sum2) % (10**9+7)
if ans < 0:
  ans += (10**9+7)
ans *= int((10**9+7+1)/2) 
ans %= (10**9+7)
print(int(ans))

Submission status: Accepted

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

MOD = 10**9 + 7

S1, S2 = 0, 0
for a in A:
    S1 += a
    S2 += a * a % MOD
    S1 %= MOD
    S2 %= MOD

print(((S1*S1%MOD-S2+MOD) % MOD) * pow(2,MOD-2,MOD) % MOD)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array([int(i) for i in input().split()])
sum = 0

#累積和、二乗和の両方で1項ずつ求めてmodをとる
for x in A:
  sum += x
  sum %= (10**9+7)
#sum = A.sum() % (10**9+7)

sum2 = 0
for x in A:
  sum2 += x*x
  sum2 %= (10**9+7)
#sum2 = (A**2).sum() % (10**9+7)

ans = (sum**2 - sum2) % (10**9+7)
if ans < 0:
  ans += (10**9+7)
ans *= int((10**9+7+1)/2) 
ans %= (10**9+7)
print(int(ans))

Submission status: Accepted

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

S = sum(li)
li2 = [x**2 for x in li]
S2 = sum(li2)

print((S**2-S2)//2%mod)

Submission status: Accepted

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

Submission status: Accepted

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

sq_sum = sum(map(lambda x: x*x, A))
sum_sq = sum(A) * sum(A)
answer = (sum_sq - sq_sum) // 2

print(answer % mod)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array([int(i) for i in input().split()])
sum = 0
for x in A:
  sum += x
  sum %= (10**9+7)
sum = A.sum() % (10**9+7)

sum2 = 0
for x in A:
  sum2 += x*x
  sum2 %= (10**9+7)

ans = (sum**2 - sum2) % (10**9+7)
if ans < 0:
  ans += (10**9+7)
ans *= int((10**9+7+1)/2) 
ans %= (10**9+7)
print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array([int(i) for i in input().split()])
sum = 0
for x in A:
  sum += x
  sum %= (10**9+7)

sum2 = 0
for x in A:
  sum2 += x*x
  sum2 %= (10**9+7)

ans = (sum**2 - sum2) % (10**9+7)
if ans < 0:
  ans += (10**9+7)
ans *= int((10**9+7+1)/2) 
ans %= (10**9+7)
print(int(ans))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = [int(i) for i in input().split()]
sum = 0
for x in A:
  sum += x
  sum %= (10**9+7)

sum2 = 0
for x in A:
  sum2 += x*x
  sum2 %= (10**9+7)

ans = (sum**2 - sum2) % (10**9+7)
if ans < 0:
  ans += (10**9+7)
ans *= int((10**9+7+1)/2) 
ans %= (10**9+7)
print(int(ans))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    c = [0]*(N)
    c[0] = A[0]%(1000000007)
    for i in range(1,N,1):
        c[i] = c[i-1]+( A[i]%(1000000007) )
        c[i] = c[i]%(1000000007)
    ans = 0
    for i in range(0,N-1,1):
        A[i] = A[i]%(1000000007)
        tmp = (A[i]*(c[N-1]-c[i]))%(1000000007)
        ans += tmp

    return ans%(1000000007)

print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

print(ans)

Submission status: Accepted

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

s.pop(0)

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

Submission status: Accepted

Solution code:


def main():
    n = int(input().strip())
    l = list(map(int, input().split()))
    stack = [0]*n

    totstack = 0
    for i in range(n-1):
        totstack += l[n-i-1]
        stack[n-i-2] = totstack

    tot = 0
    for i in range(n):
        tot += (l[i] * (stack[i] % (10 ** 9 + 7))) % (10 ** 9 + 7)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(lambda x: int(x), input().split(' ')))

res = ((sum(A)**2 - sum(list(map(lambda x: x**2, A)))) // 2) % (10**9 + 7)

print(int(res))

Submission status: Accepted

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

Submission status: Accepted

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

Sum = sum(A)

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

ans %= 1000000007

print(int(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

mod = 10**9+7

S = sum(A)
S2 = sum(map(lambda x:x*x, A))

print((S*S - S2)//2 % mod)

Submission status: Accepted

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

s = 0
for i in range(n):
  s += a[i]
  #sはaの和

t = 0
for i in range(n):
  t += a[i]**2
  #tはaの2乗和

ans = ((s**2 - t)//2) % (10**9 + 7)
print(ans)

Submission status: Accepted

Solution code:

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

    # 面積として見る: 縦*横
    square = sum(A) ** 2

    # 対角線
    diag = sum([a * a for a in A])

    ans = (square - diag) // 2
    print(ans % MOD)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

s = sum(a)
t = sum(map(lambda x:x**2,a))
ans = ((s**2 - t)//2) % (10**9 + 7)
print(ans)

Submission status: Accepted

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

n = int(input())
a = list(map(int, input().split()))
mod = int(1e9)+7

s = sum(a)
ans = sub = 0 
for i in range(n):
    ans += a[i]*s
for i in range(n):
    sub += a[i]*a[i]
ans = ((ans-sub)//2)%mod
print(ans)

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Submission status: Accepted

Solution code:
n = int(input())
As = list(map(int, input().split()))
MOD = 10**9 + 7
S = sum(As)%MOD
ans = 0
for a in As:
    ans += (a * (S-a))%MOD
    ans %= MOD
ans *= pow(2,MOD-2,MOD)
print(ans%MOD)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

    mod = 10 ** 9 + 7
    ans = 0
    x = 0

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

    print(ans)

if __name__ == '__main__':
    abc177_c_2()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
mod=10**9+7
a=[int(x) for x in input().split()]
a.sort(reverse=True)
ruiseki=[a[0]]
for i in range(1,n):
  ruiseki.append(a[i]+ruiseki[i-1])

ans=0
ruiseki.sort(reverse=True)

a.sort()
for i in range(1,n):
  ans+=((a[i-1])*ruiseki[i])%mod

print(ans%mod)

Submission status: Accepted

Solution code:
'''
（公式解説参照）
A[i]*A[i+1]+A[i]*A[i+2]+…+A[i]*A[N]=A[i]*(A[i+1]+A[i+2]+…+A[N])
リストAに対する累積和を求めておく
'''
N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

# 累積和
total = []
total.append(A[0])
for i in range(1, N):
    total.append(total[i-1] + A[i])
# print(total)

# iの値について全探索
ans = 0
for i in range(N):
    tmp = (total[-1] - total[i])
    ans += A[i] * tmp
    ans %= MOD

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)
for i in a:
    s -= i
    ans = (ans + (i * s))
ans = ans % mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
 
# required function 
def findProductSum(A, n): 
      
    # calculating array sum (a1 + a2 ... + an) 
    array_sum = 0
    for i in range(0, n, 1): 
        array_sum = 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)
mod = 10**9+7
  
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
  
print(modinv(2)*int(findProductSum(a, n))%mod)

Submission status: Accepted

Solution code:
N = int(input())

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

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

c = S
d = 0
for j in range(N):
  c -= A[j]
  c = c%(10**9+7)
  d += A[j]*c
  d = d%(10**9+7)
  
print(d)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10**9 + 7


def main():
    _ = int(input())
    A = [int(i) for i in input().split()]

    ans = (sum(A)**2) % MOD - (sum(pow(a, 2, MOD) for a in A)) % MOD
    ans *= pow(2, MOD-2, MOD)
    ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
p = 10**9+7
n = int(input().rstrip())
a_ls = list(map(int, input().rstrip().split(" ")))
a_ls = [a%p for a in a_ls]
a2_ls = [(a**2)%p for a in a_ls]
a_sum_sq = ((sum(a_ls))**2)%p
a2_sum = sum(a2_ls)%p
ans = (a_sum_sq-a2_sum)%p
if ans%2 != 0:
  ans += p
ans //= 2
print(ans)

Submission status: Accepted

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

tot = 0
sqr = 0
ind = 0
for i in range(n):
  tot += a[i]

sqr = tot*tot
for i in range(n):
  ind += a[i]*a[i]

print(((sqr-ind)//2)%1000000007)

Submission status: Accepted

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

Submission status: Accepted

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

sum_num = 0
sum_list = []
for num in num_list:
  sum_num += num
  sum_num = sum_num % mod
  sum_list.append(sum_num)

answer = 0
for i in range(N):
  sum_num -= num_list[i]
  sum_num = sum_num if sum_num >= 0 else sum_num + mod
  answer += num_list [i] * sum_num
  answer = answer % mod

print(answer)

Submission status: Accepted

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

list_sum = sum(num_list)

ans = 0
for i in range(N-1):
    list_sum -= num_list[i]
    ans += num_list[i] * list_sum

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10**9+7
N = int(input())
A = list(map(int,input().split()))
from itertools import accumulate
B = list(accumulate(A))
ans = 0
for i in range(N):
    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())) #入力する数列A
SUMA = sum(A) #数列の和
MOD = 10**9 + 7 # mod
C = [0] * (N-1) #累積和数列
for i in range(N-1): #\sum_{j = i+1}^{N}を求めて数列に代入する
  SUMA -= A[i]
  C[i] = SUMA
ans = 0 #求める答え
for i in range(N-1):
  ans += A[i]*C[i]
  ans %= MOD #その都度modで割った余りにする
print(ans) #答えを出力する

#計算量はO(N)です。

Submission status: Accepted

Solution code:
def square(list):
    return [i ** 2 for i in list]
m = 1000000007
n = int(input())
A = list(map(int, input().split()))
sq = square(A)
s1 = sum(A)
s2 = sum(sq)
 
result = (s1**2-s2)//2
print(result%m)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, N):
  r += A[i - 1]
  ans += A[i] * r
  
ans = ans % m

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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