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:
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', '')
import sys
import itertools
import copy


def main():
    N = I()
    a_list = LI()
    sum_list = []
    # print(a_list)
    # print(list(range(N)))
    # print(list(itertools.combinations(list(range(N))), 2))
    # print(list(itertools.combinations([1, 2, 3], 2)))
    cnt = 0
    total = sum(a_list)
    # print(total)
    current_sum = 0
    for i in range(N):
        current_sum += a_list[i]
        sum_list.append(total - current_sum)
    # print(sum_list)
    ans = 0
    for i in range(N):
        ans += sum_list[i] * a_list[i]
        ans =  ans % (10**9 + 7)
    print(ans)
    # L = LLIN(N)

    #     if T[-i]
    # if D / T <= S:
    #     print('Yes')
    # else:
    #     print('No') 
    # cnt = 0
    # for l in L:
    #     num = l[0] ** 2 + l[1] ** 2 
    #     if num <= D ** 2:
    #         cnt+=1
    # print(cnt)
    # print(row)
    # total = sum(row)
    # target = total - K
    # if K == total:
    #     print(1)
    #     sys.exit()
    # col = []
    # for w in range(W):
    #     cnt = 0
    #     for h in range(H):    
    #         print(A[h][w])
    #         if A[h][w] == '#':
    #             cnt += 1
    #     col.append(cnt)
    # print(col)
    # ans = 0
    # for w in range(W):
    #     for h in range(H):
    #         if A[h][w] == '#':
    #             if col[w] + row[h] - 1 == target:
    #                 print(str(h) + str(w))
    #                 ans +=1
    #         else:
    #             if col[w] + row[h] == target:
    #                 print(str(h) + str(w))
    #                 ans +=1
    # print(ans)
    #     for  in x:


    # print('AC x ' + str(cnt['AC']))
    # print('WA x ' + str(cnt['WA']))
    # print('TLE x ' + str(cnt['TLE']))
    # print('RE x ' + str(cnt['RE']))
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
lst=list(map(int,input().split()))
aa=sum(lst) % (10**9+7)
# print(sum(lst) % (10**9+7) )
new_lst=[]
temp_1=aa
for i in range(n-1):
    temp_1-=lst[i]
    new_lst.append(temp_1)
# print(new_lst)

tem=0
for i in range(n-1):
    tem+=(lst[i] )*new_lst[i]
        # print(tem)
print(tem % (10**9 +7))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
total=0
ans=0
d=[0]*210000

for i in range(N):
    total+=A[i]
    d[i]=total

for j in range(N):
    ans+=A[j]*(total-d[j])

print(ans%1000000007)

Submission status: Accepted

Solution code:
MOD = 10**9+7

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

a1 = sum(a)
a2 = sum([x*x for x in a])

