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 s():return int(input())
def mp():return map(int,input().split())
a = s()
b = list(mp())
ans=0
t = sum(b)%(10**9+7)
for i in range(a):
	t-=b[i]
	ans+=b[i]*t
	ans%=(10**9+7)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum_a = 0
sum_aa = 0
for a in aaa:
  sum_a += a
  sum_aa += a ** 2
  
print(((sum_a ** 2 - sum_aa) // 2) % (10 ** 9 + 7))

Submission status: Accepted

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

for ai in A:
    ans+=(ai*(s-ai))
print((ans//2)%(10**9+7))

Submission status: Accepted

Solution code:
import math
import sys
#####sorting a dictionary by the values#####
def dict_sort(ans):
    ans=sorted(ans.items(),reverse=True,key=lambda kv:(kv[1]))
##### naive method for testing prime or not#####
def is_prime(n):
    if n==1:
        return 0
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
#####swap function#####
def swap(a,b):
    temp=a
    a=b
    b=temp
    return a,b
#####Primes till Nth#####
def seive_primes(n):
    flag=[0]*(n+10)
    flag[1]=flag[0]=1
    i=2
    while i*i<=n+1:
        if flag[i]==0:
            j=i*i
            while j<=n+1:
                flag[j]=1
                j+=i
        i+=1
    return flag
#####function that return all combination of sum(i^n)=x#####
def checkRecursive(x, n, curr_num=1, curr_sum=0): 
    results = 0 
    p = curr_num**n 
    while(p + curr_sum < x):  
        results += checkRecursive(x, n, curr_num + 1, p + curr_sum) 
        curr_num = curr_num + 1
        p = curr_num**n  
    if(p + curr_sum == x): 
        results = results + 1
    return results
#####number of minimum swaps required to sort the array#####
def findMinSwap(arr, n): 
    vec = [] 
    for i in range(n): 
        vec.append([arr[i], i])  
    vec = sorted(vec) 
    ans, c, j = -1, 0, 0
    for i in range(n):  
        if(vec[i][1] == i): 
            continue
        else:  
            vec[i][0], vec[vec[i][1]][1] = swap(vec[i][0],vec[vec[i][1]][1])
            vec[i][1], vec[vec[i][1]][1] = swap(vec[i][1],vec[vec[i][1]][1]) 
        if(i != vec[i][1]): 
            i -= 1
        ans += 1
    return ans
#####greatest common divisor of two numbers#####
def gcd(a,b):
    if b==0:
        return a
    return gcd(b,a%b)
#####least common multiplyer of two numbers#####
def lcm(a,b):
    return (a*b)//gcd(a,b)
#####function that return all the letters#####
def alphabates():
    return "abcdefghijklmnopqrstuvwxyz"
#####binary search#####
def binary_search(ls,n,flag):
    low=0
    hi=n-1
    while(low<=hi):
        mid=(low+hi)//2
        if ls[mid]==flag:
            return mid
        elif ls[mid]>flag:
            hi=mid-1
        else:
            low=mid+1
    return -1
#####Decimal to Binary(8 bits)#####
def Binary(n):
    if n==0:
        return "00000000"
    binary = "" 
    i = 0
    while n > 0 and i<=8: 
        s1 = str(int(n%2)) 
        binary = binary + s1 
        n /= 2
        i = i+1
        d = binary[::-1] 
    return d
#####quadratic#####
def qdrt(x,a,b,c):
    return a*x*x+b*x+c
#####taking an array/list as input#####
def inp():
    ls=list(map(int,input().split()))
    return ls
#####taking multiple inputs#####
def mult_inp():
    return map(int,input().split())
#####0-1 Knapsack#####
def knapSack(W, wt, val, n): 
    K = [[0 for x in range(W + 1)] for x in range(n + 1)]  
    for i in range(n + 1): 
        for w in range(W + 1): 
            if i == 0 or w == 0: 
                K[i][w] = 0
            elif wt[i-1] <= w: 
                K[i][w] = max(val[i-1]+K[i-1][w-wt[i-1]],  K[i-1][w]) 
            else: 
                K[i][w] = K[i-1][w] 
  
    return K[n][W] 
#####Main function starts from here#####
n=int(input())
ls=inp()
ans=sum(ls)**2
sq=0
for i in range(n):
    sq+=ls[i]**2
print(((ans-sq)//2)%1000000007)

Submission status: Accepted

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

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

Submission status: Accepted

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

mod = 10**9 + 7

a_sum = sum(a_list)

ans = 0
for i in range(0, n):
    ans += (a_list[i] * (a_sum - a_list[i]))
ans = ans // 2
print(ans % mod)

Submission status: Accepted

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

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

ans = 0
mod = 10 ** 9 + 7

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

Submission status: Accepted

Solution code:
#C
N = int(input())
A = list(map(int, input().split()))
result = sum(A)**2
for i in A:
    result -= i**2
print((result//2)%1000000007)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(A)
result = 0

MOD = 10**9 + 7

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.reverse()
    
    temp = 0
    a_sum = []

    for i in range(len(a)):
        temp += a[i]
        a_sum.append(temp)

    total = 0
    a_sum.reverse()
    a.reverse()

    for i in range(1, len(a_sum)):
        total += a[i-1]*a_sum[i]
    
    print(total % (10**9+7))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

temp=sum(A)
ans=0

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

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

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

Submission status: Accepted

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

Submission status: Accepted

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

tmp = 0
li = []
for i in range(n - 1, 0, -1):
    tmp += a[i]
    li.append(tmp * a[i - 1])

print(sum(li) % mod)

Submission status: Accepted

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

for i in range(1, n):
    res = (res + ((sum_a % mod) * (a[i] % mod)) % mod) % mod
    sum_a = (sum_a + a[i]) % mod

print(res % mod)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7
A.sort()
ans = 0
sumA = sum(A)
for i in range(N):
    sumA -= A[i]
    for j in range(i+1,N):
        if A[i] > A[j]:
            continue
        ans += (A[i] * (sumA % MOD)) %MOD
        break

print(ans % MOD)

Submission status: Accepted

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

suma = [0]

for aa in a:
    suma.append((suma[-1] + aa)%MOD)

ans = 0
for i, aa in enumerate(a[:-1]):
    ans += (aa % MOD) * ((suma[-1] - suma[i+1])%MOD)
    ans %= MOD
print(ans)

Submission status: Accepted

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

n=int(input())

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

B=[A[0]]

for i in range(len(A)-1):
    B.append((B[i]+A[i+1]) % (10**9+7))

ans=0

for i in range(n-1):
    
    ans += (A[i] * (B[-1]-B[i])  % (10**9+7))
    
#    for j in range(i+1,n):
        
#        ans += (A[i] * A[j]) % (10**9+7)
#
print(ans % (10**9+7))

Submission status: Accepted

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

Submission status: Accepted

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

e = s * s 

ss = 0
for i in range(n): 
    ss += a[i] * a[i] 
p =  (e - ss) // 2
print(p % (10**9 + 7))

Submission status: Accepted

Solution code:
from itertools import accumulate

n = int(input())
a = list(map(int, input().split()))
s = list(accumulate(a))
MOD = 10 ** 9 + 7

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sm=0
sumj=sum(a)

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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 0
a_sum = 0
for i in range(len(A)):
    ans += A[i] * a_sum
    a_sum += A[i]
ans = ans % 1000000007
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
MOD = 10 ** 9 + 7

sum_mod = 0
for v in A:
    sum_mod += v
    sum_mod %= MOD

ans = 0
for v in A:
    sum_mod -= v
    if sum_mod < 0:
        sum_mod = MOD + sum_mod
    ans += ((v * sum_mod) % MOD)
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
N = int (input ())
A = [int (x) for x in input().split()]
S = sum (A)
X = 0
for i in range (N-1):
  X += A[i]*(S-A[i])
  S -= A[i]
  if X > 10**9+7:
    X = X%(10**9+7)
print (X)

Submission status: Accepted

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

mod = 1000000007

sq_line=[i*i for i in line]

s=sum(line)
t=sum(sq_line)

print(((s*s-t)//2)%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!python3.8
# -*- coding: utf-8 -*-
# abc177/abc177_c
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [i2s() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

P = 10**9 + 7

def main():
    N = i2n()
    A = i2nn()
    B = [None] * (N - 1)
    B[-1] = A[-1]
    for i in range(2, N):
        B[-i] = (A[-i] + B[-i+1]) % P
    ans = 0
    for i in range(N - 1):
        ans += (A[i] * B[i]) % P
    print(ans % P)
    return

main()

Submission status: Accepted

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

sum_value = sum(A)
m = 10**9 + 7

answer = 0
for a in A:
    sum_value -= a
    answer += ((a * sum_value) % m)

print(answer % m)

Submission status: Accepted

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

A_2 = 0
for i in A:
    A_2 += i**2
    
ans = ((sum(A)**2 -A_2)//2)%(10**9 + 7)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

def main():
    import sys
    input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

K = 1000000007

sumA = 0
for a in A:
    sumA += a
    sumA = sumA
    #print(sumA)

sumA2 = (sumA * sumA)

sumAA = 0
for a in A:
    sumAA += a * a
    #print(sumAA)
    sumAA = sumAA
    #print(sumAA)

X = (sumA2 - sumAA) // 2 % K

print(X)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

square = 0
for i in A:
    square += i*i
ans = ((sum(A)*sum(A) - square) // 2)%(10**9+7)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

mod = pow(10, 9) + 7

sum_array = [a[-1]]
for i in range(n - 1):
    sum_array.append(sum_array[i] + a[n - i - 2])

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

ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def solve():
    MOD = 10**9+7
    N = int(input())
    A_L = tuple(int(x) for x in input().split())
    cum_a_l = [0] * N
    cum_a_l[0] = A_L[0]
    for i in range(1, N):
        cum_a_l[i] = (cum_a_l[i-1] + A_L[i]) % MOD
    ans = 0

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


solve()

Submission status: Accepted

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

mod = 10**9 + 7
s = sum(l)
res = 0

while len(l) > 1:
    i = l.pop()
    s -= i
    res += i * (s%mod)
    res %= mod
    
print(res)

Submission status: Accepted

Solution code:
# Date [ 2020-08-29 21:10:46 ]
# Problem [ c.py ]
# Author Koki_tkg

import sys
# import math
# import bisect
# import numpy as np
# from decimal import Decimal
# from numba import njit, i8, u1, b1 #JIT compiler
# from itertools import combinations, product
# from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_ints2(x):   return map(lambda num: int(num) - x, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def GCD(a: int, b: int) -> int: return b if a%b==0 else GCD(b, a%b)
def LCM(a: int, b: int) -> int: return (a * b) // GCD(a, b)

def Main():
    n = read_int()
    a = read_int_list()
    
    ans = 0

    total = sum(a)
    floatcnt = 0
    for i in range(n):
        ans += a[i] * (total - a[i])
        ans %= MOD
        total -= a[i]

    print(int(ans % MOD))

if __name__ == '__main__':
    Main()

Submission status: Accepted

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

M = 10**9+7

entire = sum(A) % M

ans = 0
s = 0
for a in A:
    entire -= a
    entire %= M
    ans += (a*entire)%M
    ans %= M
print(ans)

Submission status: Accepted

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

mod = 10 ** 9 + 7

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

arr_sum_square = (arr_sum * arr_sum) % mod

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate
n = int(input())
a = list(map(int, input().split()))
rev = list(reversed(a))
acc = list(accumulate(rev))
ans = 0
for i in range(n-1):
    ans += acc[i]*a[n-2-i]
print(int(ans%(1000000000+7)))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
  aa[i] = A[i]**2
X = sum(A)**2 - sum(aa)
print((X//2)%inf)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n):
  ret += A[i] * (S[-1]-S[i])
  ret = ret % MOD
print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
s = 0

mod = 10**9 + 7

def add(a, b):
    return (a + b) % mod

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

for i in reversed(range(N-1)):
    s = add(s, A[i+1])
    ans = add(ans, mul(A[i], s)) 
    
print(ans)

Submission status: Accepted

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

temp = arr_sum * arr_sum  
temp1 = 0

for i in range(0, n, 1): 
    temp1 += arr[i] * arr[i] 
ans=((temp - temp1)//2)%mod
print(ans)

Submission status: Accepted

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

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

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

print(ans % mod)

Submission status: Accepted

Solution code:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy

import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class Point():
    def __init__(self,y,x):
        self.x=x
        self.y=y
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    from queue import PriorityQueue
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

def to10(A):
    ret=0
    for a in range(len(A)-1,-1,-1):
        ret+=X[a]*2**(N-1-a)
    return ret
######################################################################################################
N=int(input())
A=inputintlist()
S=sum(A)
S%=mod
ans=0
for i in range(N):
    s=(S-A[i]+mod)%mod
    ans+=s*A[i]
    ans%=mod
ans=ans*pow(2,mod-2,mod)%mod
print(ans)

Submission status: Accepted

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

mod=(10**9)+7

goukei=0
S=[]
tmp=0
for i in range(N):
    tmp+=T[i]
    S.append(tmp)

for i in range(N-1):
    
    #print(T[i]*(S[N-1]-S[i]))
    goukei+=(T[i]*(S[N-1]-S[i]))
goukei=goukei%mod
print(goukei)

Submission status: Accepted

Solution code:
mod = 1000000007

N = int(input())

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

S = [0]*N

for i in range(N):
    j = N-1 - i

    if i == 0:
        S[j] = 0
    else:
        S[j] = S[j+1] + A[j+1]

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

acum = list(accumulate(A))

ans = 0
for i in range(n-1):
    ans += (A[i] * (acum[-1] - acum[i]))%rem
    ans  %=  rem
print(ans)

Submission status: Accepted

Solution code:
import copy
n = int(input())
a = list(map(int,input().split()))
a_2 = copy.copy(a)
a_2.reverse()
sinu = [0] * n
t = 0
for i in range(n):
    t += a_2[i]
    sinu[i] += t
sinu.reverse()

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

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

Submission status: Accepted

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

ans = 0

sum_j = [0] * n
sum_tmp = 0

for i in reversed(range(n)):
    sum_tmp += a[i]
    sum_j[i] = sum_tmp

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

print(ans % mod)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
m = 10**9+7
s = sum(A)
cnt = 0
#print(mod_A)
for i in range(N) :
    s = s - A[i]
    cnt += A[i] * s
    cnt = cnt % m

print(cnt)

Submission status: Accepted

Solution code:
(n,),a = [list(map(int, s.split())) for s in open(0)]
s2 = 0
mod = int(1e9+7)
inv_2 = pow(2, mod-2, mod)
for x in a:
    s2 += pow(x, 2, mod)
    s2 %= mod
a2 = pow(sum(a), 2, mod)
ans = ((a2 - s2) * inv_2) % mod
print(ans)

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
a = LI()
MOD = 10**9 + 7
sum_ai = a[0]
ans = 0
for ai in a[1:]:
    ans += ai * sum_ai
    ans %= MOD
    sum_ai += ai
print(ans)

Submission status: Accepted

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

    lst = [sum(A)]
    for j in A:
        lst.append(lst[-1] - j)
    ans = 0
    for index, i in enumerate(A):
        ans += i * lst[index + 1] % mod
    print(ans % mod)


if __name__ == '__main__':
    main()

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