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
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
from itertools import accumulate as acc
MOD=10**9+7
#======================================================#
def main():
    n = II()
    aa = MII()
    aa_cum = list(acc(aa))
    ans = 0
    for i in range(n):
        mul = aa_cum[-1] - aa_cum[i]
        ans += (aa[i]*mul)%MOD
    print(ans%MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
N = int(input())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

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

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

Submission status: Accepted

Solution code:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy
import numpy as np
n = int(input())
#d, t, s = map(int, sys.stdin.readline().split())
#s = input()
a = list(map(int, sys.stdin.readline().split()))

mod = 10 ** 9 + 7

sumlist= sum(a)
x = []
for i in range(n):
    x.append(sumlist - a[i])
    sumlist -= a[i]

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

print(ans)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from math import gcd, factorial, ceil, floor, sqrt
from bisect import bisect_left, bisect_right
from copy import deepcopy
from heapq import heapify, heappop, heappush
from itertools import permutations, combinations, product, accumulate
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(10**7)

ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(map(int, input().split()))

N = ii()
A = li()
mod = 10**9+7

ans = 0
m = list(accumulate(A))
for i, x in enumerate(m[:-1]):
    ans += ((A[i]%mod) * ((m[-1] - m[i])%mod))%mod
    ans %= mod
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

p = 10 ** 9 + 7

s = sum(A) % p
X = [s]
for i in range(N):
    s = s + p - A[i]
    s %= p
    X.append(s)
# print(X)

ans = 0
for i in range(N-1):
    #for j in range(i+1, N):
    #    ans += A[i] * A[j]
    #    ans %= p
    ans += A[i] * X[i+1]
    ans %= p
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = [int(x) for x in input().split(' ')]

pre = []

for x in arr:
    if len(pre) == 0:
        pre.append(x)
    else:
        pre.append(pre[len(pre)-1] + x)
        
ans = 0
mod=int(1e9+7)
for i in range(n):
    s = (pre[n - 1] - pre[i]) * arr[i]
    ans = ((ans % mod) + s % mod) % mod
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import sys,io
import math
from collections import deque
from heapq import heappush, heappop

input = sys.stdin.buffer.readline

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7
#DIV = 998244353

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As,mod=DIV):
    s = 0
    for a in As:
        s += a
        if mod !=None:
            s = s%mod
        yield s


sys.setrecursionlimit(200005)


def dijkstra(G,start=0):
    heap = []
    cost = [INF]*len(G)
    heappush(heap,(0,start))
    while len(heap)!=0:
        c,n = heappop(heap)
        if cost[n] !=INF:
            continue
        cost[n]=c
        for e in G[n]:
            ec,v = e
            if cost[v]!=INF:
                continue
            heappush(heap,(ec+c,v))
    return cost


def main():
    N = iRaw()
    As = isRaw()
    sumAs = list(cumsum(As,DIV))
    ans = 0
    for aidx in range(len(As)-1):
        ans = (ans+As[aidx]*(sumAs[-1]-sumAs[aidx]))%DIV
    print(ans)

main()

Submission status: Accepted

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
mod=10**9+7
def do():
    n=INT()
    A=LIST()
    B=[0]*(n)
    B[0]=sum(A[1:])
    ans=0

    for i in range(1,n):
        B[i]=(B[i-1]-A[i])%mod

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

    print(ans)

    

    
if __name__=='__main__':
    do()

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

P = 10**9 + 7
k = 3
inv_t = [0]+[1]
for i in range(2,k):
  inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

N = int(input())
A = list(map(int, input().split()))
AA = [A[i]**2 % mod for i in range(N)]

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

ans = (ans**2) % mod
ans = (ans - b) % mod

ans = (ans*inv_t[2]) % mod
print(ans)

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

# N -> 1の累積和
acum = [0 for _ in range(N+1)]
acum[0] = sum(A)
for i in range(N):
    acum[i+1] = acum[i] - A[i]

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    inlis = list(map(int, input().split()))

    total = sum(inlis)

    tmp = 0

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import os
import heapq
import sys,threading
import math
import bisect
import operator
from collections import defaultdict
sys.setrecursionlimit(10**5)
from io import BytesIO, IOBase
def gcd(a,b):
    if b==0:

        return a
    else:
        return gcd(b,a%b)
