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**7)
def input(): return sys.stdin.readline().rstrip()


def main():
    _ = int(input())
    nums = tuple(map(int, input().split()))
    mod = 10 ** 9 + 7

    num_sum = sum(nums)
    part_sum = sum(map(lambda x: x**2, nums)) # naname

    print((num_sum**2 - part_sum) // 2 % mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = sum(A)
S2 = sum(map(lambda x:x**2,A))
 
print((S**2-S2)//2%(1000000000+7))

Submission status: Accepted

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

total = sum(data)

ans = 0
for a in data:
    total -= a
    ans += a*(total)
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A = [int(_) for _ in input().split()]
mod = 10**9 + 7
cum = [0] + list(itertools.accumulate(A))
ans = 0
for a, c in zip(A, cum):
    ans += a * c
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
mod = 10**9 + 7
n=int(input())
s=list(map(int,input().split()))
s2=sum(map(lambda x: x ** 2,s))
ans=(((sum(s))**2)-(s2))//2%mod
print(ans)

Submission status: Accepted

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

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

ans = ans % m

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sums = [0] * N
sums[0] = A[0]
for i in range(N - 1):
    sums[i + 1] = sums[i] + A[i + 1]  # sums[i] stores the sum of A0~Ai

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

print(sum % 1000000007)

Submission status: Accepted

Solution code:
N = int(input())
A_list = list(map(int, input().split()))
A_list.insert(0, 0)

cum_sum = 0
cum_sum_list = []
for i in range(N + 1):
    cum_sum += A_list[i]
    cum_sum_list.append(cum_sum)

ans = 0
for i in range(1, N+1):
    ans += A_list[i] * (cum_sum_list[N] - cum_sum_list[i])
    ans = ans % 1000000007
print(ans)

Submission status: Accepted

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

ans = 0
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:
# C - Sum of product of pairs

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

modulus = 10 ** 9 + 7

s = sum(a)
sq = sum(x * x for x in a)
print(((s * s - sq) // 2) % modulus)

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math
import collections
import copy
import decimal
import itertools
from collections import deque 
from functools import reduce
from itertools import product
from itertools import combinations
 
# input = sys.stdin.readline
N = int(input())
An = list(map(int, input().split()))
An_l = list(itertools.accumulate(An))
An_l.insert(0, 0)
ans = 0
for i, A in enumerate(An):
    tt = An_l[-1] - An_l[i+1]
#     print(i, An_l[-1], An_l[i], tt)
    At = A * tt
#     print(A, tt, At)
    ans = (ans+At) % (10**9+7)
#     print(ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

c_sum = sum(A)
mod = 10**9 + 7
ans = 0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = input()
a = l.split(' ')

tmp = 0
total2 = 0

for i in range(n):
    tmp += int(a[i])

for i in range(n):
    total2 += int(a[i])*(tmp-int(a[i]))
    tmp -= int(a[i])

print(total2%1000000007)

Submission status: Accepted

Solution code:
def main():
    input()
    array = (int(x) for x in input().split())
    mod = 10 ** 9 + 7
    ans = 0
    multiplier = 0
    for x in array:
        ans = (ans + x * multiplier) % mod
        multiplier = (multiplier + x) % mod
    return ans


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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(x) for x in input().split()]

    MOD = 10 ** 9 + 7

    ruiseki = [0] * N

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

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

    print(ans)

    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

row_sum = sum(A)
ans = 0
for i in range(N):
    ans += A[i] * row_sum - A[i]*A[i]
print(ans // 2 % mod)

Submission status: Accepted

Solution code:
def main():
    input()  # n
    array = [int(x) for x in input().split()]
    mod = 10 ** 9 + 7
    total = 0
    center_blocks = 0
    for x in array:
        total += x
        center_blocks += x ** 2
    total = total ** 2
    return (total - center_blocks) // 2 % mod


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

Submission status: Accepted

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

Submission status: Accepted

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


def main():
    n = int(input())
    A = list(map(int, input().split()))
    total = sum(A)
    mod = 10**9 + 7
    ans = 0
    for a in A[:-1]:
        total -= a
        ans += a * total
        ans %= mod

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
al = inpl()
rui = []
for i in range(n):
    if(i == 0):
        rui.append(al[i])
    else:
        rui.append(rui[i - 1] + al[i])

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

Submission status: Accepted

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

mod = 10**9 + 7

print(((sum(A)**2 - sum([i**2 for i in A]))//2) % mod)

Submission status: Accepted

Solution code:
from itertools import accumulate

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):
    tmp = acc[-1] - acc[i]
    ans += (A[i]*tmp)%mod
    ans %= mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans=sum(a)%mod
ans**=2

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

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

Submission status: Accepted

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

S = sum(A)
S2 = sum(map(lambda x: x**2, A))
print(((S**2 - S2)// 2)%M)

Submission status: Accepted

Solution code:
def l_in(type_): return list(map(type_, input().split()))
def i_in(): return int(input())
def m_in(type_): return map(type_, input().split())
def r_in(n, type_): return [type_(input()) for _ in range(n)]
const = 1000000007
ans = 0

n = i_in()
a = l_in(int)

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

print(ans%const)

Submission status: Accepted

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

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

Submission status: Accepted

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

s = sum(A)
sumpro = 0

for a in A[:-1]:
	s -= a
	sumpro += a * s

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

Submission status: Accepted

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

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

Submission status: Accepted

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

def main():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input())
    a = list(map(int, input().split()))
    b = list(itertools.accumulate(a))

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


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

Submission status: Accepted

Solution code:
#_______________________________________________________________#
def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x-1)
def lower_bound(li, num): 
	answer = -1
	start = 0
	end = len(li)-1

	while(start <= end):
		middle = (end+start)//2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer #index where x is not less than num
def upper_bound(li, num): 
	answer = -1
	start = 0
	end = len(li)-1

	while(start <= end):
		middle = (end+start)//2

		if li[middle] <= num:
			answer = middle
			start = middle + 1
		
		else:
			end = middle - 1
	return answer #index where x is not greater than num

def abs(x):
	return x if x >=0 else -x
def binary_search(li, val, lb, ub): 
	ans = 0
	while(lb <= ub):
		mid = (lb+ub)//2
		#print(mid, li[mid])
		if li[mid] > val:
			ub = mid-1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans
def kadane(x): #maximum sum contiguous subarray
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = mpos(sum_so_far,current_sum)
	return sum_so_far
def pref(li):
	pref_sum = [0]
	for i in li:
		pref_sum.append(pref_sum[-1] + i)
	return pref_sum

#_______________________________________________________________#
'''
      ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
   ▄███████▀▀▀▀▀▀███████▄
░▐████▀▒▒Aestroix▒▒▀██████
░███▀▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀████
░▐██▒▒▒▒▒KARMANYA▒▒▒▒▒▒████▌         ________________
░▐█▌▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒████▌  ? ?   |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░░█▒▒▄▀▀▀▀▀▄▒▒▄▀▀▀▀▀▄▒▒▐███▌   ?    |___CM ONE DAY___|
░░░▐░░░▄▄░░▌▐░░░▄▄░░▌▒▐███▌     ? ? |▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒|
░▄▀▌░░░▀▀░░▌▐░░░▀▀░░▌▒▀▒█▌    ? ?    
░▌▒▀▄░░░░▄▀▒▒▀▄░░░▄▀▒▒▄▀▒▌      ? 
░▀▄▐▒▀▀▀▀▒▒▒▒▒▒▀▀▀▒▒▒▒▒▒█     ? ? 
░░░▀▌▒▄██▄▄▄▄████▄▒▒▒▒█▀       ? 
░░░░▄█████████ ████=========█▒▒▐▌
░░░▀███▀▀████▀█████▀▒▌
░░░░░▌▒▒▒▄▒▒▒▄▒▒▒▒▒▒▐
░░░░░▌▒▒▒▒▀▀▀▒▒▒▒▒▒▒▐
░░░░░████████████████
'''
from math import *
#for _ in range(int(input())):
mod = 1000000007
for _ in range(1):
	n = int(input())
	#p,f = map(int,input().split())
	#d,t,s = map(int,input().split())
	#ws,wa = map(int,input().split())
	#b = list(input())
	#x = int(input())
	#n = len(b)
	#s2 = input()
	#r, g, b, w = map(int,input().split())
	a = list(map(int,input().split()))
	#b = list(map(int,input().split()))
	#s1 = list(st)
	li = pref(a)
	ans = 0
	for i in range(1,n+1):
		ans += a[i-1]*(li[-1]-li[i])
	print(ans%mod)

Submission status: Accepted

Solution code:

import collections
from functools import lru_cache
import bisect

INF = float("inf")
ZERO = 0
ONE = 1


def read():
    return input().strip()


def readInt():
    return int(input().strip())


def readList():
    return list(map(int, input().strip().split()))


def solve(N, arr):
    ans = 0
    MOD = 10**9 + 7
    totalSum = sum(arr) % MOD
    currSum = 0

    for num in arr:
        currSum += num
        ans = (ans + num * (totalSum - currSum)) % MOD

    return ans % MOD


N = readInt()
arr = readList()

print(solve(N, arr))

Submission status: Accepted

Solution code:

import collections
from functools import lru_cache
import bisect

INF = float("inf")
ZERO = 0
ONE = 1


def read():
    return input().strip()


def readInt():
    return int(input().strip())


def readList():
    return list(map(int, input().strip().split()))


def solve(N, arr):
    ans = 0
    MOD = 10**9 + 7
    prefixSums = [0]

    for num in arr:
        prefixSums.append(prefixSums[-1] + num)

    for i, num in enumerate(arr):
        ans = (ans + num * (prefixSums[-1] - prefixSums[i+1])) % MOD

    return ans % MOD


N = readInt()
arr = readList()

print(solve(N, arr))

Submission status: Accepted

Solution code:
import math

N = int(input())
lst = list(map(int, input().split(' ')))
cur_sum = sum(lst)

ans = 0
for val in lst:
    cur_sum -= val
    ans += cur_sum * val
    ans %= (math.floor(math.pow(10,9)) + 7)

ans %= (math.floor(math.pow(10,9)) + 7)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int, input().split()))
mod = 1000000007
pre = [0] * n
su = 0
total = sum(lis)
for i in range(n):
    su += lis[i]
    pre[i] = su
suf = [0] * n
for i in range(n):
    suf[i] = total-pre[i]
ans = 0
for i in range(n):
    ans = (ans + (lis[i] * suf[i]) % mod) % mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate
MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    aa = list(accumulate(A))
    #print(aa)
    x = sum(A)
    #print(x)

    ans = 0

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


# 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:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    total = sum(A) % MOD

    ans = total * total % MOD
    for a in A:
        ans = (ans - a * a) % MOD

    ans = ans * pow(2, MOD - 2, MOD) % MOD

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    total = sum(A)

    ans = 0
    for a in A:
        total -= a
        ans = (ans + a * total % MOD) % MOD

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
aa=list(map(int,input().split()))
total=0
c=sum(aa)

for i in range(n):
    c-=aa[i]
    total+=aa[i]*c
   
x=10**9+7
ans=total%x
print(ans)

Submission status: Accepted

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

sum_A = sum(A)
ans = 0
for a in A[:-1]:
  sum_A -= a
  ans += a * sum_A

ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
n = int(sys.stdin.buffer.readline())
a = list(map(int, sys.stdin.buffer.readline().split()))
aa = list(accumulate(a))
MOD = 10**9+7
ans = 0
for i in range(n):
    ans += a[i]*(aa[n-1] - aa[i]) % MOD
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
mod=1000000007
suff=[0]*n
suff[-1]=l[n-1]
for i in range(n-2,-1,-1):
    suff[i]=suff[i+1]+l[i]
s=0
for i in range(n-1):
    s=(((suff[i+1]%mod)*l[i])%mod+s)%mod
print(s)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,(input().split())))
total = 0
result = 0
MOD = pow(10,9)+7
l = []

for i in range(N):
    total += A[i]
    l.append(total)#累積和

for i,num in enumerate(l):
    temp = total-num #A[i+1]+A[i+2]+...+A[N]
    result += temp*A[i]#A[i](A[i+1]+A[i+2]+...+A[N])の総和

print(result%MOD)

Submission status: Accepted

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

ans %= mod
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    all_sum = sum(A) ** 2
    st = 0
    for i in range(N):
        st += A[i] ** 2
    print(int(((all_sum - st)//2) % 1000000007))
main()

Submission status: Accepted

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

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

sum_A = sum(A)
ans = 0
for a in A[:-1]:
  sum_A -= a
  ans += a * sum_A

ans %= mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(k)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce
mod = 10**9 + 7

n = int(input())
arr = list(map(int, input().split()))
to_mutiply = [0]*n

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

data = list(map(lambda i: (to_mutiply[i]*arr[i]) % mod, range(n-1)))
print(reduce(lambda x, y: (x+y) % mod, data))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

sum = 0
f = 0

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

Submission status: Accepted

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

cumsum = []
total = 0
for num in a:
    total+=num
    cumsum.append(total)

result = 0
MOD = pow(10,9)+7
for i,num in enumerate(cumsum):
    temp = total-num
    result += temp*a[i]
    
print(result%MOD)

Submission status: Accepted

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

# こまめにmodを取る
mod = 10**9 + 7
s = 0
t = 0
for i in range(N):
    s += A[i]
    s %= mod
    t += (A[i]**2 % mod)
    t %= mod
s = s**2 % mod
ans = (s - t) % mod
# 割り算のmodを取るときは、逆元を求め掛け算に変換してmodを取る
ans = (ans * 500000004) % mod

print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *A = map(int, read().split())

    csum = [0]
    csum.extend(accumulate(A))

    ans = 0
    for i, a in enumerate(A):
        ans = (ans + a * (csum[N] - csum[i + 1]) % MOD) % MOD

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools

n = int(input())
num_list = input().split()

mod_base = 10 ** 9 + 7

num_list = [int(x) for x in num_list]

sum_list = []
sum_tmp = sum(num_list)
for i in range(len(num_list)):
    sum_list.append(sum_tmp)
    sum_tmp -= num_list[i]

all_sum = 0
for index, num in enumerate(num_list):
    if index + 1 >= len(num_list):
        break
    all_sum += num * sum_list[index+1]

print(all_sum % mod_base)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
s, t = 0, 0
for i in range(N):
  s += A[i]
  t += A[i]**2
s = s**2

# 割り算のmodを求めるため、逆元を求め、積に変換する
ans = (s-t) % (10**9+7)
# tmp = (2**1000000005) % (10**9+7)
tmp = 500000004
ans = (ans*tmp) % (10**9+7)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
 
s, t = 0, 0
for i in range(N):
  s += A[i]
  t += A[i]**2
s = s**2

ans = (s-t) % (10**9+7)
# tmp = (2**1000000005) % (10**9+7)
tmp = 500000004
ans = (ans*tmp) % (10**9+7)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
s2=int(sum(a)*sum(a))
a2=[int(i*i) for i in a]
# ans1=int((s2-sum(a2))/2)
gyakugen=pow(2,10**9+5,10**9+7)
ans1=(s2-sum(a2))*gyakugen
print(int(ans1%(10**9+7)))

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

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

S1 = sum(A)
S2 = sum(a * a for a in A)

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

Submission status: Accepted

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

sum=0
f=0

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

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

Submission status: Accepted

Solution code:
try:
    n=int(input())
    m=10**9+7
    arr=[int(i) for i in input().split()]
    sm=sum(arr)%m
    ans=0
    for i in range(n-1):
        sm-=arr[i]
        x=arr[i]*sm
        ans+=x
        #print(x)
    print(ans%m)
except  EOFError as e:
    print(e)

Submission status: Accepted

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

B = []
tmp = 0
for i in range(n):
    tmp += A[n-i-1]
    tmp %= mod
    B.append(tmp)

sum = 0
for i in range(n-1):
    sum += A[i]*B[n-i-2]
    sum %= mod
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

mod = 10**9+7

x = (((sum(A))**2) // 2)
x -= (sum([a**2 for a in A]) // 2)
x %= mod
print(x)

Submission status: Accepted

Solution code:
n=int (input())
a= list(map(int,input().split()))
s2=int(sum(a)*sum(a))
a2=[int(i*i) for i in a]
ans1=(s2-sum(a2))//2

print(int(ans1%((10**9)+7)))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
def resolve():
    import sys
    input = sys.stdin.readline
 
    n = input().rstrip()
    past_a = 0
 
    ans = 0
    for a in input().rstrip().split(" "):
        a = int(a)
        #print(a)
        ans += a * past_a
        #print(ans)
        past_a += a
        #print(past_a)
 
    x = 1000000007
 
    ans = ans % x
 
    print(ans)
 
if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:

if __name__ == "__main__":
	MOD = 10 ** 9 + 7
	N=int(input())
	A=list(map(int,input().split()))

	Asum = sum(A) % MOD

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

	print(ans % MOD)

Submission status: Accepted

Solution code:
import math

class BitIndexedTree:
	'''NOTES: 1-origin-indexed
	'''
	def __init__(self, itemCount):
		self.itemCount = itemCount
		self.items = [0 for i in range(itemCount+1)]

	def add(self, i,value):
		while i <= self.itemCount:
			self.items[i] += value
			i += (i & (-i))

	def sumFromStart(self, end):
		summary = 0
		i = end
		while i > 0:
			summary += self.items[i]
			i -= (i & (-i))
		return summary

	def sum(self, start,end):
		summary = self.sumFromStart(end) - self.sumFromStart(start-1)
		return summary

if __name__ == "__main__":
	MOD = 10 ** 9 + 7
	N=int(input())
	A=list(map(int,input().split()))

	BIT=BitIndexedTree(N)

	for n in range(N):
		BIT.add(n+1, A[n])

	ans = 0
	for n in range(N-1):
		# print(A[n],BIT.sum(n+2,N),A[n] * BIT.sum(n+2,N))
		# print(MOD)
		ans += ((A[n] * BIT.sum(n+2,N)) % MOD)


	print(ans % MOD)

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:
if __name__ == '__main__':

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

    #全体
    sm = sum(A) ** 2
    #共通部分
    com = sum([i * i for i in A])

    ans = (sm - com) // 2
    print(ans%(10**9+7))

Submission status: Accepted

Solution code:
n = int(input())

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

sum_A = sum(A)

total = 0

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

Submission status: Accepted

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

mod = 10 ** 9 + 7
ans = 0

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

cumsum = [0] + list(accumulate(a))
sum_a = cumsum[-1]
mod = 10**9 + 7
res = 0
for i in range(n):
    res += a[i] * (sum_a - cumsum[i+1])
    res %= mod
print(res)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
x = [*l]
ans = 0
mod = 1000000007
for i in range(1,n):
  l[i] += l[i-1]
i = 0
while i<n-1:
  ans += (x[i])*(l[n-1] - l[i])
  i += 1
print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

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


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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
l=[int(i) for i in input().split()][:N]
arr=[-1]*N
arr[0]=l[0]
for i in range(1,N):
    arr[i]=arr[i-1]+l[i]
cnt=0
for i in range(N-1):
    cnt+=(l[i]*(arr[-1]-arr[i]))%1000000007
print(cnt%1000000007)

Submission status: Accepted

Solution code:
import itertools

n=int(input())
a_list=list(map(int,input().split()))
sum=0
ps = list(itertools.accumulate(a_list))
for i in range(n-1):
  sum+=a_list[i]*(ps[n-1]-ps[i])

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
nums = [int(x) for x in input().split()]
multinums = [0]
total = 0

for i in range(0,n):  #累積和配列を作成する
    multinums.append(multinums[i]+nums[i])

#print(multinums)

for i in range(0,n):
    #print(nums[i],multinums[n]-multinums[i+1],nums[i] * (multinums[n]-multinums[i+1]))
    total += nums[i] * (multinums[n]-multinums[i+1])

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

Submission status: Accepted

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

for i in range(N):
  S -= A[i]
  P += A[i]*S
  
ans = P%m
print(ans)