Problem:


Score : 500 points

Problem Statement

On a two-dimensional plane, there are m lines drawn parallel to the x axis, and n lines drawn parallel to the y axis.
Among the lines parallel to the x axis, the i-th from the bottom is represented by y = y_i.
Similarly, among the lines parallel to the y axis, the i-th from the left is represented by x = x_i.

For every rectangle that is formed by these lines, find its area, and print the total area modulo 10^9+7.

That is, for every quadruple (i,j,k,l) satisfying 1\leq i < j\leq n and 1\leq k < l\leq m, find the area of the rectangle formed by the lines x=x_i, x=x_j, y=y_k and y=y_l, and print the sum of these areas modulo 10^9+7.

Constraints

2 \leq n,m \leq 10^5

-10^9 \leq x_1 < ... < x_n \leq 10^9

-10^9 \leq y_1 < ... < y_m \leq 10^9

x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

n m
x_1 x_2 ... x_n
y_1 y_2 ... y_m

Output

Print the total area of the rectangles, modulo 10^9+7.

Sample Input 1

3 3
1 3 4
1 3 6

Sample Output 1

60

The following figure illustrates this input:

The total area of the nine rectangles A, B, ..., I shown in the following figure, is 60.

Sample Input 2

6 5
-790013317 -192321079 95834122 418379342 586260100 802780784
-253230108 193944314 363756450 712662868 735867677

Sample Output 2

835067060



Sample input:
3 3
1 3 4
1 3 6


Sample output:
60

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.buffer.readline
mod=10**9+7

n,m=map(int,input().split())
*x,=map(int,input().split())
*y,=map(int,input().split())
xd=[i-j for j,i in zip(x,x[1:])]
yd=[i-j for j,i in zip(y,y[1:])]
xs=ys=0
for i,d in enumerate(xd):
	xs+=d*(i+1)*(n-1-i)%mod
	xs%=mod
for i,d in enumerate(yd):
	ys+=d*(i+1)*(m-1-i)%mod
	ys%=mod
print(xs*ys%mod)

Submission status: Accepted

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

MOD = 1000000007  # type: int


def solve(n: int, m: int, x: "List[int]", y: "List[int]"):
    X = 0
    for k in range(1,n+1):
        X += (k-1)*x[k-1]-(n-k)*x[k-1]
        X %= MOD
    Y = 0
    for k in range(1,m+1):
        Y += (k-1)*y[k-1]-(m-k)*y[k-1]
        Y %= MOD
    print(X*Y%MOD)

    return


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
    m = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    y = [int(next(tokens)) for _ in range(m)]  # type: "List[int]"
    solve(n, m, x, y)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
mod=1000000007
n,m=map(int,input().split())
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]

xc=0
for i in range(1,n):
    a=x[i]-x[i-1]
    l=min(i,n-i)
    #xc+=a*(l*n-((l*l+l)//2))
    xc+=a*(l*(n-l))
    xc%=mod

yc=0
for i in range(1,m):
    a=y[i]-y[i-1]
    l=min(i,m-i)
    #yc+=a*(l*m-((l*l+l)//2))
    yc+=a*(l*(m-l))
    yc%=mod
print((xc*yc)%mod)

Submission status: Accepted

Solution code:
mod=1000000007
n,m=map(int,input().split())
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]

