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:
#!/usr/bin/env python3

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input():  return sys.stdin.readline().rstrip()
    def ii():     return int(input())
    def isp():    return input().split()
    def mi():     return map(int, input().split())
    def mi_0():   return map(lambda x: int(x)-1, input().split())
    def lmi():    return list(map(int, input().split()))
    def lmi_0():  return list(map(lambda x: int(x)-1, input().split()))
    def li():     return list(input())
    def debug(x): print(x, file=sys.stderr)
    # def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)
    
    
    n = ii()
    L = lmi()
    ac = L[:]
    for i in range(n-2, -1, -1):
        ac[i] = (ac[i+1] + ac[i]) % mod
    
    # debug(ac)

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



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
from copy import deepcopy
from heapq import heappop, heappush
from bisect import bisect_left, bisect
from collections import Counter, defaultdict, deque
from itertools import product, permutations, combinations

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

s = 0

S = [0]

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

for i in range(N):
    s += A[i] * S[i]

print(s % (10**9 + 7))
exit()

Submission status: Accepted

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

all_sum = (sum(a) ** 2)
diff = [(a[i] ** 2) for i in range(n)]

ans = (all_sum - sum(diff)) // 2

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

Submission status: Accepted

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

Submission status: Accepted

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

Sum = 0
Asum = sum(A) % 1000000007
for i in range(len(A) -1 ):
    b = A[i]
    Asum = Asum - b
    if Asum < 0:
        Asum = Asum + 1000000007
    Sum = (Sum + Asum * b) % 1000000007

print(Sum)

Submission status: Accepted

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

def multipleSum(A, n):
    sum = 0
    for i in range(n):
        sum += A[i]
    mulsum = sum*sum
    insum = 0
    for i in range(n):
        insum+=A[i]*A[i]
    return (mulsum-insum)//2

N = int(input())
A = list(map(int,input().split()))
n = len(A)
print(multipleSum(A,n)%1000000007)

Submission status: Accepted

Solution code:
import sys,bisect

from sys  import stdin,stdout

from bisect import bisect_left,bisect_right,bisect,insort,insort_left,insort_right

from math import gcd,ceil,floor,sqrt

from collections import Counter,defaultdict,deque,OrderedDict

dx=[-1,1,0,0]
dy=[0,0,1,-1]


sys.setrecursionlimit(10**6)
INF = float('inf')
MOD = 998244353
mod = 10**9+7

def isPrime(n):
    if (n <= 1) :return False
    if (n <= 3) :return True
    if (n%2 == 0 or n%3 == 0):return False
    for i in range(5,ceil(sqrt(n))+1,6):
        if (n%i==0 or n%(i+2)==0):
            return False
    return True

def st():
    return list(stdin.readline().strip())

def inp():
    return int(stdin.readline())

def li():
    return list(map(int,stdin.readline().split()))

def mp():
    return map(int,stdin.readline().split())

def pr(n):
    stdout.write(str(n)+"\n")


def soe(limit):
    l=[1]*(limit+1)
    l[0]=0
    l[1]=0
    prime=[]
    for i in range(2,limit+1):
        if l[i]:
            for j in range(i*i,limit+1,i):
                l[j]=0
    
    for i in range(2,limit+1):
        if l[i]:
            prime.append(i)
    return prime

                
def gcd(a,b):
    while b:
        a=a%b
        b,a=a,b
    return a

def power(a,n):
    r=1
    while n:
        if n&1:
            r=(r*a)
        a*=a
        n=n>>1
    return r
    
def solve():
    n=inp()
    s=1
    A=li()
    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] 

    c=((array_sum_square - individual_square_sum)>>1)%mod
    pr(c)
    
for _ in range(1):
    solve()

Submission status: Accepted

Solution code:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque, defaultdict
from fractions import gcd
from functools import reduce
from itertools import groupby, combinations
from heapq import heapify, heappush, heappop

# sys.setrecursionlimit(5000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return list(map(lambda x: x + d, A))
def ldec(A, d=1): return list(map(lambda x: x - d, A))

def modinv(a, n):
    (x, lastx) = (0, 1); (y, lasty) = (1, 0); p = n
    while p != 0:
       q = a // p; (a, p) = (p, a % p)
       (x, lastx) = (lastx - q * x, x); (y, lasty) = (lasty - q * y, y)
    return lastx % n

N = n1()
A = nn()

mod = 10**9 + 7

s = sum(A) % mod

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

