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:
def examC():
    N = I()
    d = defaultdict(int)
    for i in range(N):
        curD = defaultdict(int)
        S = SI()
        for s in S:
            curD[s]+=1
        if i==0:
            d = curD
            continue
        for s in alphabet:
            d[s] = min(d[s],curD[s])
    d = sorted(d.items())
    ans = ""
    for key,i in d:
        ans +=key*i
    print(ans)
    return

def examD():
    N, M = LI()
    X = LI(); Y = LI()
    distX = [0]*N; distY = [0]*M
    for i in range(N-1):
        distX[i+1] = X[i+1]-X[i]
    for i in range(M-1):
        distY[i+1] = Y[i+1]-Y[i]
    numX = [0]*N; numY = [0]*M
    for i in range(N):
        numX[i] = i*(N-i)
    for i in range(M):
        numY[i] = i*(M-i)
    LX = 0; LY = 0
    for i in range(N):
        LX += distX[i]*numX[i]
        LX %= mod
    for i in range(M):
        LY += distY[i]*numY[i]
        LY %= mod
#    print(numX,distX); print(numY,distY)
    ans = (LX*LY) % mod
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examD()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
def s(k):
    X = [int(a) for a in input().split()]
    return sum([(X[i] - X[i-1]) * i * (k-i) for i in range(1, k)])

print(s(N) * s(M) % (10**9+7))

Submission status: Accepted

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

N, M = map(int, readline().split())
MOD = 10**9+7

N -= 1
M -= 1
X = list(map(int, readline().split()))
CX = [x2 - x1 for x1, x2 in zip(X, X[1:])]
CX = [CX[i]*(N-i)*(i+1) for i in range(N)]
Y = list(map(int, readline().split()))
CY = [y2 - y1 for y1, y2 in zip(Y, Y[1:])]
CY = [CY[j]*(M-j)*(j+1) for j in range(M)]
print(sum(CX)%MOD*sum(CY)%MOD)

Submission status: Accepted

Solution code:
input()
def s():
    A = [int(a) for a in input().split()]
    return sum([(A[i] - A[i-1]) * i * (len(A)-i) for i in range(1, len(A))])
print(s() * s() % (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()))

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

ans = 0
for i in range(m-1):
    val_y = (i+1) * (m-1-i) * (y[i+1] - y[i])
    ans += val_x * val_y
    ans %= 10**9 + 7

print(ans)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
aa=list(map(int,input().split()))
bb=list(map(int,input().split()))
mod=10**9+7
ss=0
for i in range(n-1):
    ss+=(aa[i+1]-aa[i])*(i+1)*(n-i-1)
    ss%=mod
tt=0
for i in range(m-1):
    tt+=(bb[i+1]-bb[i])*(i+1)*(m-i-1)
    tt%=mod
