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
import math
import collections
import bisect
import itertools
import decimal

# import numpy as np

# sys.setrecursionlimit(10 ** 6)
INF = 10 ** 20
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline().rstrip())
ns = lambda: map(int, sys.stdin.readline().rstrip().split())
na = lambda: list(map(int, sys.stdin.readline().rstrip().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().rstrip().split()))


# ===CODE===

def main():
    n = ni()
    a = na()

    cum = [0 for _ in range(n + 1)]
    for i, ai in enumerate(a):
        cum[i + 1] = cum[i] + ai
        # cum[i + 1] %= MOD

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
import sys
def main(N, A):
    s = Mint()
    for a in A:
        s += a
    ans = Mint()
    for a in A:
        ans += a * (s - a)
    print(ans // 2)

MOD = 10**9 + 7
class Mint:
    def __init__(self, value=0):
        self.value = value % MOD
        if self.value < 0: self.value += MOD

    @staticmethod
    def get_value(x): return x.value if isinstance(x, Mint) else x

    def inverse(self):
        a, b = self.value, MOD
        u, v = 1, 0
        while b:
            t = a // b
            b, a = a - t * b, b
            v, u = u - t * v, v
        if u < 0: u += MOD
        return u

    def __repr__(self): return str(self.value)
    def __eq__(self, other): return self.value == other.value
    def __neg__(self): return Mint(-self.value)
    def __hash__(self): return hash(self.value)
    def __bool__(self): return self.value != 0

    def __iadd__(self, other):
        self.value = (self.value + Mint.get_value(other)) % MOD
        return self

    def __add__(self, other):
        new_obj = Mint(self.value)
        new_obj += other
        return new_obj
    __radd__ = __add__

    def __isub__(self, other):
        self.value = (self.value - Mint.get_value(other)) % MOD
        if self.value < 0: self.value += MOD
        return self

    def __sub__(self, other):
        new_obj = Mint(self.value)
        new_obj -= other
        return new_obj

    def __rsub__(self, other):
        new_obj = Mint(Mint.get_value(other))
        new_obj -= self
        return new_obj

    def __imul__(self, other):
        self.value = self.value * Mint.get_value(other) % MOD
        return self

    def __mul__(self, other):
        new_obj = Mint(self.value)
        new_obj *= other
        return new_obj
    __rmul__ = __mul__

    def __ifloordiv__(self, other):
        other = other if isinstance(other, Mint) else Mint(other)
        self *= other.inverse()
        return self

    def __floordiv__(self, other):
        new_obj = Mint(self.value)
        new_obj //= other
        return new_obj

    def __rfloordiv__(self, other):
        new_obj = Mint(Mint.get_value(other))
        new_obj //= self
        return new_obj

if __name__ == '__main__':
    input = sys.stdin.readline
    N = int(input())
    *A, = map(int, input().split())
    main(N, A)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

p=10**9+7

SUM=0
for i in range(N):
    SUM+=A[i]
    SUM%=p

ans=0
for i in range(N):
    SUM-=A[i]
    SUM%=p
    ans+=(A[i]*(SUM))%p
    
    ans%=p

print(ans)
#print(SUM)

Submission status: Accepted

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

b = [0 for i in range(n - 1)]
b[0] = a[0]

for i in range(1, n - 1):
  b[i] = (b[i - 1] + a[i]) % PRIME

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

print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N = getN()
A = getList()

imos = copy.deepcopy(A)
for i in range(N - 1):
    imos[-i - 2] += imos[-i - 1]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int,input().split()))
LL = [item**2 for item in List]
mod = 10**9 + 7
ListTotal = sum(List)
ans = (ListTotal**2 - sum(LL))//2%mod
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)
INF = 10 ** 9 + 1  # sys.maxsize # float("inf")
MOD = 10 ** 9 + 7


def debug(*x):
    print(*x, file=sys.stderr)


def solve(N, AS):
    sum = 0
    sumSq = 0
    for i in range(N):
        sum += AS[i]
        sum %= MOD
        sumSq += AS[i] * AS[i]
        sumSq %= MOD

    ret = (sum * sum - sumSq) % MOD
    if ret % 2 == 0:
        return ret // 2
    else:
        return (ret + MOD) // 2


def main():
    # parse input
    N = int(input())
    AS = list(map(int, input().split()))
    print(solve(N, AS))


# tests
T1 = """
3
1 2 3
"""
TEST_T1 = """
>>> as_input(T1)
>>> main()
11
"""

T2 = """
4
141421356 17320508 22360679 244949
"""
TEST_T2 = """
>>> as_input(T2)
>>> main()
437235829
"""


def _test():
    import doctest
    doctest.testmod()
    g = globals()
    for k in sorted(g):
        if k.startswith("TEST_"):
            doctest.run_docstring_examples(g[k], g, name=k)