print((ans * modinv(2, mod)) % mod)

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline  #文字列では使わない
N = int(input())
A = list(map(int, input().split()))
mod =10**9 +7
sum_a =sum(A)
ans =0
for i in range(N-1):
    sum_a -=A[i]
    ans +=(A[i] *(sum_a)) %mod
ans %=mod        
print(ans)

Submission status: Accepted

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

sum = 0
for i in range(n):
  sum += a_list[i] * (sum_a-a_list[i])
  sum = sum%(10**9+7)
  sum_a -= a_list[i]
print(sum)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=input()
arr=map(int, raw_input().split())
sums=[0]*n
sums[0]=arr[0]
for i in xrange(1, n):
  sums[i]=sums[i-1]+arr[i];
mod=10**9+7
ans=0
for i in xrange(n):
  ans=(ans%mod+arr[i]*(sums[-1]-sums[i])%mod)%mod
print ans

Submission status: Accepted

Solution code:
n = int(input())
array = list(map(int, input().split()))
sum = sum(array)
answer = 0
from collections import deque
d = deque(array)
for i in range(n):
    a = d.popleft()
    sum -= a
    answer += a*sum
    answer %= 10**9 + 7
print(answer)

Submission status: Accepted

Solution code:
import sys
import math

big = 10 ** 9 + 7

N = int(input())
A = list(map(int, sys.stdin.readline().strip().split()))
sum_A = sum(A)

ans = 0
for i in range(1, len(A)):
    sum_A -= A[i - 1]
    ans += A[i - 1] * sum_A

print(ans % big)

Submission status: Accepted

Solution code:
MOD = 10**9 + 7

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

sum_sq = pow(sum(A), 2)
sq_sum = sum([a**2 for a in A])

ans = (sum_sq - sq_sum) // 2 % MOD
print(ans)

Submission status: Accepted

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


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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys


def main():
    N = int( sys.stdin.readline() )
    A_list = list(map(int, sys.stdin.readline().split()))
    
    cum_sum = 0
    A_cum_sum = []

    for i in range(len(A_list)):
        cum_sum += A_list[i]
        A_cum_sum.append(cum_sum)


    mod = 10**9 + 7
    S = 0

    for i in range(len(A_list)):
        S += A_list[i] * ( A_cum_sum[-1] - A_cum_sum[i] )
        S %= mod
    
    print(S)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
import random
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10 ** 9 + 7
divide = lambda x: pow(x, MOD - 2, MOD)


def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD


def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n - k])) % MOD


def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv


def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]


class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h + 2) * (w + 2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid

    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x

    def getgrid(self, x, y):
        return self.grid[y][x]

    def get(self, x, y):
        return self.cost[self.pos(x, y)]

    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return

    def show(self):
        for i in range(self.h + 2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i + 1)])

    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return

    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy


def solve(nums=0):
    n = getN()
    nums = getList()
    sumn = sum(nums)
    ans = 0
    for num in nums:
        ans += (num * (sumn - num))


    ans //= 2
    print((ans % MOD))


def solve2(nums):
    n = len(nums)
    ans = 0
    for i in range(n):
        for j in range(i+1, n):
            ans += nums[i] * nums[j]

    return (ans % MOD)
if __name__ == "__main__":
    solve()
    # for i in range(100):
    #     nums = [random.randint(0, 100) for i in range(1000)]
    #     print(nums)
    #     # main()
    #     a1 = solve(nums)
    #     a2 = solve2(nums)
    #     if a1 != a2:
    #         print(nums)
    #         print(a1)
    #         print(a2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n=int(input())
A=list(map(int,input().split()))
ans=0
mod=10**9+7
s=[0 for _ in range(n+1)]

for i in range(n):
    s[i+1]=(s[i]+A[i]%mod)%mod

for k in range(1,n):
    ans=(ans+(s[k]*(A[k]%mod))%mod)%mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n-1):
    sum_A -= A[i]
    total += A[i] * sum_A

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

Submission status: Accepted

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

s = 0
d = 0
for j in range(1,N):
    d += A[j]

for i in range(N-1):
    s += d * A[i]
    if s > 10 ** 9 + 7:
        s = s % (10 ** 9 + 7)
    d -= A[i+1]

print(s)

Submission status: Accepted

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

diff = A[0]
res = 0

for i in range(1, N):
    res += diff*A[i]
    diff += A[i]
    res %= 10**9+7
    diff %= 10**9+7
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

rule = 1000000007
s = 0
list_sum = sum(As[1:])
if list_sum >= rule:
    list_sum = list_sum % rule

for i in range(len(As) - 1):
    s += As[i] * list_sum
    if s >= rule:
        s = s % rule
    list_sum -= As[i+1]
    if list_sum < 0:
        list_sum += rule
