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

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log 
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', '')

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

Solution (Python):
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))  
sy = sum(mod_mul(_y, i - (M - 1 - i)) for i, _y in enumerate(y))  
print(sx * sy % mod)

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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())




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)%(10**9+7))

Solution (Python):
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)

Solution (Python):
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))

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

r = [0] 
for i in range(1, n):
    r.append(r[-1] + (x[i] - x[i-1]) * i)

dp_h = [0] * n 
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)

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):
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)

Solution (Python):
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()

Solution (Python):

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):



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

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

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

print(ans)

Solution (Python):



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)

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):

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)

Solution (Python):

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 )

Solution (Python):
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)

Solution (Python):



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

n,m = LI()
x = LI()
y = LI()
x.sort()
y.sort()
tmpx,tmpy = 0,0
for i in range(n):
    tmpx += x[i]*i
    tmpx -= x[i]*(n-1-i)
for i in range(m):
    tmpy += y[i]*i
    tmpy -= y[i]*(m-1-i)
ans = tmpx*tmpy
ans %= mod
print(ans)

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

Solution (Python):
N,M=map(int,input().split())
X,Y=(list(map(int,input().split())) for i in range(2))
x1,x2=([0]*N for i in range(2))
y1,y2=([0]*M for i in range(2))
m=10**9+7
for i in range(N-1):
    x1[i+1]=(x1[i]+(X[i+1]-X[i])*(i+1))%m
    x2[i+1]=(x2[i]+x1[i])%m
for i in range(M-1):
    y1[i+1]=(y1[i]+(Y[i+1]-Y[i])*(i+1))%m
    y2[i+1]=(y2[i]+y1[i])%m
print(((x1[-1]+x2[-1])*(y1[-1]+y2[-1]))%m)

Solution (Python):
MOD = 10**9 + 7

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

w = 0
for i in range(n):
    if (n-1-i) - i <= 0:
        break
    w += (x[n-1-i] - x[i]) * ((n-1-i) - i)
    w %= MOD

h = 0
for i in range(m):
    if (m-1-i) - i <= 0:
        break
    h += (y[m-1-i] - y[i]) * ((m-1-i) - i)
    h %= MOD


ans = (w * h) % MOD

print(ans)

Solution (Python):
def main():
    n, m = map(int, input().split())
    mod = pow(10, 9) + 7
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))

    prefix_x = [0]
    prefix_y = [0]
    for i in range(n):
        prefix_x.append(prefix_x[-1] + x[i])
    for i in range(m):
        prefix_y.append(prefix_y[-1] + y[i])

    
    
    ysum = 0
    for j in range(1, m):
        ysum += j*y[j] - prefix_y[j]
        ysum %= mod
    xsum = 0
    for j in range(1, n):
        xsum += j*x[j] - prefix_x[j]
        xsum %= mod

    
    
    return ysum * xsum % mod


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

Solution (Python):


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

X = sum([(2*i-n+1)*x[i] for i in range(n)]) % 1000000007
Y = sum([(2*i-m+1)*y[i] for i in range(m)]) % 1000000007
ans = (X*Y) % 1000000007
print(ans)

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

x_sum = 0
for k, value in enumerate(x):
    x_sum += (2 * (k + 1) - n - 1) * value
    x_sum %= MOD

y_sum = 0
for k, value in enumerate(y):
    y_sum += (2 * (k + 1) - m - 1) * value
    y_sum %= MOD

print ((x_sum * y_sum) % MOD)

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


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

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

N = [n - 1]
for i in range(n - 3, 0, -2):
    N.append(N[-1] + i)
    

M = [m - 1]
for i in range(m - 3, 0, -2):
    M.append(M[-1] + i)
    

if N[0] % 2 == 0:
    N += N[::-1]
elif N[0] != 1:
    N += N[-2::-1]

if M[0] % 2 == 0:
    M += M[::-1]
elif M[0] != 1:
    M += M[-2::-1]






if len(x) - 1 > len(N):
    while True:
        pass

if len(y) - 1 > len(M):
    while True:
        pass


X_sum = 0
for i in range(n - 1):
    X_sum += x[i] * N[i]
    X_sum %= MOD

Y_sum = 0
for i in range(m - 1):
    Y_sum += y[i] * M[i]
    Y_sum %= MOD

print ((X_sum * Y_sum) % MOD)