xc=0
for i in range(1,n):
    a=x[i]-x[i-1]
    if i<=((n)//2):
        l=i
    else:
        l=n-i
    xc+=a*(l*n-(l*l))
    xc=xc%mod

yc=0
for i in range(1,m):
    a=y[i]-y[i-1]
    if i<=((m)//2):
        l=i
    else:
        l=m-i
    yc+=a*(l*m-(l*l))
    yc=yc%mod
print((xc*yc)%mod)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

X = sum([(2*(k+1) - n - 1) * x[k] for k in range(n)])
Y = sum([(2*(k+1) - m - 1) * y[k] for k in range(m)])

print(X * Y % (10**9+7))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
xsum=0;ysum=0;mod=10**9+7
for i,x in enumerate(X): xsum+=x*(i-(n-1-i))
for i,y in enumerate(Y): ysum+=y*(i-(m-1-i))
print(xsum * ysum % mod)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
X, Y = X[::-1], Y[::-1]

MOD = 10**9 + 7
x_sum, y_sum = 0, 0

for i in range(N):
  x_sum = x_sum + (N-i-1)*X[i]
X = X[::-1]
for i in range(N):
  x_sum = x_sum - (N-i-1)*X[i]
x_sum %= MOD

for i in range(M):
  y_sum = y_sum + (M-i-1)*Y[i]
Y = Y[::-1]
for i in range(M):
  y_sum = y_sum - (M-i-1)*Y[i]
y_sum %= MOD

print((x_sum*y_sum)%MOD)

Submission status: Accepted

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

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 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = LIST()
B = LIST()

# 縦横独立に、全区間総和を取る
lsm = rsm = 0
for i in range(N):
    lsm += A[i] * (N-i-1)
    rsm += A[i] * i
    lsm %= MOD
    rsm %= MOD
h = rsm - lsm
lsm = rsm = 0
for i in range(M):
    lsm += B[i] * (M-i-1)
    rsm += B[i] * i
    lsm %= MOD
    rsm %= MOD
w = rsm - lsm

ans = h * w % MOD
print(ans)

Submission status: Accepted

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

import sys
from itertools import accumulate

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 ** 18
MOD = 10 ** 9 + 7

N, M = MAP()
A = LIST()
B = LIST()

# データの持ち方を座標から区間長にして累積和
A2 = []
for i in range(N-1):
    A2.append(A[i+1] - A[i])
B2 = []
for i in range(M-1):
    B2.append(B[i+1] - B[i])
A2 = [0] + list(accumulate(A2))
B2 = [0] + list(accumulate(B2))

# 縦横独立に、累積和の全区間総和を取る
lsm = rsm = 0
for i in range(N):
    lsm += A2[i] * (N-i-1)
    rsm += A2[i] * i
    lsm %= MOD
    rsm %= MOD
h = rsm - lsm
lsm = rsm = 0
for i in range(M):
    lsm += B2[i] * (M-i-1)
    rsm += B2[i] * i
    lsm %= MOD
    rsm %= MOD
w = rsm - lsm

ans = h * w % MOD
print(ans)

Submission status: Accepted

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

Y = 0
for i in range(1, m + 1):
    Y += (i - 1) * y[i - 1] - (m - i) * y[i - 1]
    Y %= mod

X = 0
for i in range(1, n + 1):
    X += (i - 1) * x[i - 1] - (n - i) * x[i - 1]
    X %= mod

print(X * Y % mod)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
x = [x[i+1]-x[i] for i in range(n-1)]
y = [y[i+1]-y[i] for i in range(m-1)]
mod = 10**9+7
sumx = 0
sumy = 0
for i in range(n-1):
    sumx += x[i]*(i+1)*(n-1-i)%mod
for i in range(m-1):
    sumy += y[i]*(i+1)*(m-1-i)%mod
print(sumx*sumy%mod)

Submission status: Accepted

Solution code:
def sum_subarrays(A, mod=10**9+7):
    N = len(A)
    S = [0]
    for a in A:
        S.append(S[-1] + a)
        S[-1] %= mod
    return sum((2 * i - N) * s % mod for i, s in enumerate(S)) % mod

n,m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
mod = 10 ** 9 + 7
diff_x = [x[i+1]-x[i] for i in range(n-1)]
diff_y = [y[i+1]-y[i] for i in range(m-1)]
ans = sum_subarrays(diff_x) * sum_subarrays(diff_y) % mod
print(ans)

Submission status: Accepted

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

X = list(map(int, input().split()))
Dx = []
for i in range(n-1):
    Dx.append(X[i+1]-X[i])
#print(Dx)
S = [0]
for x in Dx:
    S.append((S[-1]+x) % mod)
#print(S)
T = [0]
for s in S:
    T.append((T[-1]+s) % mod)
#print(T)
Ux = 0
for i in range(1, n):
    Ux += (S[i]*i-T[i]) % mod
    #print(S[i]*i-T[i])

#print(Ux)

Y = list(map(int, input().split()))
Dy = []
for i in range(m-1):
    Dy.append(Y[i+1]-Y[i])
#print(Dy)
S = [0]
for y in Dy:
    S.append((S[-1]+y) % mod)
#print(S)
T = [0]
for s in S:
    T.append((T[-1]+s) % mod)
#print(T)
Uy = 0
for i in range(1, m):
    Uy += (S[i]*i-T[i]) % mod
    #print(S[i]*i-T[i])

#print(Uy)

print(Ux*Uy%mod)

Submission status: Accepted

Solution code:
n , m= map(int,input().split())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
mod = 10**9+7
def f(N,i):
  return (i*(N-i+1))%mod
def l(L,i):
  return (L[i]-L[i-1])%mod
def lf(L):
  N = len(L)
  return sum([(l(L,i)*f(N-1,i))%mod  for i in range(1,N)])
print(lf(x)*lf(y)%mod)

Submission status: Accepted

Solution code:
def make_list(a):
    t = a-1
    s = a-3
    anslist = [t]
    while s > 0:
        anslist.append(s+t)
        t += s
        s -= 2
    if a % 2 == 1:
        anslist += anslist[::-1]
    else:
        anslist = anslist[:-1] + anslist[::-1]
    return anslist

mod = 10**9+7
h, w = [ int(v) for v in input().split() ]
h_list = [ int(v) for v in input().split() ]
w_list = [ int(v) for v in input().split() ]
h_list_2 = [(h_list[i+1] - h_list[i]) % mod for i in range(h-1)]
w_list_2 = [(w_list[i+1] - w_list[i]) % mod for i in range(w-1)]
h_list_3 = make_list(h)
w_list_3 = make_list(w)
h_total = sum([(h_list_3[i] * h_list_2[i]) % mod for i in range(h-1)]) % mod
w_total = sum([(w_list_3[i] * w_list_2[i]) % mod for i in range(w-1)]) % mod

print((h_total*w_total)%mod)

Submission status: Accepted

Solution code:

N,M = map(int, input().split())

X = list(map(int, input().split()))
Y = list(map(int, input().split()))
MOD = 10**9+7


sum_x = 0
sum_y = 0
for i in range(N):
    sum_x += (i - (N - i - 1))*X[i] % MOD

for j in range(M):
    sum_y += (j - (M - j - 1))*Y[j] % MOD


print(sum_x*sum_y%MOD)

Submission status: Accepted

Solution code:
import sys
import numpy as np

MOD = 10 ** 9 + 7

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n, m = I[:2]
x = I[2:2+n]
y = I[2+n:]

def main():
    
    sdx = np.sum(x * np.arange(n) - x * np.arange(n-1, -1, -1)) % MOD
    sdy = np.sum(y * np.arange(m) - y * np.arange(m-1, -1, -1)) % MOD
    ans = sdx * sdy % MOD
    return ans
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

MOD = 10 ** 9 + 7

I = np.array(sys.stdin.read().split(), dtype=np.int64)
n, m = I[:2]
x = I[2:2+n]
y = I[2+n:]

def main():
    sx = np.cumsum(x) % MOD 
    sy = np.cumsum(y) % MOD

    sdx = np.sum((sx[n-1] - sx[:n-1]) - x[:n-1] * ((n-1) - np.arange(n-1))) % MOD
    sdy = np.sum((sy[m-1] - sy[:m-1]) - y[:m-1] * ((m-1) - np.arange(m-1))) % MOD

    ans = sdx * sdy % MOD
    return ans

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
#mint
class ModInt:
    def __init__(self, x):
        self.x = x % mod
    
    def __str__(self):
        return str(self.x)
    
    __repr__ = __str__

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

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

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

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

    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, mod-2,mod))
        else:
            return ModInt(self.x * pow(other, mod - 2, mod))
            
    def __rtruediv(self, other):
        if isinstance(other, self):
            return ModInt(other * pow(self.x, mod - 2, mod))
        else:
            return ModInt(other.x * pow(self.x, mod - 2, mod))
            

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

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