print(s)

Submission status: Accepted

Solution code:
def findProductSum(A, n): 
      
    # calculating array sum (a1 + a2 ... + an) 
    array_sum = 0
    for i in range(0, n, 1): 
        array_sum = array_sum + A[i] 
  
    # calcualting square of array sum 
    # (a1 + a2 + ... + an)^2 
    array_sum_square = array_sum * array_sum 
  
    # calcualting a1^2 + a2^2 + ... + an^2 
    individual_square_sum = 0
    for i in range(0, n, 1): 
        individual_square_sum += A[i] * A[i] 
  
    # required sum is (array_sum_square - 
    # individual_square_sum) / 2 
    return (array_sum_square - 
            individual_square_sum) //2
n=int(input())
M = 1000000007
lst=list(map(int,input().split()))
x=findProductSum(lst,n)
print(x%M)

Submission status: Accepted

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

Submission status: Accepted

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

B = A[-1]
ans = 0
for ai in A[-2::-1]:
  ans += (ai*B)%(10**9+7)
  B = (B+ai)%(10**9+7)

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

Submission status: Accepted

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

a_lst.reverse()
tmp = a_lst[0]
lst = []
for i in range(1, n):
    lst.append(tmp)
    tmp += a_lst[i]
    tmp %= (10 ** 9 + 7)
lst.reverse()
a_lst.reverse()

ans = 0
for i in range(n - 1):
    ans += (a_lst[i] * lst[i]) % (10 ** 9 + 7)
ans %= (10 ** 9 + 7)
print(ans)

Submission status: Accepted

Solution code:
def main(N, A):
    div = 10 ** 9 + 7
    s = sum(A)
    ans = 0
    for a in A:
        s = s - a
        mult = ((s % div) * a) % div
        ans += mult
    print(ans % div)

if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    main(N, A)

Submission status: Accepted

Solution code:
import math

num = int(input())
array_str = input().split(" ")
array = [int(n) for n in array_str]

value=0
a=0
b=0
modbase=int(7+math.pow(10,9))
for i in range(len(array)-1):
  bindex=i*(-1)-1
  b+=array[bindex]
  a=array[bindex-1]
  value+=a*b
print(value%modbase)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

mod = 10 ** 9 + 7
sum_A = sum(A)
# print(sum_A)
ans = [0] * (N - 1)
for i in range(N):
    if i == N - 1:
        break
    sum_A -= A[i]
    ans[i] = sum_A
    # print(i, ans)

# print(ans)
ans_num = 0
for i in range(len(ans)):
    ans_num += A[i] * ans[i]

print(ans_num % mod)

Submission status: Accepted

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

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

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


main()

Submission status: Accepted

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

import sys

DEBUG = False


def read(t):
    return t(sys.stdin.readline().rstrip())


def read_list(t, sep=" "):
    return [t(s) for s in sys.stdin.readline().rstrip().split(sep)]


def dprint(*args, **kwargs):
    if DEBUG:
        print(*args, **kwargs)
    return


MOD = int(1e9 + 7)


def mod_inv(a, b, mod):
    return (a * pow(b, mod - 2, mod)) % mod


def solve(as_):
    all_sum = 0
    for a in as_:
        all_sum = (all_sum + a) % MOD
    ans_x2 = 0
    for i in range(0, len(as_)):
        ans_x2 = (
            ans_x2 + ((MOD + (all_sum - as_[i]) % MOD) % MOD) * as_[i] % MOD) % MOD

    return mod_inv(ans_x2, 2, MOD)


def main():
    _n = read(int)
    as_ = read_list(int)
    print(solve(as_))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
nn = int(input())
aa = list(map(int, input().split()))
bb = [0]*(nn-1)
mysum = sum(aa)
mydiff = 0

for i in range(1,nn):
    mydiff += aa[i - 1]
    bb[i-1] = mysum - mydiff

finalsum = 0
for i in range(nn-1):
        finalsum += aa[i]*bb[i]
        finalsum = finalsum % (10**9 + 7)

print(finalsum)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N=int(input())
num=list(map(int,input().split()))
goukei=0
for i in range(N):
    goukei += num[i]
    goukei = (goukei%(10**9+7)) 
temp=goukei
sn=0
for i in range(N):
    temp=temp-num[i]
    sn+=num[i]*temp
