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 calc(A, N, i):
    ans = 0
    ai = A[i]
    for i in range(i, N - 1):
        aj = A[i + 1]
        ans += ai * aj
    return (ans)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    mod_v = 10**9 + 7
    ans = [0] * (N + 1)
    for i in range(0, N - 1):
        if A[i] != 0:
            ans[i] = calc(A, N, i)
            break
    for j in range(i + 1, N - 1):
        if A[j] != 0:
            ans[j] = (ans[j - 1] - A[j - 1] * A[j]) // A[j - 1] * A[j]
        else:
            ans[j] = 0
    ret = 0
    for k in range(0, N):
        ret += ans[k]
    ret = ret % mod_v
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    sum_tail = sum(A)

    total = 0
    for idx, i in enumerate(A):
        sum_tail -= A[idx]
        if idx + 1 == len(A):
            break
        total += i * sum_tail
    print(total % (10**9 + 7))


def main():
    probC()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

num_sum = 0
temp_sum_num = sum(A)

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


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

print(num_sum)

Submission status: Accepted

Solution code:
n = int(input())
a_list =[int(x) for x in input().split()]
mod = 10**9 + 7

s_list = [0]*(n+1)
for i,a in enumerate(a_list):
    s_list[i+1] = s_list[i]+a


ans = 0
for i,a in enumerate(a_list):
    ans += a * (s_list[n]-s_list[i+1])
    
print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

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

print(z%1000000007)

Submission status: Accepted

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

mod = 10**9+7

tmp = 0
for i in range(N):
    tmp += A[i]
    tmp = tmp % mod
all = (tmp % mod)**2
all = all % mod

tmp = 0
for i in range(N):
    tmp += (A[i]* A[i]) % mod
di = tmp % mod

ans = all - di
ans *= (mod + 1) // 2
ans %= mod

print(ans)

Submission status: Accepted

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

prod=0

law=10**9+7

sumation=sum(S)

for i in range(N):
  sumation=sumation-S[i]
  prod=prod+(S[i]*sumation)%law

print(prod%law)

Submission status: Accepted

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

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

print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().strip().split()))
sum=0
minsum=0
for i in range(0,n):
    minsum += x[i]
for i in range(0,n-1):
    minsum-=x[i]
    sum += x[i]*minsum
    if(sum>10**9+7):
        sum%=(10**9+7)
print(sum)

Submission status: Accepted

Solution code:

N = int(input())
li = list(map(int, input().split()))
MOD_NUMBER = 10**9+7
nizyo_list = [i**2 for i in li]
ans = (sum(li)**2-sum(nizyo_list))//2
print(ans % MOD_NUMBER)

Submission status: Accepted

Solution code:
import itertools
x = 10**9 + 7
n = int(input())
a = list(map(lambda i: i % x , map(int, input().split(" "))))
sum_a = []
prev = 0
for i in a:
    sum_a.append(prev + i)
    prev = prev + i

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

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))
 
 
def main():
    n = ni()
    A = nl()
    mod = 10 ** 9 + 7
    s = sum(A)
    q = sum([a ** 2 for a in A])
    ans = (((s * s) - q)) // 2
    print(int(ans % mod))
 
 
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = list(map(int, input().split()))
#N=3
#A=[1,2,3]
q=deque(A)
#print(q)
ans=0
sums=sum(q)
#print(sums)
for i in range(N-1):
    qwe=q.popleft()
    ans+=qwe *(sums-qwe)
    sums-=qwe
print(ans%(10**9+7))

Submission status: Accepted

Solution code:
modulo=10**9+7
N=int(input())
array=[(int(x))%modulo for x in input().split()]
total=((sum(array))**2)%modulo
for i in array:
    total=((total-((i%modulo)**2)))%modulo
print((total*(5*10**8+4))%modulo)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

sigma = 0
after = sum(a[0:])
for i in range(len(a)-1):
  start = a[i]
  after = after - a[i]
  sigma = sigma + start * after
  if sigma >= (10**9 + 7):
    sigma = sigma % (10**9+7)

print(sigma)

Submission status: Accepted

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

mod = 1000000000 + 7
ans = 0

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

ans %= mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import itertools
def main():
    number = int(input())
    num_list = list(map(int, stdin.readline().split()))
    num_list_sum = sum(num_list)
    total = 0
    for i in range(number):
        num_list_sum -= num_list[i]
        total += num_list[i] * num_list_sum
    print(total % (10**9+7))
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
MOD = 1000000007
x = sum(A)
ans = 0
 
for i in range(N):
  x -= A[i]
  y = x % MOD
  z = (A[i] * y) % MOD
  ans += z
  ans %= MOD

print(ans)

Submission status: Accepted

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

mod = 10 ** 9 + 7