def as_input(s):
    "use in test, use given string as input file"
    import io
    f = io.StringIO(s.strip())
    g = globals()
    g["input"] = lambda: bytes(f.readline(), "ascii")
    g["read"] = lambda: bytes(f.read(), "ascii")


input = sys.stdin.buffer.readline
read = sys.stdin.buffer.read

if sys.argv[-1] == "-t":
    print("testing")
    _test()
    sys.exit()

main()

Submission status: Accepted

Solution code:
import sys

MOD = 10 ** 9 + 7


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    ans = sum(a) ** 2
    for e in a:
        ans -= e ** 2
    ans //= 2
    ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
MOD = 10**9 + 7


def mpow(a,b):
    ans = 1
    while (b> 0):
        if b&1:
            ans = (ans*a)%MOD
        a= (a*a)%MOD
        b= b>>1
    
    return ans


n = int(input())
arr =[int(c) for c in input().split()]
s = sum(arr)

ans = 0
for i in arr:
    su = s-i
    ans = (ans + ((su)*i)%MOD)%MOD

ans = ans * mpow(2,MOD-2)
if ans < 0:
    ans+=MOD

print(ans%MOD)

Submission status: Accepted

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

par = [0] * n
par[0] = a[0]

mod = 10**9 + 7

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cumsum = [0]
for i in range(N):
    cumsum.append(cumsum[-1] + A[i])

mod = 10**9 + 7
ans = 0

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

print(ans % mod)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque

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


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    A = NLI()
    A = [a%MOD for a in A]
    s = sum(A)
    ss = sum([a**2%MOD for a in A])
    print((s**2-ss)%MOD * pow(2, -1, MOD) %MOD)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

sA = sum(As) % (10 ** 9 + 7)
print((sA * sA - sum( (A * A % (10 ** 9 + 7) for A in As) ) ) * pow(2, -1, 10 ** 9 + 7) % (10 ** 9 + 7))

Submission status: Accepted

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

x=sum(a)

for i in a:
  ans+=((x-i)*i)
  
ans//=2