def power(x, p,m):
    res = 1
    while p:
        if p & 1:
            res = (res * x) % m
        x = (x * x) % m
        p >>= 1
    return res
def inar():
    return [int(k) for k in input().split()]

def lcm(num1,num2):
    return (num1*num2)//gcd(num1,num2)

def main():
    #t=int(input())
    t=1
    for _ in range(t):

        n=int(input())
        arr=inar()
        dp=[0]*n
        for i in range(n-1,-1,-1):
            if i==(n-1):
                dp[i]=arr[i]
                continue
            dp[i]=arr[i]+dp[i+1]
        #print(dp)
        ans=0
        mod=10**9+7
        for i in range(n-1):
            ans+=(arr[i]*dp[i+1])% mod
        print(ans%mod)




BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()
    #threadin.Thread(target=main).start()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = int(1e9 + 7)

def pw(b, ex):
    if ex == 0: return 1
    if ex == 1: return b
    if ex & 1: return b % mod * pw((b*b) % mod, ex >> 1) % mod
    return pw((b*b) % mod, ex >> 1) % mod

n = int(input())
x = list(map(int,input().split()))
st = 0
en = 0

for i in x:
    st = (st + i) % mod
    en = (en + (i * i) % mod) % mod
    
st *= st
ans = int((st - en + mod) % mod)
ans = int(ans * int(pw(2, mod - 2)) % mod) % mod
print (int(ans))

Submission status: Accepted

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

MOD = 1000000007

cum = []

s = 0
for a in A:
    s += a
    cum.append(s)


ans = 0
for i, a in enumerate(A):
    c = a * ((s - cum[i]) % MOD) % MOD
    ans += c % MOD
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
numbers = list(map(lambda x: int(x), input().split()))

sum_n = sum(numbers)
total_value = 0

for current_num in numbers:
    sum_n -= current_num
    total_value += current_num * sum_n
#     print(sum_n, total_value, current_num)

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

Submission status: Accepted

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

Submission status: Accepted

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

R=[S-L[0]]
for i in range(1,N):
    R.append(R[-1]-L[i])
#print(R)

mod=10**9+7
ans=0
for i in range(N):
    ans += L[i]*R[i]
    ans%=mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

MOD = 10 ** 9 + 7

sum_A = sum(A)
a_a = [(a * a) for a in A]

ans = ((sum_A ** 2)- sum(a_a)) // 2

print(ans % MOD)

Submission status: Accepted

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

proA = map(lambda x: x**2, A)