def main():
    N,M=MI()
    X=LI()
    Y=LI()
    ans_x = ModInt(0)
    ans_y = ModInt(0)
    for i,x in enumerate(X):
        ans_x += (i*x)-( (N-i-1)*x )
    for j,y in enumerate(Y):
        ans_y += (j*y)-( (M-j-1)*y )
    print(ans_x*ans_y)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7


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


N, M = map(int, input().split())
*x, = map(int, input().split())
*y, = map(int, input().split())

sx = sum(mod_mul(_x, i - (N - 1 - i)) for i, _x in enumerate(x))  # _x * i - _x * (N - 1 - i)
sy = sum(mod_mul(_y, i - (M - 1 - i)) for i, _y in enumerate(y))  # _y * i - _y * (M - 1 - i)
print(sx * sy % mod)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

xlen=0
ylen=0

for i in range(1,n):
    xlen+=i*(n-i)*(x[i]-x[i-1])

for i in range(1,m):
    ylen+=i*(m-i)*(y[i]-y[i-1])

print(xlen*ylen%(10**9+7))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))

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

b = []
for i in range(m - 1):
    b.append(y[i+1] - y[i])

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

t = 0
for i in range(1, m):
    t += i * (m - i) * b[i-1]
    t = t % (10 ** 9 + 7)

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