Solution (Python):
class Mint:
    MOD = 1000000007  
    CACHE_FACTORIALS = [1, 1]

    def __init__(self, v):
        if self.__isally(v):
            self.v = v.v % self.MOD
        else:
            self.v = v % self.MOD

    @property
    def inv(self):
        return Mint(self.__minv(self.v))

    @classmethod
    def factorial(cls, v):
        for i in range(len(cls.CACHE_FACTORIALS), int(v) + 1):
            cls.CACHE_FACTORIALS.append(cls.CACHE_FACTORIALS[-1] * i % cls.MOD)
        return Mint(cls.CACHE_FACTORIALS[int(v)])

    @classmethod
    def perm(cls, n, r):
        if n < r or r < 0:
            return 0
        return cls.factorial(n) // cls.factorial(n - r)

    @classmethod
    def comb(cls, n, r):
        if n < r or r < 0:
            return 0
        return cls.perm(n, r) // cls.factorial(r)

    @classmethod
    def __isally(cls, v) -> bool:
        return isinstance(v, cls)

    @classmethod
    def __minv(cls, v) -> int:
        return pow(v, cls.MOD - 2, cls.MOD)

    @classmethod
    def __mpow(cls, v, w) -> int:
        return pow(v, w, cls.MOD)

    def __str__(self):
        return str(self.v)
    __repr__ = __str__

    def __int__(self):
        return self.v

    def __eq__(self, w):
        return self.v == w.v if self.__isally(w) else self.v == w

    def __add__(self, w):
        return Mint(self.v + w.v) if self.__isally(w) else Mint(self.v + w)
    __radd__ = __add__

    def __sub__(self, w):
        return Mint(self.v - w.v) if self.__isally(w) else Mint(self.v - w)

    def __rsub__(self, u):
        return Mint(u.v - self.v) if self.__isally(u) else Mint(u - self.v)

    def __mul__(self, w):
        return Mint(self.v * w.v) if self.__isally(w) else Mint(self.v * w)
    __rmul__ = __mul__

    def __floordiv__(self, w):
        return Mint(self.v * self.__minv(w.v)) if self.__isally(w) else \
            Mint(self.v * self.__minv(w))

    def __rfloordiv__(self, u):
        return Mint(u.v * self.__minv(self.v)) if self.__isally(u) else \
            Mint(u * self.__minv(self.v))

    def __pow__(self, w):
        return Mint(self.__mpow(self.v, w.v)) if self.__isally(w) else \
            Mint(self.__mpow(self.v, w))

    def __rpow__(self, u):
        return Mint(self.__mpow(u.v, self.v)) if self.__isally(u) else \
            Mint(self.__mpow(u, self.v))


n, m = map(int, input().split())
x = [int(s) for s in input().split()]
y = [int(s) for s in input().split()]
dx = [x[i+1] - x[i] for i in range(n-1)]
dy = [y[i+1] - y[i] for i in range(m-1)]
dp = [Mint(0)] * n
for i, d in enumerate(dx):
    dp[i + 1] = dp[i] + (i + 1) * d
sx = sum(dp)
dp = [Mint(0)] * m
for i, d in enumerate(dy):
    dp[i + 1] = dp[i] + (i + 1) * d
sy = sum(dp)
print(sx * sy)

Solution (Python):
n, m = map(int, input().split())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
w = 0
for i in range(1, n):
  w += i*(n-i)*(xs[i]-xs[i-1])
h = 0
for i in range(1, m):
  h += i*(m-i)*(ys[i]-ys[i-1])
r = w*h%1000000007
print(r)

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

Solution (Python):

import sys
INF = float("inf")

MOD = 1000000007  


def solve(n: int, m: int, x: "List[int]", y: "List[int]"):

    xtot = 0
    for i in range(n-1):
        xtot += (x[i+1]-x[i])*(i+1)*(n-i-1) % MOD
    ytot = 0
    for i in range(m-1):
        ytot += (y[i+1]-y[i])*(i+1)*(m-i-1) % MOD

    print(xtot*ytot % 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))  
    m = int(next(tokens))  
    x = [int(next(tokens)) for _ in range(n)]  
    y = [int(next(tokens)) for _ in range(m)]  
    solve(n, m, x, y)


if __name__ == '__main__':
    main()

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 



n,m = [int(i) for i in readline().split()]
x = [int(i) for i in readline().split()]
y = [int(i) for i in readline().split()]

MOD = 10**9+7

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

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




print(si*sj%MOD)

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

x.sort(reverse=True)
y.sort(reverse=True)

s = sum(x[i] * (n - 1 - 2 * i) for i in range(n))
t = sum(y[i] * (m - 1 - 2 * i) for i in range(m))
print((s * t) % (10 ** 9 + 7))

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a, b, c = LI()
    if b - a == c - b:
        print("YES")
    else:
        print("NO")
    return


def B():
    o = S()
    e = S()
    for i in range(len(e)):
        print(o[i] + e[i], end="")
    if len(e) != len(o):
        print(o[-1], end="")
    print()
    return


def C():
    l = [inf] * 27
    n = II()
    for _ in range(n):
        b = [0] * 27
        for si in S():
            b[ord(si) - 97] += 1
        for i in range(27):
            l[i] = min(l[i], b[i])
    ans = []
    for i in range(27):
        ans.append(chr(97 + i) * l[i])
    print("".join(ans))
    return





def D():
    n, m = LI()
    x = LI()
    y = LI()
    ansx = 0
    ansy = 0
    for num, xi in enumerate(x, start=1):
        ansx += (num - 1) * xi - (n - num) * xi % mod
    for num, yi in enumerate(y, start=1):
        ansy += (num - 1) * yi - (m - num) * yi % mod
    print(ansx * ansy % mod)
    return


if __name__ == '__main__':
    D()

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

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

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

sumX = 0
sumY = 0

for i, x in enumerate(X):
    sumX += -(N - (i + 1)) * x + i * x
    sumX %= MOD

for i, y in enumerate(Y):
    sumY += -(M - (i + 1)) * y + i * y
    sumX %= MOD

ans = sumX * sumY % MOD
print(ans)