print(ans%mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()
aa = a[::-1]
mod = 10**9+7

for i in range(1,n):
    aa[i] = aa[i]+aa[i-1]

aa = aa[::-1]
ans = 0

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

print(ans%mod)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

result = 0

MOD = 10**9 + 7

for i in range(n):
	sum -= a[i]
	result = (result + a[i] * sum) % MOD

print(result)

Submission status: Accepted

Solution code:
N = int(input())

A = [int(s) for s in input().split()]

MOD = 10**9 + 7
sums = [A[0]]
for a in A[1:]:
    sums.append((a + sums[-1]) % MOD )

alls = 0
for i in range(1, len(A)):
    # print(A[i], sums[i-1])
    alls += A[i] * sums[i-1]
    alls %= MOD
print(alls)

Submission status: Accepted

Solution code:
n = int(input())
*arr, = map(int, input().split())
modulo = 10 ** 9 + 7
p = arr.copy()
p.append(0)
for i in range(len(p) - 2, -1, -1):
    p[i] = (p[i] + p[i + 1]) % modulo

ans = 0
for i in range(n):
    ans += arr[i] * p[i + 1]
    ans %= modulo
print(ans)

Submission status: Accepted

Solution code:
def pow(x,m):
    if m==0:
        return 1
    if m==1:
        return x
    if m%2==0:
        return (pow(x,m//2)**2)%p
    else:
        return (x*(pow(x,(m-1)//2)**2)%p)%p
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
a = sum(A)
b = 0
for i in range(N):
    b = (b+A[i]**2)%p
ans = (a*a)%p-b
ans = (pow(2,p-2)*ans)%p
print(ans)

Submission status: Accepted

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

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

print(ret%MOD)

Submission status: Accepted

Solution code:
N = int(input())

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

bubun = 0
ans = 0

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

    
print(ans)

Submission status: Accepted

Solution code:
import sys

MOD = 10**9 + 7

def main():
    N = int(sys.stdin.readline().rstrip())
    A = [int(x) for x in sys.stdin.readline().rstrip().split()]

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


main()

Submission status: Accepted

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

s = sum(A)
ans = 0
for a in A:
    s -= a
    ans += a * s
    ans %= 10**9 + 7

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    R = list(accumulate(A))
    res = 0
    for i in range(n):
        res += (A[i] * (R[-1] - R[i])) % mod
        res %= mod
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil,pi,factorial
    from operator import itemgetter
    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def LI2(): return [int(input()) for i in range(n)]
    def MXI(): return [[LI()]for i in range(n)]
    def SI(): return input().rstrip()
    def printns(x): print('\n'.join(x))
    def printni(x): print('\n'.join(list(map(str,x))))
    inf = 10**17
    mod = 10**9 + 7
#main code here!
    n=I()
    lis=LI()
    S=0
    sm1=0
    for i in range(n):
        S=(S+lis[i])%mod
    S=((S%mod)**2)%mod
    for i in range(n):
        sm1=sm1+((lis[i]%mod)**2)%mod
    ans=(((S-sm1)%mod)*pow(2,mod-2,mod))%mod
    
    
    print(ans)
            
        
if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    from sys import setrecursionlimit, stdin, stderr
    from os import environ
    from collections import defaultdict, deque, Counter
    from math import ceil, floor
    from itertools import accumulate, combinations, combinations_with_replacement
    setrecursionlimit(10**6)
    dbg = (lambda *something: stderr.write("\033[92m{}\033[0m".format(str(something)+'\n'))) if 'TERM_PROGRAM' in environ else lambda *x: 0
    input = lambda: stdin.readline().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    P = 10**9+7
    INF = 10**18+10

    N = II()
    A = LMIIS()

    sum_A = 0
    sum_pow2 = 0
    for a in A:
        sum_A += a
        sum_A %= P
        sum_pow2 += a**2
        sum_pow2 %= P
    
    print((sum_A**2 % P - sum_pow2)%P*pow(2,P-2,P)%P)


    
main()

Submission status: Accepted

Solution code:
def main():
    MOD = 10 ** 9 + 7

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD = 1000000007
class ModInt(int):
    def __init__(self, x):
        self.x = x % MOD

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

    __repr__ = __str__

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

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

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

    def __floordiv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, MOD - 2, MOD))
        else:
            if other == 0:
                raise ZeroDivisionError
            else:
                return ModInt(self.x * pow(other, MOD - 2, MOD))

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

    __radd__ = __add__

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

    __rmul__ = __mul__

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

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

def main():
    n = int(input())
    A = list(map(lambda x: ModInt(int(x)), input().split()))
    print((sum(A) ** 2 - sum(a ** 2 for a in A)) // 2)



if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

acc = [As[0]]
for i in range(1, N):
    tmp = acc[-1] + As[i]
    acc.append(tmp % MOD)

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())

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

p=10**9+7

sumA=sum(A)

A2=0
for i in range(n):
  A2+=A[i]*A[i]

print(((sumA*sumA-A2)//2)%p)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cumA = [0]
for i in range(N):
  cumA.append(cumA[-1] + A[i])

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

Submission status: Accepted

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

mx = 10**9 + 7

su = sum(A)
ans = 0

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

print(ans % mx)

Submission status: Accepted

Solution code:
N = int(input())
P = 10 ** 9 + 7
X = 0
Y = 0
for a in map(int, input().split()):
  X += a ** 2
  Y += a
  X %= P
  Y %= P
print(((Y ** 2 % P - X) * pow(2, P - 2, P)) % P)

Submission status: Accepted

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

mod = int(1e+9 + 7)

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

Submission status: Accepted

Solution code:
N=int(input())
S=list(map(int,input().split()))
m=10**9+7
x=sum(S)%m
r=0
m=10**9+7
def div(a,b):
    return (a*pow(b,m-2,m))%m
for s in S:
    r=(r+s*(x-s))%m
print(div(r,2))

Submission status: Accepted

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


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
Mod=10**9+7
sa=sum(A)
sb=0
ans =0
for a in A:
  sb += a
  ans += (sa - sb)%Mod * a
  ans%=Mod

print(ans)

Submission status: Accepted

Solution code:
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools
from math import *
from bisect import *
from heapq import *
from collections import *

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

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()

######################################

N = In.int()
A = In.ints()
MOD = 10**9+7

presum = [A[0]]

for i in range(1, len(A)):
    presum.append(presum[-1]+A[i])

ans = 0

for i in range(len(presum)-1):
    ans += A[i]*(presum[-1]-presum[i])
    ans%= MOD

Out.int(ans)

######################################

if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
aa = tuple(map(int, input().split()))

MOD = 10 ** 9 + 7

ans = 0
x = sum(aa)
for i in range(n):
    x -= aa[i]
    ans += aa[i] * x
    ans %= MOD
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
sm = sum(a)
ans = 0
mod = 1000000007
for x in a:
    sm -= x
    ans = (ans + sm*x)%mod
print(ans)

Submission status: Accepted

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

M = (10**9)+7
n = int(input())
As = map(int, input().split(" "))

cumsum = []
cur = 0
out = 0
for i in range(n-1):
    cur += As[i]
    out += cur*As[i+1] 
    out = out % M
print(out)

Submission status: Accepted

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

sumL = []
totalSum = sum(listA)
for a in listA:
    totalSum-=a
    sumL.append(totalSum)

ans = 0
for i in range(len(listA)):
    ans += listA[i]*sumL[i]%(10**9 + 7)
    ans %= (10**9 + 7)
print(ans)

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase

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")

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 Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

from math import gcd

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

n = int(ri())
a = Ri()

pre = [0]*n
for i in range(n-1,-1,-1):
    if i == n-1:
        pre[i] = a[i]
    else:
        pre[i] = pre[i+1]+a[i]

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

Submission status: Accepted

Solution code:
import itertools as it

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