Submission status: Accepted

Solution code:
n, m = (int(i) for i in input().split())
xlist=list(int(i) for i in input().split())
ylist=list(int(i) for i in input().split())

#x=xlist[-1]-xlist[0]
#y=ylist[-1]-ylist[0]

tate=0
yoko=0

for i in range(n-1):
    now=xlist[i+1]-xlist[i]
    now*=(i+1)*(n-i-1)
    tate+=now
    tate%=(10**9+7)


for j in range(m-1):
    now=ylist[j+1]-ylist[j]
    now*=(j+1)*(m-j-1)
    yoko+=now
    yoko%=(10**9+7)

#print(tate, yoko)

print((tate*yoko)%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
X=[]
Y=[]
for i in range(n-1):
  X.append(x[i+1]-x[i])
for i in range(m-1):
  Y.append(y[i+1]-y[i])
for i in range(n-1):
  X[i]=X[i]*(n-i-1)*(i+1)%(10**9+7)
for i in range(m-1):
  Y[i]=Y[i]*(m-i-1)*(i+1)%(10**9+7)
print((sum(X)%(10**9+7)*sum(Y)%(10**9+7))%(10**9+7))

Submission status: Accepted

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

r = [0] # r[i] : xiと、xiより左の縦の直線を使用した長方形の横の辺の長さの総和
for i in range(1, n):
    r.append(r[-1] + (x[i] - x[i-1]) * i)

dp_h = [0] * n # dp[i] : 左からi本目までの縦の直線のうち2本を使用したときの長方形の横の辺の長さの総和
for i in range(1, n):
    dp_h[i] = (dp_h[i-1] + r[i]) % MOD

u = [0]
for i in range(1, m):
    u.append(u[-1] + (y[i] - y[i-1]) * i)

dp_v = [0] * m
for i in range(1, m):
    dp_v[i] = (dp_v[i-1] + u[i]) % MOD

print((dp_h[n-1] * dp_v[m-1]) % MOD)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]
P=10**9+7
X=0
Y=0
for i in range(1,N+1):
  X+=x[i-1]*(2*i-1-N)
  X=X%P
for j in range(1,M+1):
  Y+=y[j-1]*(2*j-1-M)
  Y=Y%P
print((X*Y)%P)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,m=map(int,input().split())
    X=list(map(int,input().split()))
    Y=list(map(int,input().split()))
    X.sort()
    Y.sort()
    I=0
    J=0
    coef=-(n-1)
    for x in X:
        I+=coef*x
        I%=MOD
        coef+=2
    coef=-(m-1)
    for y in Y:
        J+=coef*y
        J%=MOD
        coef+=2
    print(I*J%MOD)
resolve()

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
X = [int(x) for x in input().split()]
Y = [int(y) for y in input().split()]
mod = 10**9+7

Xd = []
for i in range(n-1):
    Xd.append(X[i+1]-X[i])
    
Yd = []
for i in range(m-1):
    Yd.append(Y[i+1]-Y[i])
    
if n > 2:
    Xd[1] = 2*Xd[0] + 2*Xd[1]
    for i in range(n-3):
        Xd[i+2] = 2*Xd[i+1] - Xd[i] + (i+3)*Xd[i+2]
        Xd[i+2] %= mod
        
if m > 2:
    Yd[1] = 2*Yd[0] + 2*Yd[1]
    for i in range(m-3):
        Yd[i+2] = 2*Yd[i+1] - Yd[i] + (i+3)*Yd[i+2]
        Yd[i+2] %= mod
    
print(Xd[-1]*Yd[-1]%mod)

Submission status: Accepted

Solution code:
def main():
    n,m=map(int,input().split())
    s=list(map(int,input().split()))
    t=list(map(int,input().split()))
    mod=10**9+7
    s=[(s[i+1]-s[i])*(i+1)*(n-i-1)%mod for i in range(n-1)]
    t=[(t[i+1]-t[i])*(i+1)*(m-i-1)%mod for i in range(m-1)]
    print(sum(s)*sum(t)%mod)