print(((a1**2-a2)//2)%MOD)

Submission status: Accepted

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

s = sum(lis) ** 2
ss = sum([i ** 2 for i in lis])

print(((s - ss)//2)%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
p = 0
c = len(A) - 1
total = 0
while 0 < c:
    p += A[c]
    total = total + p * A[c-1]
    if total >= 1000000007:
        total %= 1000000007
    c -= 1

print(total)

Submission status: Accepted

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

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+=s*a[i]
print(ans%(10**9+7))

Submission status: Accepted

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

X = 0
Ans = 0

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

print(Ans)

Submission status: Accepted

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

result = 0

sum_a = 0
for i in range(n):
    sum_a = (sum_a + a[i]) % m

result = 0
for i in range(n):
    x = a[i]
    sum_a -= x
    result = (result + x*sum_a) % m

if(result < 0):
    result += m
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
N = int(input())

modsum = 0
A = list(map(int, input().split()))
for a in A:
    modsum = (modsum + a) % MOD

count = 0
for a in A[:-1]:
    modsum -= a
    while modsum < 0:
        modsum = MOD + modsum
    count = (count + (modsum * a) % MOD) % MOD
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = 0
k = 0
M = 10**9 + 7

for i in range(0, N-1):
    k += An[i]
    cnt += k * An[i+1]

ans = cnt % M
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A= list(map(int,input().split()))
mod_num = int(1.0e+9 + 7)
rest = 0
temp = 0

for i in range(N-1):
  temp+=A[i]
  rest += temp*A[i+1]
  rest = rest%mod_num

print(int(rest%mod_num))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
s=input()
m=list(s.split())
a=0
b=0
for i in range(0,n):
  a=a+int(m[i])
  b=b+(int(m[i])*int(m[i]))
l= ((a**2)-b)//2
l=l%1000000007
print(l)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

def mod(x):
	return x % 1000000007

sa = sum(A[1:])
s = mod(A[0] * sa)

for a in A[1:-1]:
	sa -= a
	s = mod(s + a * sa)

print(s)

Submission status: Accepted

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

N = 2
FAC = [1]
INV = [1]
for i in range(1,N+1):
  FAC.append((FAC[i-1]*i) % MOD)
  INV.append(pow(FAC[-1],MOD-2,MOD))
#print(INV[2])
  
s = 0
for ai in a:
  s += ai
  s %= MOD
ans = 0
for ai in a:
  ans += ai*(s-ai)
  ans %= 10**9+7
print((ans*INV[2])%MOD)
#print((10**9)**2%MOD)

Submission status: Accepted

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

for i in range(n - 1):
    main = a[i]
    a_sum = a_sum - main
    res =  (res + main * a_sum % mod) % mod
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

a = sum1*sum1
li = [x**2 for x in L]

b = sum(li)

print(((a-b)//2)%mod)

Submission status: Accepted

Solution code:
def main():

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


    m = 10**9 + 7
    ans = 0
    s = []
    total = sum(a)
    s.append(total-a[0])
    for i in range(1,n):
        s.append(s[i-1] - a[i])
    for i in range(n):
        tmp = s[i] % m
        ans += tmp * a[i]
    # ans //= 2
    print(ans%m)

if __name__=='__main__':
    main()

Submission status: Accepted

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

mod_value = 10**9 + 7

sum = 0
total = 0
for i in reversed(range(1, len(a))):
    sum += a[i]
    total += a[i-1] * sum

print(total%mod_value)

Submission status: Accepted

Solution code:
_ = input()
nums = [int(x) for x in input().split(" ")]
res = 0
sums = []
for n in nums[1:][::-1]:
    if sums:
        sums.append(sums[-1]+n)
    else:
        sums.append(n)
sums.reverse()
#print(sums)
for i, ni in enumerate(nums[:-1]):
    res += ni*sums[i]
    res %= 10**9 + 7
print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = 10**9+7
sa = sum(a)
c=0
for i in a:
    c +=(i*i)%(2*b)

print(int((sa%(2*b)*sa%(2*b)-c%(2*b))/2)%b)

Submission status: Accepted

Solution code:

import numpy as np
from functools import *
import sys
sys.setrecursionlimit(100000)
input = sys.stdin.readline


def array(size, init=0):
    return [[init for j in range(size[1])] for i in range(size[0])]


def acinput():
    return list(map(int, input().split(" ")))


def II():
    return int(input())


def egcd(a, b):
    if a == 0:
        return (b, 0, 1)
    else:
        g, y, x = egcd(b % a, a)
        return (g, x - (b // a) * y, y)


def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


K = 10**9+7
N = II()
a = np.array(acinput())
a = a[a != 0]
N = len(a)
s1 = 0
for i in range(N):
    s1 += a[i]
    s1 %= K

s1 %= K

# s1%=K
# s2 = s1
s = (s1*s1) % K
T = 0
for i in range(N):
    T += a[i]*a[i]
    T %= K
# s += K

tmp = modinv(2, K)
s += K
print((s-T)*tmp % K)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = [i % (10**9 + 7) for i in a]
val = sum(a[1:])
l = [val]
for i in range(1, n):
    val -= a[i]
    l.append(val)
ans = 0
for i in range(n - 1):
    ans += a[i]*l[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

import copy
B = copy.copy(A)
C = sum(B)
ans = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = input()
a = list(map(int, input().split()))
sum_a = sum(a)
count = 0
for i, v in enumerate(a):
    sum_a -= v
    count += v * sum_a

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

Submission status: Accepted

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

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

ans = 0

sums = [0] * (n - 1)

for j in range(n - 1):
    if j == 0:
        sums[0] = nums[0] % x
    else:
        sums[j] = (sums[j - 1] + nums[j]) % x

for i in range(1, len(nums)):
    ans += sums[i - 1] * nums[i]
    ans = ans % x

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
temparr = input()
temparr = temparr.split()
arr = []
for i in temparr:
    arr.append(int(i)  % 1000000007)
    
ans = 0 
total = []
curtotal = sum(arr)
total.append(curtotal)
for i in range(n):
    curtotal -= arr[i]
    total.append(curtotal)
for i in range(n):
    total[i] = total[i] % 1000000007

for i in range(n - 1):
    curnum = arr[i]
    tempans = curnum * total[i + 1]
    if tempans >= 1000000007:
        tempans = tempans % 1000000007
    ans += tempans

if ans >= 1000000007:
    print(ans % 1000000007)
else:
    print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int,input().split()))
    ans = 0
    m0 = 10**9+7
    Ar = [0]*N
    sum0 = 0
    for i in reversed(range(N)):
        sum0 = (sum0 + A[i]) % m0
        Ar[i] = sum0

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import setrecursionlimit
from collections import Counter
from collections import defaultdict
from collections import deque
from heapq import heapify
from heapq import heappop
from heapq import heappush
from time import time
import re
setrecursionlimit(10**9)
mod=10**9+7
inf=10**18

n=int(input())
a=[int(x) for x in input().split()]
cum_a=[]
for i in range(n):
    if i==0:cum_a.append(a[i])
    else:cum_a.append(cum_a[-1]+a[i])
ans=0
for i in range(n-1):
    ans+=a[i]*(cum_a[n-1]-cum_a[i])
    ans%=mod
     
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
l=list(map(int,input().split()))
ans=0
i=0
S=sum(l)
while i<len(l)-1:
  S=S-l[i]
  ans+=l[i]*S
  i+=1
print(ans%1000000007)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    Asum = Asum - A[i]
    ans += A[i]*Asum % (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_sum = sum(A[1:])

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

Submission status: Accepted

Solution code:
n = int(input())
M = 1000000007
a = input().split()
sum = 0
sq_sum = 0
for x in a:
    y = int(x)
    sum += y
    sq_sum += (y*y)
sum = sum * sum
ans = abs(sum - sq_sum)//2
ans = (ans + M) % M
ans = int(ans)
print(ans)

Submission status: Accepted

Solution code:
#ABC177 C
N = int(input())
A = list(map(int,input().split()))
ans = 0
Asum = sum(A)

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate


def main():
    n = int(input())
    array = [int(x) for x in input().split()]
    acc = accumulate(array)
    mod = 10 ** 9 + 7
    ans = 0
    for i, ai in enumerate(acc):
        if i == n - 1:
            continue
        ans += ai * array[i + 1]
        ans %= mod
    return ans


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
lst=list(map(int,input().split()))
aa=sum(lst) % (10**9+7)
# print(sum(lst) % (10**9+7) )
new_lst=[]
temp_1=aa
for i in range(n-1):
    temp_1-=lst[i]
    new_lst.append(temp_1)
# print(new_lst)

tem=0
for i in range(n-1):
    tem+=(lst[i] )*new_lst[i]
        # print(tem)
print(tem % (10**9 +7))

Submission status: Accepted

Solution code:
M=1000000007
def findProductSum(A, n): 
    s = 0
    for i in range(n): 
        s+=A[i] 
    array_sum_square = s*s
    individual_square_sum = 0
    for i in range(n): 
        individual_square_sum += A[i]*A[i]
    return (array_sum_square -individual_square_sum) // 2
n=int(input())
a=list(map(int,input().split()))
print(findProductSum(a,n)%M)

Submission status: Accepted

Solution code:
from itertools import accumulate
N=int(input())
A=list(map(int,input().split()))
p=10**9+7
ans=0
B=list(accumulate(A[::-1]))
for i,value in enumerate(A):
    if i==(N-1):
        break
    ans+=((value%p)*(B[-2-i]%p))%p
    ans=ans%p
print(ans)

Submission status: Accepted

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

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


print(total%mod)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	n = int(input())
	#b , c = tin()
	#s = input()	
	al = lin()
	#dd = sum([v*v for v in al])
	d = sum(al)
	#return ( d*d - dd)//2
	ret = 0
	for v in al:
		d -= v
		ret += v*d
		ret = ret % mod
	return ret
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
B = A[::-1]

mod_n = 10**9+7

cum_A = [0]
for i in range(n-1):
  cum_A.append(cum_A[i]+B[i])
  
ans = 0
cum_B = cum_A[::-1]
for i in range(n-1):
  ans += A[i]*cum_B[i]%mod_n
  
print(ans%mod_n)

Submission status: Accepted

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

ans = 0
tmp_val = 0
for i in range(n):
  tmp_val = (tmp_val + a[i]) % c

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

print(ans % c)

Submission status: Accepted

Solution code:
def fastSumPair(A):
    L = len(A)
    M = 1000000007
    
    # (a + b + c)**2 = a**2 + b**2 + c**2 + 2*(a*b + b*c + c*a)
    # derive from this equation
    
    # first array
    A_1_sum = 0
    for i in range(0, L):
        A_1_sum = (A_1_sum + A[i]) 
    
    A_1_sum = (A_1_sum ** 2) 
        
    # second array
    A_2_sum = 0
    for i in range(0, L):
        A_2_sum = (A_2_sum + A[i] ** 2) 
                  
    return int((A_1_sum - A_2_sum) // 2) % M        

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

ruisekiwa = [0]*N
wa = sum(A)%M

result = 0
for i in range(N):
    wa -= A[i]
    result = (result+(A[i]*wa)%M)%M

print(result)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = sum(a) ** 2
for i in a:
    ans -= i ** 2
print((ans // 2) % (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 A:
    S-=i
    ans+=i*S
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)
y=0
for i in range(n):
    y+=a[i]**2

ans = (x**2 - y)//2

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

print(int(ans))

Submission status: Accepted

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

s_1 = 0
s_2 = 0
m = 10 ** 9 + 7
for i in range(n):
    s_1 += a[i] % m
    s_2 += (a[i] ** 2) % m
s = (s_1 * s_1 - s_2) * pow(2, -1, m) % m
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = [int(i) for i in input().split(maxsplit=N)]
    presum = [0 for _ in range(N + 1)]
    for i in range(N):
        presum[i + 1] = presum[i] + A[i]
    sumA = 0
    for i in range(1, N):
        sumA += (A[i] * (presum[i])) % (10**9 + 7)
    print(sumA % (10**9 + 7))


resolve()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(s) for s in input().split()]
b = []
val = 0
sum_val= 0
for i in range(0, n):
    b.append(a[i] * sum_val) 
    sum_val+= a[i] 
val = sum(b)
print(val % (10 ** 9 + 7))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum1 = sum(As) % mod2
squared1 = (sum1 ** 2) % mod2

squared2 = 0

for i in range(N):
    squared2 += (As[i] ** 2) % mod2

print(int((squared1 - squared2) / 2) % mod)

Submission status: Accepted

Solution code:
from itertools import accumulate
from sys import stdin


class ModInt:
    def __init__(self, x):
        self.x = x % MOD

    def __str__(self):
        return str(self.x)

    __repr__ = __str__

    def __add__(self, other):
        return ModInt(self.x + other.x) if isinstance(other, ModInt) else ModInt(self.x + other)

    def __sub__(self, other):
        return ModInt(self.x - other.x) if isinstance(other, ModInt) else ModInt(self.x - other)

    def __mul__(self, other):
        return ModInt(self.x * other.x) if isinstance(other, ModInt) else ModInt(self.x * other)

    def __truediv__(self, other):
        return (
            ModInt(self.x * pow(other.x, MOD - 2, MOD))
            if isinstance(other, ModInt)
            else ModInt(self.x * pow(other, MOD - 2, MOD))
        )

    def __pow__(self, other):
        return ModInt(pow(self.x, other.x, MOD)) if isinstance(other, ModInt) else ModInt(pow(self.x, other, MOD))

    __radd__ = __add__

    def __rsub__(self, other):
        return ModInt(other.x - self.x) if isinstance(other, ModInt) else ModInt(other - self.x)

    __rmul__ = __mul__

    def __rtruediv__(self, other):
        return (
            ModInt(other.x * pow(self.x, MOD - 2, MOD))
            if isinstance(other, ModInt)
            else ModInt(other * pow(self.x, MOD - 2, MOD))
        )

    def __rpow__(self, other):
        return ModInt(pow(other.x, self.x, MOD)) if isinstance(other, ModInt) else ModInt(pow(other, self.x, MOD))


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = list(map(int, input().split()))

    a_ = map(ModInt, A)
    arr = list(accumulate(a_))
    ans = 0
    for i in range(N - 1):
        ans += A[i] * (arr[-1] - arr[i])
    print(ans)


MOD = 1000000007
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
alist = list(map(int, input().split()))
import numpy as np
Sum_a = 0
Sum_j_list = np.cumsum(alist)
for i in range(N-1):
  Sum_j = int((Sum_j_list[N-1] - Sum_j_list[i]) % int(1e9+7))
  Sum_a += (alist[i] * Sum_j) % int(1e9+7)
print(int(Sum_a % int(1e9+7)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = input()
a = []
for i in input().split():
    a.append(int(i))

MOD = 1000000007
MOD2 = 2*MOD

buf_a = 0
for x in a:
    buf_a += x
    buf_a %= MOD

buf_b = 0
for x in a:
    buf_a -= x
    buf_b += buf_a * x
    buf_b %= MOD

print(buf_b)


# ans = (buf_a * buf_a) - buf_b
# if ans < 0:
#     ans += MOD2
# ans /= 2
# ans %= MOD
# print(int(ans))




# ll ans = (sum_a * sum_a) - sum_d; // MOD^2*4 ~ -MOD*2
# if (ans < 0) ans += MOD*2; // MOD^2*4 ~ 0
# dump(ans);
# ans /= 2; // MOD^2*2
# ans = fmod(ans , MOD);
# co(ans);
# return 0;

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
summ1 = 0
answer = 0
temp = 0
summ2 = 0
summ3 = 0
for i in range(N):
    summ1 += A[i]
for l in range(N):
    summ3 += A[l]
    summ2 += A[l]*(summ1-summ3)
answer = summ2 % 1000000007
print(answer)

Submission status: Accepted

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

m = 1000000007

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

ans = ans//2
ans = ans % m

print(ans)

Submission status: Accepted

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

MOD = 10**9 + 7
A_mod = [a%MOD for a in A]

R = sum(A_mod)

ans = 0

for a in A_mod:
    R -= a
    ans = (ans + a%MOD * R%MOD)%MOD
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l=int(input())
k=list(map(int,input().split()))
s=0
mod=10**9+7
o=sum(k)
p=o**2

s=0
for i in k:
    s=(s+i**2)
    
print((p-s)//2%mod)

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split())
print((sum(l)**2-sum(i**2 for i in l))//2%(10**9+7))

Submission status: Accepted

Solution code:
import sys
import re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []


def main():
    N = i_input()
    A = list(i_map())

    mod1 = 0
    for i in range(0,N):
        mod1 = (mod1+A[i]%MOD)%MOD
    
    mod1 = (mod1 * mod1)%MOD

    mod2 = 0
    for i in range(0,N):
        mod2 = (mod2+((A[i]*A[i])%MOD)%MOD)

    mod = (mod1-mod2)%MOD
    
    answer = ((500000004 * mod))% MOD

    print(answer)
    
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

sum_A = sum(A)

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

Submission status: Accepted

Solution code:
def main():
    input()
    mod = 10**9 + 7
    arr = sorted(map(int, input().split()))
    arr.reverse()

    sm = arr[0] + arr[1]
    ans = arr[0] * arr[1] % mod

    for x in range(2, len(arr)):
        ans += sm * arr[x]
        sm += arr[x]

    print(ans % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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