S = ((sum(A)**2-sum(proA))//2)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():

    N = I()
    A = LI()
    mod = 10 ** 9 + 7
    sum_ = 0
    D = []
    for i in range(N):
        a = A[i]
        sum_ += a
        D.append(a ** 2)
    ans = (sum_ ** 2 - sum(D)) // 2
    print(ans % mod)










if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:


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


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


def solve():
    N = read_int()
    A = read_ints()
    S = A[0]
    answer = 0
    modulo = 10**9+7
    for i in range(1, N):
        answer = (answer+A[i]*S)%modulo
        S = (S+A[i])%modulo
    return answer


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

Submission status: Accepted

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

mod = 10**9 + 7

ans = sum(A) * sum(A) % mod
ans += mod - (sum([a*a for a in A]) % mod)
ans %= mod
if(ans%2==0):
    ans //= 2
else:
    ans = (ans + mod) // 2
    ans %= mod
print(ans)

Submission status: Accepted

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

ans = 0
sum_a = sum(A)
for a in A:
  ans = (ans + (sum_a - a)*a)%mod

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

Submission status: Accepted

Solution code:
MODULO = 1_000_000_007


def main():
    N = int(input())
    A = [int(_x) for _x in input().split()]
    sums = [0]*N
    for i in reversed(range(N)):
        if (i == N-1):
            sums[i] = A[i]
        else:
            sums[i] = sums[i+1] + A[i]

    result = 0
    for i in range(N):
        if i == N-1:
            break
        result = (result + A[i] * sums[i+1]) % MODULO
    print(result)


main()

Submission status: Accepted

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

for p in range(N):
    Asum += A[p]
    A2sum += A[p]**2


print(((Asum**2-A2sum)//2)%(10**9+7))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
a=sum(l[1:])
m=10**9+7
s=0
for v,u in zip(l[:-1],l[1:]):
    s+=v*a
    a-=u
print(s%m)

Submission status: Accepted

Solution code:
import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

N = INT()
A = LIST()

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

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

MOD = 10**9+7

sum_j = 0
for v in a:
    sum_j += v
    sum_j %= MOD

ans = 0
for v in a:
    ans += v * ((sum_j - v) % MOD)
    ans %= MOD

# 重複削除
class FLT:
    """
    フェルマーの小定理
    a^(-1) = a^(m-2) mod p
    """
    def __init__(self, mod=10**9+7):
        self.mod = mod

    def rep_sqr(self, base, k):
        ans = 1
        while k > 0:
            if k & 1:
                ans = ans * base % self.mod
            base = base * base % self.mod
            k >>= 1
        return ans

    def inv(self, a):
        """ 逆元を取る """
        return self.rep_sqr(a, self.mod-2)

flt = FLT()

ans = ans * flt.inv(2)
ans %= MOD
print(ans)

Submission status: Accepted

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

MOD = pow(10, 9) + 7

two_inv = pow(2, -1, MOD)

sq = []

for x in a:
    sq.append(x ** 2 % MOD)

s = sum(a) % MOD
s_sq = sum(sq) % MOD

print((pow(s, 2, MOD) - s_sq) * two_inv % MOD)

Submission status: Accepted

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


mod=pow(10,9)+7

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

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

Submission status: Accepted

Solution code:

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

S = sum(A) % MOD
T = sum([a*a for a in A]) % MOD

ans = (S*S - T) % MOD
if ans % 2 == 1:
	ans += MOD
print(ans // 2)

Submission status: Accepted

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

inv_A=A[::-1]

b = [0]*N
b[0]=inv_A[0]
for i in range(1,N):
    b[i] = (b[i-1]+inv_A[i])%mod
inv_b = b[::-1]

print( sum(A[i]*inv_b[i+1]%mod for i in range(0,N-1))%mod )

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
ans = 0
cnt = a[0]
for i in range(1, n):
    ans += cnt * a[i]
    ans %= mod
    cnt += a[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
an=0;md=10**9+7
suf=[0]*n
suf[-1]=l[-1]
for i in range(n-2,-1,-1):
  suf[i]=suf[i+1]+l[i]
for i in range(n-1):
  an=(an+l[i]*suf[i+1])%md
print(an)

Submission status: Accepted

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

print(kotae%mod)

Submission status: Accepted

Solution code:
import os
import sys
import math
import heapq
from decimal import *
from io import BytesIO, IOBase
from collections import defaultdict, deque

def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

mod=1000000007
n = r()
a = rl()
b = [a[-1]]
for i in range(n-2,-1,-1):
    b.append(b[-1]+a[i])
b = b[::-1]
ans = 0
for i in range(n-1):
    ans= (ans+(a[i]*b[i+1])%mod)%mod
print(ans)

Submission status: Accepted

Solution code:
import os
import heapq
import sys,threading
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase

sys.setrecursionlimit(10 ** 5)
threading.stack_size(2**27)


def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)


"""def pw(a,b):
    result=1
    while(b>0):
        if(b%2==1): result*=a
        a*=a
        b//=2
    return result"""


def inpt():
    return [int(k) for k in input().split()]


def main():
    n=int(input())
    ar=inpt()
    sm=0
    ans=0
    md=10**9+7
    for i in range(n-1):
        sm+=ar[i]
        temp=(sm%md * ar[i+1]%md)%md
        ans=(ans+temp)%md
    print(ans)






BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

if __name__ == "__main__":
    main()
    #threading.Thread(target=main).start()

Submission status: Accepted

Solution code:
from itertools import accumulate
n=int(input())
A=[int(i) for i in input().split()]

B=list(accumulate(A))

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

Submission status: Accepted

Solution code:
import sys

if sys.subversion[0] == "PyPy":
    import io, atexit

    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))

    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()
RS = raw_input
RA = lambda x=int: map(x, RS().split())
RN = lambda x=int: x(RS())


def solve():
    m = 10 ** 9 + 7
    s = sum(a) % m
    ans = 0
    running = 0
    for i in range(n):
        running += a[i]
        ans += (a[i] * (s - running))
        ans %= m
    ans %= m
    print(ans)
    return



n = RN()
a = RA()
solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
INF = float('inf')
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    A = NLI()
    
    sum_A = sum(A)
    ans = 0
    
    for n in range(N):
        sum_A -= A[n] 
        ans += (A[n] * sum_A)%MOD
        ans = ans % MOD
    print(ans)
        



if __name__ == '__main__':
    main()

Submission status: Accepted

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

s = sum(l)%mod
ans = 0

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
num=int(input())
date=list(map(int,input().split(" ")))
sum0=0
sum1=0
for i in range(num):
    sum0+=date[i]
for i in range(num):
    sum0-=date[i]
    sum1+=date[i]*sum0
print(sum1%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
mod = int(1e9+7)
n=int(input())
L=list(map(int,input().split()))
total = 0
hb = sum(L)
val = 0
for i in range(n-1):
    val +=L[i]
    total += L[i]*(hb-val)
print(total%mod)

Submission status: Accepted

Solution code:
'''
Created on 2020/08/29

@author: harurun
'''
def main():
  import itertools
  import sys
  pin=sys.stdin.readline
  pout=sys.stdout.write
  perr=sys.stderr.write
  
  N=int(pin())
  A=list(map(int,pin().split()))
  mod=10**9+7
  B=list(itertools.accumulate(A))
  ans=0
  for i in range(N-1):
    ans+=A[i]*(B[N-1]-B[i])
  print(ans%mod)
  return 
main()

Submission status: Accepted

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

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

print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.reverse()
cum = []
cumsum = 0
for i in A:
    cum.append(cumsum+i)
    cumsum+=i
ans = 0
for j in range(N-1):
    ans += A[N-j-1]*cum[N-j-2]
    ans = ans%1000000007
print(ans)

Submission status: Accepted

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

MOD = 1000000007  # type: int


def solve(N: int, A: "List[int]"):
    from itertools import accumulate
    acc = list(accumulate(reversed(A), lambda a,b: (a+b)%MOD))[::-1]
    ans = 0
    for i in range(N-1):
        ans = (ans + A[i] * acc[i+1] % MOD) % MOD
    return ans


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
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]"
    print(solve(N, A))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
def inp():
    return input()
def iinp():
    return int(input())
def inps():
    return input().split()
def miinps():
    return map(int,input().split())
def linps():
    return list(input().split())
def lmiinps():
    return list(map(int,input().split()))
def lmiinpsf(n):
    return [list(map(int,input().split()))for _ in range(n)]

n = iinp()
a = lmiinps()

ans = 0
count = 0

for i in range(n):
    count += (a[i]%(10**9+7))

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


ans %= (10**9+7)

print(ans)

Submission status: Accepted

Solution code:
import sys

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

    sum_A = sum(A)
    cum_A = [sum_A]
    for a in A[:N-1]:
        cum_A.append(cum_A[-1] - a)

    res = 0
    mod = 1000000007
    for i, a in enumerate(cum_A[1:]):
        res += A[i] * a
        res %= mod

    print(res)

if __name__ == "__main__":
    main()

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())
arr = list(map(int,input().split()))
mod = (10**9)+7
print(findProductSum(arr,n)%mod)

Submission status: Accepted

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

Asum=[0]
for i in A:
  Asum.append(Asum[-1]+i)

s=sum(A)
ans=0

for i,v in enumerate(A):
  ans+=v*(Asum[-1]-Asum[i+1])
  ans%=10**9+7

print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools

input = sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = A[::-1]
    B = list(itertools.accumulate(A))
    MOD = 10**9+7
    ans = 0
    for i in range(N - 2, -1, -1):
        ans = (ans + A[i + 1] * B[i]) % MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

sums = 0
for a in A:
    sums += a
    sums %= mod

ans = 0

for a in A:
    sums = (sums - a) % mod
    ansi = (sums * a) % mod
    ans += ansi
    ans %= mod

print(ans)

Submission status: Accepted

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

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


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

print(ans)