main()

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, M = getlist()
	x = getlist()
	y = getlist()
	X = 0
	Y = 0
	for i in range(N - 1):
		X += ((x[i + 1] - x[i]) * (i + 1) * (N - i - 1)) % con
		X %= con
	for i in range(M - 1):
		Y += ((y[i + 1] - y[i]) * (i + 1) * (M - i - 1)) % con
		Y %= con
	print(X * Y % con)


if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
def examD(mod):
    n, m = LI()
    x = LI()
    y = LI()
    ans = 0; ans1 = 0; ans2 = 0
    for i in range(n-1):
        surface = (x[i+1]-x[i])*(n-1-i)*(i+1)
        ans1 = (ans1+surface)%mod
    for i in range(m-1):
        surface = (y[i+1]-y[i])*(m-1-i)*(i+1)
        ans2 = (ans2+surface)%mod
    ans = ans1*ans2%mod
    print(ans)

import sys
import copy
import bisect
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examD(mod)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))

MOD = 10**9+7
w_sum = 0
for i in range(N//2):
    times = N-1 - i*2
    w = X[-1-i] - X[i]
    w_sum += (w*times)%MOD
    w_sum %= MOD

h_sum = 0
for i in range(M//2):
    times = M-1 - i*2
    h = Y[-1-i] - Y[i]
    h_sum += (h*times)%MOD
    h_sum %= MOD

print((w_sum * h_sum) % MOD)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

MOD = 10**9 + 7


def f(ls):
    n = len(ls)
    s = 0
    for i, x in enumerate(ls):
        s += (i + 1) * (n - i) * x
        s %= MOD
    return s


n, m = ril()
ls0 = ril()
ls1 = ril()
ls0 = [b - a for a, b in zip(ls0, ls0[1:])]
ls1 = [b - a for a, b in zip(ls1, ls1[1:])]
print(f(ls0) * f(ls1) % MOD)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
mod=10**9+7
h=[]
for i in range(n):
    h.append(x[i]-x[0])
x=h
h=[]
for i in range(m):
    h.append(y[i]-y[0])
y=h

s=0
for i in range(n):
    s=(s+x[i])%mod
t=0
for i in range(m):
    t=(t+y[i])%mod

data=[s]
for i in range(1,n):
    u=data[-1]-(x[i]-x[i-1])*(n-i)
    data.append(u)
s=0
for v in data:
    s=(s+v)%mod
data=[t]
for i in range(1,m):
    u=data[-1]-(y[i]-y[i-1])*(m-i)
    data.append(u)
t=0
for v in data:
    t=(t+v)%mod

print(s*t%mod)

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
nn = n-1
mm = m-1
XList = list(map(int,input().split()))
YList = list(map(int,input().split()))

XDiff = [0] * nn
YDiff = [0] * mm
for i in range(nn):
    XDiff[i] = XList[i+1]-XList[i]
for i in range(mm):
    YDiff[i] = YList[i+1]-YList[i]

MOD = 10**9+7 
sumx = 0
sumy = 0
for i in range(1,n):
    sumx += (i*(n-i))*XDiff[i-1] 
    sumx %= MOD
for i in range(1,m):
    sumy += (i*(m-i))*YDiff[i-1]
    sumy %= MOD

ans = sumx*sumy%MOD
print(ans)

Submission status: Accepted

Solution code:
mod = 1000000007
n,m = [int(x) for x in input().split()]
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]

xtot=0
ytot=0
mult=n-1
multdiff=mult-2
for i in range(1,n):
    xadd = x[i]-x[i-1]
    xadd = (xadd%mod * abs(mult)%mod)%mod
    xtot = (xtot%mod + xadd%mod)%mod

    mult+=multdiff
    multdiff-=2

mult=m-1
multdiff=mult-2
for i in range(1,m):
    yadd = y[i]-y[i-1]
    yadd = (yadd%mod * abs(mult)%mod)%mod
    ytot = (ytot%mod + yadd%mod)%mod

    mult+=multdiff
    multdiff-=2

print((xtot%mod*ytot%mod)%mod)

Submission status: Accepted

Solution code:
# 予めsx, syを全て計算しておく
# このとき漸化式を使えば毎回全ての話を求める必要がない

mod = int(1e9+7)
n, m = [int(num) for num in input().split()]

x = [int(x) for x in input().split()]
y = [int(y) for y in input().split()]


sx = [0] * n
sx[0] = x[0]
for i in range(n-1):
    sx[i+1] = sx[i] + x[i+1]
sxd = 0  
for i in range(n-1):
    sxd += ((sx[n-1] - sx[i]) - x[i] * ((n-1) - (i+1) + 1)) #% mod

sy = [0] * m
sy[0] = y[0]
for k in range(m-1):
    sy[k+1] = sy[k] + y[k+1]
syd = 0
for k in range(m-1):
    syd += ((sy[m-1] - sy[k]) - y[k] * ((m-1) - (k+1) + 1)) #% mod

ans = ((sxd % mod) * (syd % mod)) % mod

print(ans)

Submission status: Accepted

Solution code:
# 予めsx, syを全て計算しておく
# このとき漸化式を使えば毎回全ての話を求める必要がない

mod = int(1e9+7)
n, m = [int(num) for num in input().split()]

x = [int(x) for x in input().split()]
y = [int(y) for y in input().split()]


sx = [0] * n
sx[0] = x[0]
for i in range(n-1):
    sx[i+1] = sx[i] + x[i+1]
sxd = 0  
for i in range(n-1):
    sxd += ((sx[n-1] - sx[i]) - x[i] * ((n-1) - (i+1) + 1)) % mod

sy = [0] * m
sy[0] = y[0]
for k in range(m-1):
    sy[k+1] = sy[k] + y[k+1]
syd = 0
for k in range(m-1):
    syd += ((sy[m-1] - sy[k]) - y[k] * ((m-1) - (k+1) + 1)) % mod

ans = ((sxd % mod) * (syd % mod)) % mod

print(ans)

Submission status: Accepted

Solution code:
p=10**9+7

n,m=map(int,input().split())
x=list(map(int,input().split()))
w=[((x[i+1]-x[i])*(1+i)*(n-i-1))%p for i in range(n-1)]
y=list(map(int,input().split()))
h=[((y[k+1]-y[k])*(1+k)*(m-k-1))%p for k in range(m-1)]

print(((sum(w)%p)*(sum(h)%p)%p))

Submission status: Accepted

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

lx = [x[i+1]-x[i] for i in range(n-1)]
ly = [y[i+1]-y[i] for i in range(m-1)]
lsum = [0]
for i in range(10**5+5):
    lsum.append((lsum[i]+i+1)%mod)

xres = 0
for i in range(1,n):
    xsum = 0
    kaisuu = min(i,n-i)
    xsum += lsum[kaisuu-1]*2
    xsum += kaisuu * (n-1-2*(kaisuu-1))
    xsum %= mod
    xsum = (xsum * lx[i-1])%mod
    xres += xsum
xres %= mod

yres = 0
for i in range(1,m):
    ysum = 0
    kaisuu = min(i,m-i)
    ysum += lsum[kaisuu-1]*2
    ysum += kaisuu * (m-1-2*(kaisuu-1))
    ysum %= mod
    ysum = (ysum * ly[i-1])%mod
    yres += ysum
yres %= mod

print((xres*yres)%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
mod = 1000000007
xtot = 0
ytot = 0
for i in range(n):
	xtot += (2*i-n+1)*x[i]
	xtot %= mod
for i in range(m):
	ytot += (2*i-m+1)*y[i]
	ytot %= mod
print(int((xtot*ytot)%mod))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
list_x = list(map(int, input().split()))
list_y = list(map(int, input().split()))

sum_x_pair = 0
for i in range(n):
  sum_x_pair += i * list_x[i] - (n - i - 1) * list_x[i]
sum_y_pair = 0
for j in range(m):
  sum_y_pair += j * list_y[j] - (m - j - 1) * list_y[j]

print((sum_x_pair * sum_y_pair) % (7 + 10**9))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split( ))
x = list(map(int, input().split( )))
y = list(map(int, input().split( )))

mod = 10**9 +7

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

H = 0
for i in range(m-1):
    H += (y[i+1]-y[i]) * (i+1) *(m-i-1)
    H %= mod

ans = H * W % mod 

print(ans)

Submission status: Accepted

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

ans = 0
y01 = (y[1] - y[0])%mod
ynum = m-1
for i in range(n-1):
    s = (((x[i+1] - x[i])%mod) * y01) % mod
    xnum = (i+1) * (n-i-1)
    ans = (ans + (s*xnum*ynum) % mod) % mod
band = ans

inv = [0] * (m + 1)
inv[1] = 1
for i in range(2, m):
    inv[i] = mod - inv[mod % i] * (mod // i) % mod

for j in range(1, m-1):
    band = (((((band * inv[j])%mod) * inv[m-j])%mod) * (((j+1) * (m-j-1))%mod))%mod
    band = ((band * ((y[j+1] - y[j])%mod))%mod * pow(((y[j]-y[j-1])%mod), mod-2, mod))%mod
    ans = (ans + band)%mod

print(ans)

Submission status: Accepted

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

N, M = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
mod = 10**9+7

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

B = 0
for j in range(M-1):
    B += (Y[j+1]-Y[j])*((j+1)*(M-1-j)) % mod
    B %= mod

print(A*B%mod)

Submission status: Accepted

Solution code:

n,m = map(int,input().split())

x = list(map(int,input().split()))
y = list(map(int,input().split()))

xsum = 0

up = 1
down = n-1

for i in range(n-1):

    xsum += (x[i+1] - x[i]) * up * down
    xsum %= (10 ** 9 + 7)

    up += 1
    down -= 1

left = 1
right = m-1

ans = 0

for i in range(m-1):

    ans += (y[i+1] - y[i]) * left * right * xsum
    ans %= (10 ** 9 + 7)

    left += 1
    right -= 1

print (ans)

Submission status: Accepted

Solution code:
n,m=[int(j) for j in input().split()]
x=[int(j) for j in input().split()]
y=[int(j) for j in input().split()]
mod=10**9+7
sx=0
sy=0
for i in range(n):
    sx+=(2*i-n+1)*x[i]
    sx=sx%mod
for i in range(m):
    sy+=(2*i-m+1)*y[i]
    sy=sy%mod

print((sx*sy)%mod)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
x_sum, y_sum = 0, 0
for i in range(n):
  k = i + 1
  x_sum += (2*k - n - 1) * x[i]
  x_sum = x_sum % 1000000007
for i in range(m):
  k = i + 1
  y_sum += (2*k - m - 1) * y[i]
  y_sum = y_sum % 1000000007
ans = (x_sum * y_sum) % 1000000007
print(ans)

Submission status: Accepted

Solution code:

n, m = map(int,input().split())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))

