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:
def main():
    N=int(input())
    A=list(map(int,input().split()))

    sumA=sum(A)

    rsumA=A[0]

    res=0
    mod=pow(10,9)+7

    for i in range(len(A)-1):
        res+=A[i]*(sumA-rsumA)
        res=res%mod
        rsumA+=A[i+1]

    print(res)
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin, stdout
def gmi(): return map(int, stdin.readline().strip().split())
def gms(): return map(str, stdin.readline().strip().split())
def gari(): return list(map(int, stdin.readline().strip().split()))
def gars(): return list(map(int, stdin.readline().strip().split()))
def gs(): return stdin.readline().strip()
def gls(): return list(stdin.readline())
def gi(): return int(stdin.readline())

n = gi()
ar = gari()
c = 0
ans = 0
s = sum(ar)
sub = 0
for i in ar[:-1]:
    sub +=  ar[c]
    ans += i*(s-sub)
    c += 1
print(ans%1000000007)

Submission status: Accepted

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

Submission status: Accepted

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

total = 0
before = 0
for i in range(len(As) - 1):
    if i == 0:
        total = total + As[i] * sum(As[i+1:])
        before = sum(As[i+1:])
    else:
        total = total + ((before - As[i]) * As[i])
        before = before - As[i]

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

print(int(total))

Submission status: Accepted

Solution code:
N = int(input())
Alist = list(map(int,input().split()))
M = 10**9+7
Asum = sum(Alist)
Answer = 0
for i in range(len(Alist)):
    Asum -= Alist[i]
    Answer += (Alist[i]%M)*(Asum%M)
    Answer = Answer%M
print(Answer)

Submission status: Accepted

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

m=sum(i)

ans=0
for k in range(n):
    ans+=(i[k]*(m-i[k]))
ans=ans//2
ans=ans%(10**9+7)
print(ans)

Submission status: Accepted

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

A = [0]
for i in range(n):
    A.append(A[i] + a[i])

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

print(ans%MOD)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sum_A = sum(A) % (10 ** 9 + 7)
ans = 0

for i in range(len(A)):
  if sum_A - A[i] < 0:
    sum_A = sum_A - A[i] + (10 ** 9 + 7)
  else:
    sum_A -= A[i]
  ans += A[i] * sum_A % (10 ** 9 + 7)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
s = 0
s2 = 0
P = 10**9 + 7
for i in range(N):
    s = ( s + A[i] )%P
    s2 = (s2 + (A[i]**2) % P ) % P
r2 = pow(2, P-2, P)
print( ( (  ( s**2 % P - s2 ) % P ) * r2 ) % P)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

mod = 10**9+7

count = 0

num = sum(A) % mod

ans = 0
for a in A[:-1]:
    num = num-a
    ans = (ans+a*num) % mod

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7

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

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

print(res)

Submission status: Accepted

Solution code:
from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip

import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
#from functools import lru_cache   #use @lru_cache(None)
if os.path.exists('in.txt'): sys.stdin=open('in.txt','r')
if os.path.exists('out.txt'): sys.stdout=open('out.txt', 'w')
#
def input(): return sys.stdin.readline()
def mapi(arg=0): return map(int if arg==0 else str,input().split())
#------------------------------------------------------------------


n = int(input())
a = list(mapi())
b = a[:]
for i in range(n-2,-1,-1):
    b[i] += b[i+1]
res = 0
mod =  int(1e9+7)
for i in range(n-1):
    res = (res+a[i]*b[i+1]%mod)%mod
print(res)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
su=sum(a)
ru=[0 for i in range(n)]
ru[0]=su-a[0]

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

Submission status: Accepted

Solution code:
#ceilevenoddformula
import math
import bisect
import copy
import time
from collections import Counter
from itertools import combinations
#map(int,input().split())
#int(x) for x in input().split()
n=int(input())
l=[int(x) for x in input().split()]
ans=[0]*n
l=l[::-1]
ans[0]=l[0]
for i in range(1,n):
	ans[i]=l[i]+ans[i-1]
l=l[::-1]
c=0
M=10**9+7
ans=ans[::-1]
for i in range(1,n):
	c+=(ans[i]*l[i-1])%M
	c=c%M
print(c)

Submission status: Accepted

Solution code:
n = int(input())
*A, = map(int, input().split())
MOD = 10**9 + 7
x = ((sum(A) % MOD)**2) % MOD
y = sum(a**2 % MOD for a in A) % MOD
print(((x - y) % MOD) * pow(2, MOD - 2, MOD) % MOD)