sum_a=0
for i in A:
    sum_a+=i
    sum_a%=mod

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

Submission status: Accepted

Solution code:
import functools
import operator

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

Submission status: Accepted

Solution code:
MOD = 1000000007

n = int(input())
str_ary = input().split()
ary = [int(c) for c in str_ary]
# ary = []
# queue = [0 for i in range(n)]
# for i int range(n):
#   num = int(str_ary[i])
#   ary.append(num)
#   for j int range(i):
#     pre_queue = []
#     queue.append(pre_queue)

result = 0
sum = sum(ary[i] for i in range(n))
# for diff in range(1, n):
#   for i in range(n - diff):
#     # print(diff, i, i + diff)
#     sum += ary[i] * ary[i + diff] % MOD
for i in range(n):
  sum -= ary[i]
  result += ary[i] * sum

print(result % MOD)

Submission status: Accepted

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

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

print(ans%mod)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり


N = I()
A = LI()
mod = 10**9+7

ans = sum(A)**2-sum(A[i]**2 for i in range(N))
ans %= mod
ans *= pow(2,mod-2,mod)
ans %= mod
print(ans)

Submission status: Accepted

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

ans = 0
sum_A = sum(A_int_list)

for i in range(len(A_int_list)):
    sum_A -= A_int_list[i]
    ans += A_int_list[i] * sum_A


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

Submission status: Accepted

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    s = sum(A)
    t = sum(a * a for a in A)
    print((s ** 2 - t) * pow(2, MOD - 2, MOD) % MOD)
resolve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 29 22:19:15 2020