print((ss*tt)%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

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

ans = 0
for i in range(N - 1):
    ans += YArea * (X[i + 1] - X[i]) * (i + 1) * (N - 1 - i)
    ans %= 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

dxs = [b-a for a,b in zip(X,X[1:])]
dys = [b-a for a,b in zip(Y,Y[1:])]

xsum = 0
k = d = len(dxs)
for dx in dxs:
    xsum += k*dx
    xsum %= MOD
    d -= 2
    k += d

ysum = 0
k = d = len(dys)
for dy in dys:
    ysum += k*dy
    ysum %= MOD
    d -= 2
    k += d

print((xsum * ysum) % MOD)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    mod = 10**9+7
    
    N,M = map(int,input().split())
    X = list(map(int,input().split()))
    Y = list(map(int,input().split()))
    XX = 0
    YY = 0
    for i in range(N-1):
        l = X[i+1]-X[i]
        XX += l*(i+1)*(N-i-1)
        XX %= mod
    for i in range(M-1):
        l = Y[i+1]-Y[i]
        YY += l*(i+1)*(M-i-1)
        YY %= mod
    ans = (XX*YY)%mod
    print(ans)
        
    
    
    
main()

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
sys.setrecursionlimit(2147483647)


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m = LI()
X = LI()
Y = LI()
y_ret = 0
x_ret = 0
for i in range(1, n):
    x_diff = X[i] - X[i - 1]
    x_ret += x_diff * i * (n - i)
    x_ret %= mod


for j in range(1, m):
    y_diff = Y[j] - Y[j - 1]
    y_ret += y_diff * j * (m - j)
    y_ret %= mod



print(y_ret * x_ret % mod)

Submission status: Accepted

Solution code:
MOD = 1000000007
def hoge(a):
    b = a//2
    return (b*(b+1) + (a%2)*(b+1))%MOD
def power(a):
    return (a*a)%MOD
def abss(a):
    if a<0:
      return -a
    else:
      return a
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
dx = []
dy = []
n-=1
m-=1
for i in range(n):
    dx.append(x[i+1]-x[i])
for i in range(m):
    dy.append(y[i+1]-y[i])
cx = hoge(n)
cy = hoge(m)
sx = 0
sy = 0
for i in range(n):
    if n%2==1:
        sx += dx[i]*(cx-power(abss(i-n//2)))
    else:
        if i<(n//2):
            sx += dx[i]*(cx-(n//2-i-1)*(n//2-i))
        else:
            sx += dx[i]*(cx-(i-n//2)*(i-n//2+1))
    sx%=MOD
for i in range(m):
    if m%2==1:
        sy += dy[i]*(cy-power(abss(i-m//2)))
    else:
        if i<(m//2):
            sy += dy[i]*(cy-(m//2-i-1)*(m//2-i))
        else:
            sy += dy[i]*(cy-(i-m//2)*(i-m//2+1))
    sy%=MOD
ans = (sx*sy)%MOD
if ans<0:
    ans+=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()))
x.sort()
y.sort()
mod = 10 ** 9 + 7
#重なりのない無数の小長方形を作って考える
#全ての長方形はこの小長方形の組み合わせで作られる
#小長方形ごとに面積を求め、他の長方形の組み立てに寄与する回数を掛ける
#小長方形ごとの計算をしてるとあれなので辺ごとにして、片方はすでに和をとっておくと計算量落ちる
xe = [0] * (N - 1)
ye = [0] * (M - 1)
for i in range(N - 1):
  xe[i] = abs(x[i + 1] - x[i]) * (N - i - 1) * (i + 1)
for i in range(M - 1):
  ye[i] = abs(y[i + 1] - y[i]) * (M - i - 1) * (i + 1)
sumxe = sum(xe)
res = 0
for yi in ye:
  res = (res + sumxe * yi) % mod
print(res)

Submission status: Accepted

Solution code:
mod = 10**9+7
n, m = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
sum_x = 0
sum_y = 0
for i in range(1, n):
    x0 = X[i-1]
    x1 = X[i]
    sum_x += (x1-x0)*i*(n-i)
    sum_x%=mod
for i in range(1, m):
    y0 = Y[i-1]
    y1 = Y[i]
    sum_y += (y1-y0)*i*(m-i)
    sum_y%=mod
print((sum_x*sum_y)%mod)

Submission status: Accepted

Solution code:
mod = 10**9 + 7

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

sx = 0
sy = 0

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

for j in range(m):
    sy += (2*j-m+1)*y[j]
    sy %= mod

print((sx*sy) % mod)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
INF=10**9+7
ls1,ls2=[],[]
tmp1,tmp2=0,0
for i in l1:
    tmp1+=i
    ls1.append(tmp1)
for i in l2:
    tmp2+=i
    ls2.append(tmp2)
w=0
for i,j in enumerate(l1):
    w+=ls1[-1]-ls1[i]-j*(n-i-1)
h=0
for i,j in enumerate(l2):
    h+=ls2[-1]-ls2[i]-j*(m-i-1)
print((w*h)%INF)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from math import *
from heapq import *

n, m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
mod = 10**9+7
_a = 0
for i, a in enumerate(A):
    _a += i*a-(n-i-1)*a
    _a %= mod
_b = 0
for i, b in enumerate(B):
    _b += i*b-(m-i-1)*b
    _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()))
MOD = 10 ** 9 + 7

xsum, ysum = 0, 0
for i in range(1, N):
    xsum += (X[i] - X[i-1]) * i * (N - i)
    xsum %= MOD
for j in range(1, M):
    ysum += (Y[j] - Y[j-1]) * j * (M - j)
    ysum %= MOD

print((xsum * ysum) % MOD)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

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

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

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

for j in range(m-1):
  sum_v += (y[j+1] - y[j])*(j+1)*(m-j-1)
sum_v = sum_v % mod

sum = (sum_h * sum_v) % mod

#print(sum_h,sum_v)
print(sum)

Submission status: Accepted

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

# i番目の直線とi-1番目の直線の間の領域が使用される回数をもとに解を求める
ans = (
    sum(i * (n - i) * (x[i] - x[i - 1]) for i in range(1, n)) *
    sum(i * (m - i) * (y[i] - y[i - 1]) for i in range(1, m))
) % (10**9 + 7)

# 出力
print(ans)

Submission status: Accepted

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

xsum = 0
for i, x in enumerate(xlist):
  xsum += i * x - (n - i - 1) * x

ysum = 0
for i, y in enumerate(ylist):
  ysum += i * y - (m - i - 1) * y

print(xsum * ysum % 1000000007)

Submission status: Accepted

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

Submission status: Accepted

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

x_sum = 0
for i in range(1,n):
    x_sum += (x[i]-x[i-1])*i*(n-i)
    x_sum = x_sum%mod
y_sum = 0
for i in range(1,m):
    y_sum += (y[i]-y[i-1])*i*(m-i)
    y_sum = y_sum % mod
print(x_sum*y_sum%mod)

Submission status: Accepted

Solution code:
def getN():
    return int(input())

def getMN():
    a = input().split()
    b = [int(i) for i in a]
    return b[0],b[1]

def getlist():
    a = input().split()
    b = [int(i) for i in a]
    return b

MOD = 1000000007

n,m = getMN()
anums = getlist()
bnums = getlist()

#ある時点でのx幅の総パターンの和
#x_i * (i-1) - (累積和)
def calc_accum_width(nums):
    x_pos_acc = 0
    x_width_acc = 0

    for i, a in enumerate(nums):
        x_width_acc += a * i - x_pos_acc
        x_pos_acc += a

    return x_width_acc

x_acc = calc_accum_width(anums)
y_acc = calc_accum_width(bnums)

print(x_acc * y_acc % MOD)

Submission status: Accepted

Solution code:
def count_func(m,k):
    return k*(m-k+1)

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

x.sort()
y.sort()

x_line_sum = 0
y_line_sum = 0

for i in range(N-1):
    x_line_sum += (count_func(N-1,min(i+1,N-1-i)) * (x[i+1]-x[i]))%(10**9 + 7)

for i in range(M-1):
    y_line_sum += (count_func(M-1,min(i+1,M-1-i)) * (y[i+1]-y[i]))%(10**9 + 7)
 
print((x_line_sum*y_line_sum)%(10**9 + 7))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]
mod = 10**9+7

n, m = inpl()
x = inpl()
y = inpl()
H = [(x[i+1]-x[i])*(i+1)*(n-i-1) % mod for i in range(n-1)]
W = [(y[j+1]-y[j])*(j+1)*(m-j-1) % mod for j in range(m-1)]

print(sum(H)*sum(W) % 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
ans = 0
def f(a, n):
    ret = 0
    for i, v in enumerate(a):
        ret += i * v - (n - i - 1) * v
        ret %= mod
    return ret
print((f(x, n) * f(y, m)) % mod)

Submission status: Accepted

Solution code:
ai = lambda: list(map(int, input().split()))

n,m = ai()
x = ai()
y = ai()
mod = 10**9+7

from itertools import accumulate as acc
sx = list(acc(x))
ssx = list(acc(x[::-1]))
sy = list(acc(y))
ssy = list(acc(y[::-1]))
    

a = sum(ssx[i] - sx[i] for i in range(n)) % mod
b = sum(ssy[i] - sy[i] for i in range(m)) % mod

print(a*b % mod)

Submission status: Accepted

Solution code:
ai = lambda: list(map(int, input().split()))

n,m = ai()
x = ai()
y = ai()
mod = 10**9+7
ans = 0

sx,ssx,sy,ssy = [x[0]], [x[-1]], [y[0]], [y[-1]]
for i in range(1, n):
    sx.append(sx[-1] + x[i])
    ssx.append(ssx[-1] + x[-i-1])
for i in range(1, m):
    sy.append(sy[-1] + y[i])
    ssy.append(ssy[-1] + y[-i-1])

a = sum(ssx[i] - sx[i] for i in range(n)) % mod
b = sum(ssy[i] - sy[i] for i in range(m)) % mod

ans = a*b % mod
print(ans)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import bisect
from heapq import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

MOD = 10**9 + 7
 
n, m = reads()
X = reads()
Y = reads()

P = sum(i * (n-i) * (X[i] - X[i-1]) % MOD for i in range(1, n)) % MOD
Q = sum(j * (m-j) * (Y[j] - Y[j-1]) % MOD for j in range(1, m)) % MOD
ans = P * Q % MOD
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, m = [int(i) for i in input().split()]
X = [0] + [int(i) for i in input().split()]
Y = [0] + [int(i) for i in input().split()]

p = 10 ** 9 + 7

xsum = 0
ysum = 0

for i in range(1, n+1):
  xsum += (2*i-n-1) * X[i] 
  
for i in range(1, m+1):
  ysum += (2*i-m-1) * Y[i] 

print(xsum * ysum % p)

Submission status: Accepted

Solution code:
mod=10**9+7
n,m=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
xl=0
xt=0
yl=0
yt=0
for i in range(n-1):
  if i==0:
    for j in range(1,n):
      xt+=x[j]-x[i]
    xl=xt
  else:
    xt-=(x[i]-x[i-1])*(n-i)
    xl=(xl+xt)%mod
for i in range(m-1):
  if i==0:
    for j in range(1,m):
      yt+=y[j]-y[i]
    yl=yt
  else:
    yt-=(y[i]-y[i-1])*(m-i)
    yl=(yl+yt)%mod
print(xl*yl%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()]
mod=pow(10,9)+7
a=0
b=0
for i in range(N):
    a=(a+(-N+2*i+1)*X[i])%mod
for i in range(M):
    b=(b+(-M+2*i+1)*Y[i])%mod
print((a*b)%mod)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)
 
 
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
p = 10**9 + 7
 
 
xsum = 0
for i in range(n-1):
    xsum = (xsum + (x[i+1] - x[i]) * (i+1) * (n-i-1)) % p
 
ysum = 0
for j in range(m-1):
    ysum = (ysum + (y[j+1] - y[j]) * (j+1) * (m-j-1)) % p
 
 
ans = (xsum * ysum) % p
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)


n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
p = 10**9 + 7


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

ysum = 0
for j in range(m-1):
    ysum = (ysum + (y[j+1] - y[j]) * (j+1) * (m-j-1)) % p


ans = (xsum * ysum) % p
print(ans)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

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

N, M = map(int, input().split())
X = [0] + list(map(int, input().split()))
Y = [0] + list(map(int, input().split()))
diff_X = [X[i + 1] - X[i] for i in range(N)]
diff_Y = [Y[i + 1] - Y[i] for i in range(M)]
print(sum_subarrays(diff_X) * sum_subarrays(diff_Y) % mod)

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

SX=0
SY=0

for i in range(N):
  SX=(SX+(2*i+1-N)*X[i])%MOD
for j in range(M):
  SY=(SY+(2*j+1-M)*Y[j])%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()))
MOD = 10 ** 9 + 7

ax = 0
ay = 0

for i in range(n):
	ax = (ax + (((2 * i - n + 1) * x[i]) % MOD)) % MOD

for i in range(m):
	ay = (ay + (((2 * i - m + 1) * y[i]) % MOD)) % MOD

print(int((ax * ay) % MOD))

Submission status: Accepted

Solution code:
import collections

mod = 1000000007

def solve():
    n,m = map(int,input().split())
    x = list(map(int,input().split()))
    y = list(map(int,input().split()))
    X = int()
    for i in range(1,n):
        X += (x[i]-x[i-1])*i*(n-i)
        X %= mod
    Y = int()
    for i in range(1,m):
        Y += (y[i]-y[i-1])*i*(m-i)
        Y %= mod
    print((X*Y)%mod)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

p = 10 ** 9 + 7

X = 0; Y = 0
for i in range(n):
    X += x[i]*(2*i-n+1)
    X = X % p
for i in range(m):
    Y += y[i]*(2*i-m+1)
    Y = Y % p

print((X*Y) % p)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

def make(N):
    if N%2:
        L = [N + (N//2)**2 - n**2 + 2*n - 1 for n in range(1, N//2+2)]
        L = L[1::][::-1] + L
        return L
    else:
        L = [N + N*(N-2)//4 -n**2 + n for n in range(1, N//2+1)]
        L = L[::-1] + L
        return L

N, M = inpl()

X = inpl()
Y = inpl()

A = make(N-1)
B = make(M-1)

Sx = 0
Sy = 0

for i, a in enumerate(A):
    Sx += a * (X[i+1] - X[i])

for j, b in enumerate(B):
    Sy += b * (Y[j+1] - Y[j])

print((Sx*Sy)%(10**9 + 7))

Submission status: Accepted

Solution code:
from operator import mul

n, m = map(int, input().split())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

MOD = 10 ** 9 + 7

fX = sum(map(mul, xs, range(-n + 1, n + 1, 2))) % MOD
fY = sum(map(mul, ys, range(-m + 1, m + 1, 2))) % MOD

print((fX * fY) % MOD)

Submission status: Accepted

Solution code:
import sys

mod = 10**9 + 7

def solve():
    n, m = map(int, input().split())
    x = [int(i) for i in input().split()]
    y = [int(i) for i in input().split()]

    ans = calc_all_section_length_sum(x)
    ans = (ans * calc_all_section_length_sum(y)) % mod

    print(ans)

def calc_all_section_length_sum(a):
    n = len(a)
    res = 0

    for i, ai in enumerate(a):
        res += (ai * (2*i + 1 - n)) % mod
        res %= mod

    return res

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import itertools


def main():
    n, m = map(int, input().split())

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

    x_len = 0
    y_len = 0

    for i in range(n):
        x_len += (2 * i - n + 1) * x[i] % (10 ** 9 + 7)
        # print(x_len)

    for j in range(m):
        y_len += (2 * j - m + 1) * y[j] % (10 ** 9 + 7)
        # print(y_len)

    print(x_len * y_len % (10 ** 9 + 7))


if __name__ == '__main__':
    main()

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
xsum = sum((i * x[i] - (n - 1 - i) * x[i]) for i in range(n))
ysum = sum((i * y[i] - (m - 1 - i) * y[i]) for i in range(m))
ans = (xsum * ysum) % 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
xsum, ysum = 0, 0
for i in range(n):
    xsum += i * x[i] - (n - 1 - i) * x[i]
for i in range(m):
    ysum += i * y[i] - (m - 1 - i) * y[i]
ans = (xsum * ysum) % mod
print(ans)

Submission status: Accepted

Solution code:
n, m = map(int, raw_input().split())
X = map(int, raw_input().split())
X.sort()
Y = map(int, raw_input().split())
Y.sort()

ya = 0
for i in xrange(m-1):
    ya += (Y[i+1]-Y[i]) * (i+1) * (m-i-1)
xa = 0
for i in xrange(n-1):
    xa += ya * (X[i+1]-X[i]) * (i+1) * (n-i-1)
print xa % (10**9 + 7)

Submission status: Accepted

Solution code:
mod = 1000000007

n, m = map(int, input().split())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

xres, yres = 0, 0
for i in range(n):
    xres += i*xs[i] - (n-i-1)*xs[i]
    xres %= mod

for i in range(m):
    yres += i*ys[i] - (m-i-1)*ys[i]
    yres %= mod

ans = (xres * yres) % mod
print(ans)

Submission status: Accepted

Solution code:
def input(cnt):
    inp = []
    for i in range(cnt):
        inp.append(map(int, raw_input().split()))
    return inp

(n, m), X, Y = input(3)

moder = 10**9 + 7
u, v = 0, 0
for i in xrange(1, n):
    u += (X[i] - X[i-1]) * i * (n - i) % moder
for j in xrange(1, m):
    v += (Y[j] - Y[j-1]) * j * (m - j) % moder
res = u * v % moder

print res

Submission status: Accepted

Solution code:
def get_sum_line(count, val_list):
    len_list = len(val_list)
    sum_line = 0
    for i in range(len_list):
        sum_line += val_list[i] * (i - (len_list - 1 - i))
    return sum_line

n, m = list(map(int, input().split()))
x_list = list(map(int, input().split()))
y_list = list(map(int, input().split()))

sum_x_line = get_sum_line(n, x_list)
sum_y_line = get_sum_line(m, y_list)

ans = int(sum_x_line * sum_y_line % (10**9 + 7))

print(ans)

Submission status: Accepted

Solution code:
div = int(1e9 + 7)
n, m = map(int, input().split())
tx, ty = 0, 0
coe_x, coe_y = 1 - n, 1 - m
for x in map(int, input().split()):
    tx = (tx + x * coe_x) % div
    coe_x += 2
for y in map(int, input().split()):
    ty = (ty + y * coe_y) % div
    coe_y += 2
print(tx * ty % div)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
Xs = list(map(int, input().split()))
Ys = list(map(int, input().split()))

MOD = 1000000007

w = Xs[1] - Xs[0]
ws = [w]
for i in range(2, N):
    ws.append((i * (Xs[i] - Xs[i - 1]) + ws[-1]) % MOD)
    w += ws[-1]
    w %= MOD
   
h = Ys[1] - Ys[0]
hs = [h]
for i in range(2, M):
    hs.append((i * (Ys[i] - Ys[i - 1]) + hs[-1]) % MOD)
    h += hs[-1]
    h %= MOD

ans = w * h % MOD
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from random import *

def readln():
    _res = list(map(int,str(input()).split(' ')))
    return _res

p = 1000000007

def calc(x):
    n = len(x)
    res = 0
    for i in range(0,n):
        res = res + x[i] * i
        res = res - x[i] * (n - i - 1)
    return res % p

n,m = readln()
x = readln()
y = readln()
print((calc(x) * calc(y)) % p)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#
# まず1次元で考える
#   |  |   |  |
#   a  b   c  d
# とあるとき、長さの合計は
#  (b-a) + (c-a) + (d-a) + (c-b) + (d-b) + (d-c)
# = 3d + c - b - 3a
# これは、各点の座標を、<自分より左にある点の数> - <自分より右にある点の数> でかけたものの和である
#
# もとめる面積は X座標、Y座標それぞれについて蒸気を計算した積

MOD = 1000000007

def len_sum(Xs):
    size = len(Xs)
    ret = 0
    n = len(Xs) - 1
    # 負の値にMODをとることを防ぐため、座標の大きい点から計算する
    for x in reversed(Xs):
        ret += n * x
        ret %= MOD
        n -= 2
    return ret

def solve(Xs, Ys):
    x_len_sum = len_sum(Xs)
    y_len_sum = len_sum(Ys)
    return (x_len_sum * y_len_sum) % MOD

N, M = map(int, input().split())
Xs = list(map(int, input().split()))
Ys = list(map(int, input().split()))
print(solve(Xs, Ys))

Submission status: Accepted

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

xs = list(map(int, input().split()))
ys = list(map(int, input().split()))

MOD = int(1e9 + 7)

x_sum = 0
y_sum = 0

for k in range(1, n+1):
    #x_sum += ((k-1) * xs[k-1] - (n-k) * xs[k-1])
    x_sum += ((2*k-n-1) * xs[k-1]) % MOD

for k in range(1, m+1):
    y_sum += (2*k-m-1) * ys[k-1] % MOD

print(x_sum * y_sum % MOD)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,m=map(int,raw_input().split())
x=map(int,raw_input().split())
y=map(int,raw_input().split())
mod=10**9+7
xx=0
yy=0
for i in xrange(n-1):
    xx+=(i+1)*(n-1-i)*(x[i+1]-x[i])%mod
for i in xrange(m-1):
    yy+=(i+1)*(m-1-i)*(y[i+1]-y[i])%mod
print xx*yy%mod

Submission status: Accepted

Solution code:
INF=10**9+7
L=[int(i)-1 for i in input().split()]
x=[int(i)   for i in input().split()]
y=[int(i)   for i in input().split()]
x.sort()
y.sort()

X=[x[i+1]-x[i] for i in range(L[0])]
Y=[y[i+1]-y[i] for i in range(L[1])]

usingX=[0 for i in range(L[0])]
usingY=[0 for i in range(L[1])]

if L[0]%2==0:
	for i in range(L[0]):
		temp=min(i,L[0]-i-1)
		temp+=1
		usingX[i]+=int(temp*(temp+1)/2)
		#print(usingX)
		usingX[i]+=temp*int(L[0]/2-temp)
		#print(usingX)
		usingX[i]*=2
		#print()
else:
	for i in range(L[0]):
		temp=min(i,L[0]-i-1)
		temp+=1
		usingX[i]+=int(temp*(temp+1)/2)
		usingX[i]+=temp*int((L[0]-1)/2-temp)
		usingX[i]*=2
		
		usingX[i]+=temp
	#temp=int((L[0]-1)/2)
	#usingX[temp]-=(temp+1)*2


if L[1]%2==0:
	for i in range(L[1]):
		temp=min(i,L[1]-i-1)
		temp+=1
		usingY[i]+=int(temp*(temp+1)/2)
		#print(usingY)
		usingY[i]+=temp*int(L[1]/2-temp)
		#print(usingY)
		usingY[i]*=2
		#print()
else:
	for i in range(L[1]):
		temp=min(i,L[1]-i-1)
		temp+=1
		usingY[i]+=int(temp*(temp+1)/2)
		usingY[i]+=temp*int((L[1]-1)/2-temp)
		usingY[i]*=2
		
		usingY[i]+=temp
	#temp=int((L[1]-1)/2)
	#usingY[temp]-=(temp+1)*2	

#print(usingX)
#print(usingY)
for i in range(L[0]):
	X[i]*=usingX[i]
for j in range(L[1]):
	Y[j]*=usingY[j]
ans=sum(X)*sum(Y)
print(ans%INF)

Submission status: Accepted

Solution code:
import sys

mod = 10**9 + 7

def solve():
    n, m = map(int, input().split())
    x = [int(i) for i in input().split()]
    y = [int(i) for i in input().split()]

    yoko = 0

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

    tate = 0

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

    ans = (tate * yoko) % mod

    print(ans)

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

if __name__ == '__main__':
    solve()