Submission status: Accepted

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

Submission status: Accepted

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

B = [0]*N

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

print(ans%1000000007)

Submission status: Accepted

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

# 愚直
# res = 0
# for i in range(n-1):
#     for j in range(i+1, n):
#         res += arr[i] * arr[j]
#         res %= mod

# print(res)

s = sum(arr)
res = 0
for a in arr:
    s -= a
    res += a * s
    res %= mod

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
res = 0
S = sum(A)
for a in A:
    res += a * (S - a)
    S = (S - a)
print(res % 1000000007)

Submission status: Accepted

Solution code:
def modpow(a, n, mod):
    if n == 0:
        return 1
    else:
        if n % 2 == 0:
            return (modpow((a * a)%mod, n//2, mod)) % mod
        else:
            return ((a%mod) * modpow(a, n-1, mod)) % mod

# modが素であるとする
def modinv(a, mod):
    return modpow(a, mod-2, mod) % mod

def moddiv(a, b, mod):
    return ((a%mod) * modinv(b, mod)) % mod


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

Submission status: Accepted

Solution code:
import sys


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

def main():

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

    ss =sum(A) %mod
    sm =ss *ss % mod
    for i in A:
        sm -= i*i
        sm %=mod

    sm =sm*pow(2,mod-2,mod) %mod

    print(int(sm))










if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().strip().split()))[:n]
sum=0 
sqsum=0
MOD=1000000007
for x in a:
    sum = sum+x
    sqsum = sqsum+ x*x

sum = sum*sum

ans = (sum-sqsum)//2
ans = ans%MOD
print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

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

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

Submission status: Accepted

Solution code:
from copy import deepcopy

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

B = deepcopy(A)

for i in range(N-1):
    B[i+1] += B[i]

ans = 0

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
MOD = 10 ** 9 + 7
MOD2INV = 5 * 10 ** 8 + 4
total = 0
delta = 0

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

result = ((((total * total) - delta + MOD) % MOD) * MOD2INV)%MOD
print(result)

Submission status: Accepted

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

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    sum_all = sum(A) % MOD
    ans = 0
    for i in range(N-1):
        ans = (ans + (A[i] * (sum_all-A[i]))) % MOD
        sum_all -= A[i]
    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=tf-8
#

"""
"""

from collections import defaultdict
from itertools import accumulate
import bisect

import sys
input = sys.stdin.readline

def solve(a):
    b = a.copy()
    b.reverse()
    c = list(accumulate(b))
    ans = 0
    modulo = 10**9 + 7
    n = len(a)
    for i, aa in enumerate(a[:-1]):
        cc = c[n-2-i] % modulo
        ans = (ans + (aa*cc%modulo))%modulo
    #print(a,b,c)
    print(ans)

            

def main():
    _ = input()
    a = list(map(int,input().split()))
    solve(a)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# print('input >>')
N = input()
As = list(map(int,(input().split())))
MOD = 10**9 + 7

As_sum = [As[0]]

for i in range(1, len(As)):
    As_sum.append((As_sum[-1]+As[i]) % MOD)

ans = 0
for i in range(len(As)-1, 0, -1):
    ans += As[i] * As_sum[i-1]
    ans %= MOD

# print('-----output-----')
print(ans % MOD)

Submission status: Accepted

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

S_n = 0
for a in A:
    S_n += a
    S_n %= MOD

a_i2 = 0
for a in A:
    a_i2 += a*a
    a_i2 %= MOD