@author: naoki
"""

N = int(input())

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

count1 = 0
count2 = 0
count = 10**9 +7
for i in A:
    count1+=i

for j in A:
    count2 = (j*(count1-j) + count2)%count
    count1 = count1 -j 
    
print(count2%count)

Submission status: Accepted

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

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

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

for a in A:
    tmp -= a
    sum_ += a * tmp

print(sum_ % (1_000_000_007))

Submission status: Accepted

Solution code:
N = int(input())
Alst = list(map(int, input().split()))
l = len(Alst)
Blst = [0]*l
k = 0
ans = 0
for i in range(l-1, 0, -1):
    k = (k + Alst[i])%(10**9 + 7)
    Blst[i] = k

for j in range(l-1):
    ans = (ans + Alst[j] * Blst[j+1])%(10**9 + 7)

print(ans)

Submission status: Accepted

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

z = 0
for i in range(n):
    z += a[i]
e = z % 2

y = (z ** 2) % 2000000014

x = 0
for j in range(n):
    x += a[j] ** 2
    x = x % 2000000014

w = int(((y - x) / 2) % 1000000007)
print(w)

Submission status: Accepted

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

Ans=0
S=[0]*(len(A)+1)

for i in range(0,N):
  S[i+1]=S[i]+A[i]
  #print(S[i+1])
  
#print(S[N])  
  
for i in range(0,N):
#  print(A[i]*(S[N]-S[i+1]))
  Ans+=A[i]*(S[N]-S[i+1])
  
print(Ans%(10**9+7))

Submission status: Accepted

Solution code:
N = int(input())
A_list = tuple(map(int, input().split(' ')))

# print(N)
# print(A_list)

ret = 0
now_sum = sum(A_list)
# print("スタート{}".format(now_sum))

for i in range(len(A_list)):
  now_sum -= A_list[i]
  multiple_base = A_list[i]
  # print('now_sumから{0}を引いて{1}'.format(A_list[i], now_sum))
  before_total = 0
  # print("before_totalは{0}, multiple_baseは{1}, 掛け算で{2}".format(multiple_base, before_total, multiple_base * before_total))
  ret += multiple_base * now_sum
  
print(ret % 1000000007)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
#n = (10**5)*2
#n = 10
#a = [int(10**9)]*n
mod = (10**9)+7
ans = 0
c = [0]*n
c[n-1] = a[n-1]
for i in range(n-1,1,-1) :
    c[i-1] = c[i]+a[i-1]
c.pop(0)
for i in range(0,n-1) :
    ans += (a[i]*c[i])

print(ans%mod)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
s = 0
t = 0
b = 0
m = 10 ** 9 + 7
for i in range(n):
    t += a[i]
    t = t % m
    b += a[i] ** 2
    b = b % m
s += int(((((t ** 2) % m + m - b) % m) * (5 * 10 ** 8 + 4)) % m)
print(s)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
tmp = 0
tmpura = 0
while(i < n):
    tmp += a[i]
    i+=1
tmp=tmp **2
i = 0
while (i < n):
    tmpura += a[i]**2
    i+=1
tmpura=tmpura
ans = ((tmp - tmpura)//2)%(10**9+7)
print(ans)

Submission status: Accepted

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

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

check = ans
for i in range(1, n):
    if li[i-1] == 0:
        continue
    check -= li[i] * li[i-1]
    check //= li[i-1]
    check *= li[i]
    ans += check
    ans %= mod

print(ans)

Submission status: Accepted

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

DIV = 10**9+7
ans = 0
Asum = sum(A)
Asum_sq = Asum**2

for i in range(N):
	ans += A[i]**2

res = (Asum_sq-ans)//2
print(res%DIV)

Submission status: Accepted

Solution code:
mod = 1000000000+7

def product(N, As):
  result = 0
  summer = sum(As)
  for i in range(N-1):
    left = As[i]
    summer = summer - left
    result += left * summer
    
  return result % mod


if __name__ == "__main__":
  N = list(map(int, input().split()))
  As = list(map(int, input().split()))
  print(product(N[0], As))

Submission status: Accepted

Solution code:
from collections import deque
import sys, copy, itertools,heapq
input = sys.stdin.readline
 
n = int(input())
A = list(map(int,input().split()))
 
suma = sum(A)
a = (suma ** 2)
 
b =0
for i in range(n):
  b += A[i]**2
 
ans = (a-b)//2
 
answer = ans % (10**9 +7)
 
#print(a)
#print(b)
#print(ans)
print(answer)

Submission status: Accepted

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

import numpy
A_CS = numpy.cumsum(sorted(A, reverse=True))
A_CS = A_CS%MOD

A = sorted(A)

#print(A_CS)
ans = 0
for i in range(n-1):
#    print(A[i], A_CS[-i-2])
#    A_CS -= A[i]
    ans = (ans+(A[i]*A_CS[-i-2])%MOD)%MOD
print(ans)

Submission status: Accepted

Solution code:
from sys import exit, stdin

input = stdin.readline
import copy

# from collections import deque,Counter
import numpy as np

nmod = 1000000007

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

ans = 0
tsum = 0
for i in range(N - 1, 0, -1):
    tsum += A[i]
    tsum %= nmod
    tmp = (A[i - 1] * tsum) % nmod
    ans += tmp
    ans %= nmod

print(int(ans))

Submission status: Accepted

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

s = 0
ss = 0
for i in range(n):
    j = n - i - 1
    s += (a[j] * ss % (10**9 + 7))
    s %= (10**9 + 7)
    ss += a[j]

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
res = 0
sum1 = sum(li)
sum2 = 0
m = 10**9 + 7
for i in range(0, len(li)-1):
    sum2 += li[i]
    res = (res + li[i]*(sum1-sum2))%m
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = map(int,input().split())
ans = 0
ans2 = 0
mod = 10 ** 9 + 7
for a in A:
    ans += a
    ans2 += a**2

print(((ans **2 - ans2)//2)%mod)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
A =  list(map(int, input().split()))
sa = sum(A)
r = 0
xx = sa
m = 10**9+7
for x in A:
    xx -= x
    r += x*xx
    r = r % m
print(r)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    x = [0] * N
    tot = 0
    for i in range(N-1, 0, -1):
        tot += A[i]
        x[i] = tot

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import itertools as itr
MOD=pow(10,9)+7

N=int(input())

A=list(map(int, input().split()))
sumA = list(itr.accumulate(A))
ans=0
for i in range(len(A)-1):
    a=A[i+1]
    b=sumA[i]
    ans=(ans+a*b%MOD)%MOD

print(ans)

Submission status: Accepted

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

def calc(A, n, dp):
    dp += [A[0] * A[1]]
    rsum = A[0] + A[1]
    for i in range(2, n):
        dp += [dp[-1] + rsum * A[i]]
        rsum += A[i]

    i = dp.index(max(dp))
    rsum = 0
    for j in range(i + 1):
        rsum += A[j]
    res = max(dp)
    dp2 = max(dp)
    for j in range(0, i):
        rsum -= A[j]
        dp2 -= A[j] * rsum
        res = max(dp2, res)
    return res

ans = calc(a, n, dp) % mod
print(ans)

Submission status: Accepted

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

pref = [0]*n

for i in range(n)[::-1]:
    pref[i] = (pref[i + 1] if i < n - 1 else 0) + nums[i]
    pref[i] %= MOD

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

print(ans)

Submission status: Accepted

Solution code:
# from itertools import combinations
def Sum(A, n): 
    array_sum = 0
    for i in range(0, n, 1): 
        array_sum = array_sum + A[i] 
    array_sum_square = array_sum * array_sum 
    individual_square_sum = 0
    for i in range(0, n, 1): 
        individual_square_sum += A[i] * A[i] 
    return (array_sum_square -individual_square_sum) // 2
n=int(input())
a=[int(i) for i in input().split()]
print(Sum(a,n)%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
  ans += a[i]*(zenbu-a[i])
 
print(ans // 2 % 1000000007)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
def calc_mod(n):
    return n %(10**9+7)
AA=list(map(calc_mod, A))
X=0
XX=0
for i in range(N-1):
  XX+=AA[N-i-1]
  X+=AA[N-i-2]*XX
  X=X%(10**9+7)
print(X)

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
mod = 10 ** 9 + 7


def main():
    ans = 0
    cs = np.cumsum(np.array(a))
    for i in range(n-1):
        s = (cs[-1] - cs[i]) % mod
        ans = (ans+(a[i]*int(s)) % mod) % mod
    return ans


n, *a = map(int, read().split())
a = main()
print(a)

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())
t=10**9+7
S=[a%t for a in A]
g=sum(S)

ans=0
for i in range(N):
       g-=S[i]
       ans+=S[i]*g%t
print(ans%t)

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


def mod10_9_7(num):
    return num % (10**9+7)


seki = A[-1]

ans = 0
for i in range(N-2, -1, -1):
    ans += A[i] * seki
    seki += A[i]
print(mod10_9_7(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))%(10**9+7)
print(ans%(10**9+7))

Submission status: Accepted

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

max = (10 ** 9) + 7

#一列の総和
wa = 0
for i in range (n):
    wa += a[i]

#a11, a22, ...の総和
rem = 0
for i in range(n):
    rem += a[i]*a[i]

sum = ((wa*wa) - rem) // 2

ans = sum % max

print(ans)

Submission status: Accepted

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

x = sum(A)
x = x ** 2
for i in A:
    y = i * i
    x -= y

x = x // 2
x = x % z
print(x)

Submission status: Accepted

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

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

d = 1000000000 + 7

s = 0

sum_list = [A[0]]
for a in A[1:]:
    sum_list.append(a + sum_list[-1])
for i in range(N):
    s += A[i] * (sum_list[N-1] - sum_list[i])
print(s % d)

Submission status: Accepted

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

sumA = sum(A)

ans = 0

for i in range(len(A)):
    sumA -= A[i]
    ans += sumA * A[i]

print(ans % 1000000007)

Submission status: Accepted

Solution code:
n = int(input())
t = 10**9+7
z = list(map(int,input().split() ))
_sum = 0
x = sum(z)
y = len(z)
for i in range(y-1):
    x = x-z[i]
    _sum += (z[i]*x)
        #print("_sum:"+str(_sum))
print(_sum%t)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key
from bisect import bisect_left


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


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

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

def bindex(a,x):
    i = bisect_left(a,x)
    if i != len(a) and a[i] == x:
        return i
    return None

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    N = intinput()
    indatas = list(mulinputs())

    # 出力
    # ans = ModInt(0)
    # for i in range(N):
    #     for j in range(i+1, N):
    #         a1, a2 = indatas[i], indatas[j]
    #         ans += ModInt(a1) * ModInt(a2)
    # print(ans)
    ruiseki = []
    for _ in range(len(indatas)):
        ruiseki.append(ModInt(0))
    ruiseki[0] = ModInt(indatas[0])
    for i in range(1,len(indatas)):
        ruiseki[i] = ModInt(indatas[i]) + ruiseki[i-1]
    ans = ModInt(0)
    b = ruiseki[len(ruiseki)-1]
    for i in range(len(indatas)):
        a = ModInt(indatas[i])
        b = b-a
        ans += a*b
    print(ans)

Submission status: Accepted

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

for i in range(n):
    
    keisu[i] += t
    t += a[i]


for i in range(n):
    ans += a[i] * keisu[i]

        
        

print(ans%(1000000000+7))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
MOD=1000000007
s=0
for i in range(1,N):
    s+=A[i]
    s%=MOD

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

Submission status: Accepted

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

# 拡張ユークリッドアルゴリズム
# (d, x, y): d=ax+by を満たすd, x, yを求める
# aとbが互いに素な整数であればgcd(a,b)=d=1, ax=1 (mod b)
# xは法bの元でaの乗法逆元a^-1になる
def exEuclid(a, b):
    if (b==0):
        return (a, 1, 0)
    else:
        (dd, xx, yy) = exEuclid(b, a % b)
        return (dd, yy, xx - (a//b)*yy)

def main(n,a):
    MOD=10**9+7
    sum1=0
    sum2=0
    for i in range(n):
        sum1=(sum1+a[i])%MOD
        sum2=(sum2+(a[i]*a[i]))%MOD

    aa=(sum1*sum1-sum2+MOD)%MOD

    d,x,y=exEuclid(2,MOD)
    if x<0:
        x+=MOD
    #print(x)

    return (aa*x)%MOD

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)