print(sn%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
A = LI()

"""

1 2 3 4

1*2, 1*3 1*4
2*3 2*4
3*4
1 6 3 5
1*6 1*3 1*5 = 1 * (6 + 3 + 5)
3*5 
10 ** 5 * 
"""
ans = 0
s = sum(A) % mod
for a in A:
    s -= a
    s %= mod
    ans += a * s % mod
    ans %= mod

print(ans)

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 range(len(A)-1):
    Y = A[i]
    X -= Y
    ans += (Y * X)
print(ans % mod)

Submission status: Accepted

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

sum_a  = sum(A)
ans = 0
for i in range(len(A)):
    sum_a -= A[i]
    ans = (ans + (A[i] * sum_a) % 1000000007) % 1000000007
print(ans)

Submission status: Accepted

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

big = pow(10,9) + 7
sums = pow(sum(a),2)
a_2 = list(map(lambda x: x ** 2, a))

print(((sums-sum(a_2)) // 2)%big)

Submission status: Accepted

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

Submission status: Accepted

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

M = 1000000007
A = sum(a)
A *= A
B = 0
for ai in a:
    B += ai * ai
print(int((A - B) // 2 % M))

Submission status: Accepted

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

result = 0
MOD = 10**9+7

S = sum(As)

for i in range(len(As)-1):
    S -= As[i]
    tmp = As[i] * S
    result += tmp
    result %= MOD

print(result)

Submission status: Accepted

Solution code:
def sumproductsofpairs(lst):
    total = 0
    psum = 0
    for x in lst:
        total += psum * x
        psum += x
    return total%(1000000007) 
b=int(input())
A=list(map(int,input().split()))
print(sumproductsofpairs(A))

Submission status: Accepted

Solution code:
""" C
"""
import sys
import math
from functools import reduce
from bisect import bisect_left

def readString():
    return sys.stdin.readline()

def readInteger():
    return int(readString())

def readStringSet(n):
    return sys.stdin.readline().split(" ")[:n]

def readIntegerSet(n):
    return list(map(int, readStringSet(n)))

def readIntegerMatrix(n, m):
    return reduce(lambda acc, _: acc + [readIntegerSet(m)], range(0, n), [])

def main(N, A):
    M = pow(10, 9) + 7
    result = 0
    S = sum(A)
    for i in range(len(A)):
        S = S - A[i]
        result = (result + A[i] * S) % M
    return result

if __name__ == "__main__":
    _N = readInteger()
    _A = readIntegerSet(_N)

    print(main(_N, _A))

Submission status: Accepted

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

    mod = 10**9+7
    s = 0
    sq = 0
    for i in range(n):
        s += a[i]
        sq += (a[i])**2

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

resolve()

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
sum_a = sum(a_list)
ans = 0
m = 10 ** 9 + 7

for i in range(n):
    sum_a -= a_list[i]
    ans += a_list[i] * sum_a
    ans %= m

print(ans)

Submission status: Accepted

Solution code:
import sys


def resolve(in_):
    N = int(next(in_))
    A = tuple(map(int, next(in_).split()))

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

    return ans
    

def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0

for aa in a:
  ans += aa
  ans %= mod
ans *= ans
ans %= mod
for aa in a:
  ans -= aa*aa
  ans %= mod


if ans%2 == 1:
  print((ans+mod)//2)
else:
  ans //= 2
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

MOD = 10**9 + 7
# MOD = 10

sum_A = 0
for i in range(N):
    sum_A += A[i]
    # sum_A %= MOD
 
ans = (sum_A * sum_A) # % MOD

for i in range(N):
    ans -= (A[i] * A[i]) # % MOD
    # ans %= MOD

ans //= 2
ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
s = sum(arr)
sq_s = 0
for i in range(len(arr)):
    sq_s += arr[i]*arr[i]
m = 1000000007
print(((s**2 - sq_s)//2)%m)

Submission status: Accepted

Solution code:
#Sが長いほう
N = int(input())
A = list(map(int, input().split()))

cnt = 0
sumall = sum(A)
#print(sumall)
for i in range(N-1):
  sumall -= A[i]
  cnt += (A[i] * sumall )%(10**9+7)
print(cnt%(10**9+7))

Submission status: Accepted

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

for n, i in enumerate(reversed(A[1:]), 1):
    s += i
    ans += A[N-n-1] * s
    
print(ans % 1000000007)

Submission status: Accepted

Solution code:

n = int(input())
# n = 2 * 10**4
# a = [i for i in range(n)]
a = list(map(int, input().split(" ")))

wari = 10**9 + 7

ruiseki = [0]
for i, ai in enumerate(a):
    ruiseki.append(ruiseki[i] + ai)

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

print(ans)