d_ans = pow(S_n, 2 , MOD) - a_i2 + MOD
ans = (d_ans // 2) % MOD1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum=[0]*n

sum[-1]=s[-1]

for i in range(n-2,-1,-1):
    sum[i]=sum[i+1]+s[i]

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

print(ans%(1000000007))

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7
n = int(input())
s = list(map(int, input().split()))
acu = [0]
t = s[::-1]
for i in range(n):
    acu.append(acu[-1] + t[i])
u = acu[::-1]

res = 0
for i in range(1, n):
    res += s[i - 1] * u[i]
    res %= MOD
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
suma=sum(arr)
suff=[suma]*n
mod=1000000007
suff[n-1]=arr[n-1]
for i in range(n-2,-1,-1):
    suff[i]=suff[i+1]+arr[i]
ans=0
for i in range(1,n):
    ans+=arr[i-1]*suff[i]
    ans%=mod
print(ans)

Submission status: Accepted

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

sum_an = sum(an)
total = 0
for ai in an:
    total = total + ai * (sum_an - ai)
    sum_an = sum_an - ai
print(total % ((pow(10, 9) + 7)))

Submission status: Accepted

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

Submission status: Accepted

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

n = int(input())
A = list(map(int, input().split()))
#n, m = map(int, input().split())
#s = input()
#s,t = input().split()
#a = [int(input()) for _ in range(n)]
#
#readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
#ab = [[int(i) for i in readline().split()] for _ in range(n)]

MOD = 10**9+7

tot = sum(A)
tot %= MOD

ans = 0
rmv = 0

for a in A:
    rmv += a
    rmv %= MOD
    ans += a*(tot-rmv)
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
n = int(input())
a_list = list(map(int, input().split()))
ans = 0
total = 0
mod = 10 ** 9 + 7
for i in range(n):
    total += a_list[i]
    if total >= mod:
        total = total % mod

for i in range(n - 1):
    total -= a_list[i]
    if total < 0:
        total += mod
    ans += a_list[i] * total
    if ans >= mod:
        ans = ans % mod
print(ans)

Submission status: Accepted

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

AA = sum(A)
ans = 0
mod = 10**9 + 7
for i in range(n):
    ans += (A[i] * (AA - A[i]))
print((ans // 2) % mod)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Ans = 0

sum_L = L[0]

l = len(L)

for i in L[1:]:
    Ans += sum_L*i%(10**9+7)
    sum_L += i
Ans = Ans%(10**9+7)
print(Ans)

Submission status: Accepted

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

print(sum)

Submission status: Accepted

Solution code:
def resolve():
  N = int(input())
  A = [int(x) for x in input().split(" ")]
  # sumA = sum(A)
  reversedIntegraA = [0] * len(A)
  reversedIntegraA[-1] = A[-1]
  base = 10**9+7

  for i in reversed(range(len(A) - 1)):
    reversedIntegraA[i] = reversedIntegraA[i+1] + A[i]

  result = 0
  for i in range(len(A) - 1):
    result += A[i] * reversedIntegraA[i+1]
    if result > base:
      result %= base
  print(result)


if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

Submission status: Accepted

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

MOD = 10**9+7


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

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


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Accepted

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

modular=10**9+7
sum=0

AllSum=0
for i in range(N):
  AllSum+=A[i]
  AllSum=AllSum%modular

  
RuisekiWa=[]
NextSum=AllSum

for i in range(N):
  RuisekiWa.append((NextSum-A[i])%modular)
  NextSum=RuisekiWa[i]

#print(RuisekiWa)
for i in range(N):
  sum+=(A[i]*RuisekiWa[i])%modular
  sum=(sum%modular)
    
print(sum%modular)

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, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()

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

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

    print(ans % mod)


if __name__ == '__main__':
    main()

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
print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 1000000007
n = int(input())
A = [int(x) for x in input().split()]
cum = [0]
for a in A:
    cum.append((cum[-1] + a)%mod)

# sum(sum(A[i]*A[j] for j in range(i+1, n)) for i in range(n))%mod =
print(sum(A[i]*(cum[n] - cum[i+1])%mod for i in range(n))%mod)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

A_sum_s = sum(A) ** 2

for e in A:
    A_sum_s -= e ** 2
print(A_sum_s // 2 % (10**9 + 7))

Submission status: Accepted

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

sum_arr = sum(arr)
sum_pow = 0

for a in arr:
  sum_pow = sum_pow + a*a

result = ((sum_arr*sum_arr) - sum_pow )//2 % mod

print(result)

Submission status: Accepted

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

MOD = 10 ** 9 + 7

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

print(res)

Submission status: Accepted

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

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    ans = 0

    B = [sum(A[1:])]
    for i, a in enumerate(A[1:-1]):
        B.append(B[i] - a)

    for i, ai in enumerate(A[:-1]):
        ans += (ai * B[i]) % MOD
    
    print(ans % MOD)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

p_mod = (10**9)+7

A_sum = [A[0]]
a_before = A_sum[0]
for a in A[1:]:
    A_sum.append(a+a_before)
    a_before = a+a_before

ans=0
for n in range(N-1):
    ans += (A[n]%p_mod)*((A_sum[-1]%p_mod)-(A_sum[n]%p_mod))%p_mod
#     print(ans)
print(ans%p_mod)

Submission status: Accepted

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

m = 10**9 + 7
s = 0
for i in lis:
    s += i
    s %= m

ans = 0
for i in lis:
    s -= i
    ans += i * s
    ans %= m

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 range(n-1):
    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()))
s=0
b=[]
su=0
for i in range(1,n):
    su+=a[-i]
    b.append(su)
for j in range(2,n+1):
    s+=a[-j]*b[j-2]
    s%=(10**9+7)
print(s)

Submission status: Accepted

Solution code:
def ruisekiwa(l):
    #関数バージョン
    lst = [0]
    s = 0
    for i,v in enumerate(l):
        lst.append(s + v)
        s += v
    return lst

def solve(n,a):
    r = ruisekiwa(a)
    sm = 0
    for i in range(n-1):
        sm += a[i] * (r[-1] - r[i+1])
    return sm % (10**9 + 7)
def solve2(n,a):
    sm = 0
    for i in range(n-1):
        for j in range(i+1,n):
            sm += a[i] * a[j] % (10**9 + 7)
    return sm


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

    print(solve(n,a))
    #print(solve2(n,a))



if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    
    SUM = [sum(A)]
    for i in range(1,N):
        SUM.append((SUM[i-1]-A[i-1])%(10**9+7))
    
    ans = 0
    for i in range(N-1):
        ans += A[i]*SUM[i+1]
        ans = ans%(10**9+7)
    
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD = 1000000007
n = int(input())
A = [int(x) for x in input().split()]
result = 0
sumA = sum(A)
for a in A:
    sumA -= a
    result = (result + a * sumA) % MOD
print(result)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = (s*s - ss)//2
print(ans%mod)

Submission status: Accepted

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

    B = sum(A)
    answer = 0
    for i in range(0, N):
        answer += A[i] * (B - A[i])
        B -= A[i]

    answer %= (10 ** 9 + 7)

    print(answer)


if __name__ == "__main__":
    main()

# A1*A2 + A1*A3 + ... + A1*AN = A1 * (A2 + A3 + ... + AN)
# +
# A2*A3 + A2*A4 + ... +| A2*AN = A2 * (A3 + A4 + ... + AN)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 100000
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"

n = k()
a = l()

ruiseki = list(itertools.accumulate(a))

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

print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys,math
from collections import deque,defaultdict
import operator as op
from functools import reduce

sys.setrecursionlimit(10**6) 

I=sys.stdin.readline

 #s="abcdefghijklmnopqrstuvwxyz"

"""
x_move=[-1,0,1,0,-1,1,1,-1]
y_move=[0,1,0,-1,1,1,-1,-1]
"""
def ii():
	return int(I().strip())
def li():
	return list(map(int,I().strip().split()))
def mi():
	return map(int,I().strip().split())

def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer // denom

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x




	
def main():
	n=ii()
	arr=sorted(li())
	ss=[arr[-1]]
	for i in range(n-2,-1,-1):
		ss.append(ss[-1]+arr[i])
	ss.reverse()
	ans=0
	for i in range(n-1):
		ans+=arr[i]*ss[i+1]
	print(ans%(10**9+7))
			


	

























	
	
	





if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
def square(x):
  y = x**2
  return y


n = int(input())
al = list(map(int, input().split()))
als = list(map(square, al))

print(((sum(al)**2-sum(als))//2)%((10**9)+7))

Submission status: Accepted

Solution code:
import math
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y
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
def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    divisors.sort()
    return divisors
mod = 10**9 + 7

#maincode-------------------------------------------------

n = ini()
a = inl()
b = a[::]
ans = 0
for i in range(n-2,0,-1):
    a[i] += a[i+1]%mod

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

Submission status: Accepted

Solution code:
def ex_euclid(a, b):
  x0, x1 = 1, 0
  y0, y1 = 0, 1
  z0, z1 = a, b
  
  while z1 != 0:
    q = z0 // z1
    z0, z1 = z1, z0 % z1
    x0, x1 = x1, x0 - q * x1
    y0, y1 = y1, y0 - q * y1
  
  return z0, x0, y0
 
def mod_inv(a, n):
  g, x, _ = ex_euclid(a, n)
  if g != 1:
    print("modular inverse does not exist")
  else:
    return x % n

N = int(input())
A = list(map(int, input().split()))
M = 10**9 + 7
normal = 0
squared = 0
ans = 0
for a in A:
  normal = normal + a % M
  squared = squared + a*a % M
ans = (ans + normal * normal) % M
ans = (ans - squared) % M
ans = ans * mod_inv(2, M) % M
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.reverse()
mod = 10**9 +7
ans = 0
S=[0]
for i in range(0,N):
    S.append(S[i]+a[i])
ans = 0
for j in range(len(a)):
    ans +=(a[j]*S[j])
print(ans%mod)