XX = [X[i+1]-X[i] for i in range(n-1)]
YY = [Y[i+1]-Y[i] for i in range(m-1)]

mod = 10**9 + 7

ans = 0
ans0 = 0
for i in range(1, n):
    ans1 = (i * (n-i)) % mod
    ans1 = (ans1 * XX[i-1]) % mod
    ans0 = (ans0 + ans1) % mod

for i in range(1, m):
    ans1 = (i * (m-i)) % mod
    ans1 = (ans1 * YY[i-1]) % mod
    ans1 = (ans1 * ans0) % mod
    ans = (ans + ans1) % mod

print(ans)

Submission status: Accepted

Solution code:

n,m =list(map(int,input().split(" ")))
xs  =list(map(int,input().split(" ")))
ys =list(map(int,input().split(" ")))
MOD = (10**9) + 7
x_diff = [abs(i - j) for i,j in zip(xs[:n-1],xs[1:n]) ]
y_diff = [abs(i - j) for i,j in zip(ys[:m-1],ys[1:m]) ]

cot = 1
x = 0
for i in range(n -1 ):
    x +=cot * (n-i -1) * x_diff[i]
    cot += 1

cot = 1
y = 0
for i in range(m -1):
    y += cot * (m-i -1) * y_diff[i]
    cot += 1



print (x* y % MOD )

Submission status: Accepted

Solution code:
MOD=1000000007
N,M=map(int,input().split())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
XX=[x2-x1 for x2,x1 in zip(X[1:],X[:-1])]
YY=[x2-x1 for x2,x1 in zip(Y[1:],Y[:-1])]

xs = [((i+1)*(N-i-1)*XX[i])%MOD for i in range(N-1)]
totaly=0
for j in range(M-1):
    tmp = ((j+1)*(M-j-1)*YY[j])%MOD
    totaly = (totaly+tmp)%MOD
ans = 0
for x in xs:
    tmp = (totaly*x)%MOD
    ans = (ans+tmp)%MOD

print(ans)