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
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


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

xs = 0
for i in range(N):
    xs += i  * X[i]
    xs -= (N - i - 1) * X[i]
    xs %= MOD
ys = 0
for i in range(M):
    ys += i  * Y[i]
    ys -= (M - i - 1) * Y[i]
    ys %= MOD
print(xs * ys % 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()]
mod = 10**9+7
a = 0
for i, j, k in zip(range(n), x[:-1], x[1:]):
    a += abs(j-k) * (i+1)*(n-1-i)
b = 0
for i, j, k in zip(range(m), y[:-1], y[1:]):
    b += abs(j-k) * (i+1)*(m-1-i)
a %= mod
b %= mod
print(a*b % mod)

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

sx=0
sy=0
mod=10**9+7

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

for i in range(m-1):
  sy=(sy+(i+1)*(m-i-1)*(y[i+1]-y[i]))%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()))
mod = 10**9 + 7

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

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

ans *= uni_x
ans %= mod

print(ans)

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,m = map(int,readline().split())
*x, = map(int,readline().split())
*y, = map(int,readline().split())

MOD = 10**9+7

dx = [i-j for i,j in zip(x[1:],x)]
rx = 0
for i in range(n-1):
    rx += dx[i]*(i+1)%MOD*(n-i-1)%MOD

dy = [i-j for i,j in zip(y[1:],y)]
ry = 0
for i in range(m-1):
    ry += dy[i]*(i+1)%MOD*(m-i-1)%MOD

print(rx*ry%MOD)

Solution (Python):
n, m = map(int, input().split())
*xlines, = map(int, input().split())
*ylines, = map(int, input().split())

modulo = 10 ** 9 + 7
vs = 0
for i in range(len(xlines) - 1):
    vs += (xlines[i + 1] - xlines[i]) * (1 + i) * (len(xlines) - i - 1)
    vs %= modulo

s = 0
for j in range(len(ylines) - 1):
    s += vs * (ylines[j + 1] - ylines[j]) * (1 + j) * (len(ylines) - j - 1)
    s %= modulo
print(s)

Solution (Python):


import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
m, n = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
x.sort()
y.sort()
fx = [0]
fy = [0]
mod = 10 ** 9 + 7
x_sum = 0
y_sum = 0
for i in range(m - 2, -1, -1):
    fx.append((fx[-1] + (x[i + 1] - x[i]) * (m - 1 - i)) % mod)
    x_sum += fx[-1]
    x_sum %= mod
for i in range(n - 2, -1, -1):
    fy.append((fy[-1] + (y[i + 1] - y[i]) * (n - 1 - i)) % mod)
    y_sum += fy[-1]

    y_sum %= mod
print(x_sum * y_sum % mod)

Solution (Python):
n,m=[int(_) for _ in input().split()]

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

sumx=0
sumy=0
for i in range(n):
  sumx+=(2*i-n+1)*x[i]
  sumx%=1000000007
  
for i in range(m):
  sumy+=(2*i-m+1)*y[i]
  sumy%=1000000007
  
print((sumx*sumy)%1000000007)

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

dif_x=[(X[i+1]-X[i])*(i+1)*(n-i-1) for i in range(n-1)]
dif_y=[(Y[i+1]-Y[i])*(i+1)*(m-i-1) for i in range(m-1)]    

print(sum(dif_x)*sum(dif_y)%MOD)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)

def sort(a):
	a.sort()

def rev(a):
	a.reverse()

def sd():
	return int(input())

def _sd():
	return map(int,input().split())

def sa():
	return list(map(int,input().split()))

def ss():
	return input()

def sf():
	return float(input())
 
def main():
	n,m=_sd()
	X=sa()
	Y=sa()
	sum_x=int(0)
	sum_y=int(0)
	mod=int((1e+9)+7)
	for i in range(1,n+1):
		sum_x+=(i+i-n-1)*X[i-1]
		sum_x%=mod
	for i in range(1,m+1):
		sum_y+=(i+i-m-1)*Y[i-1]
		sum_y%=mod
	print((sum_x*sum_y)%mod)
main()

Solution (Python):


MOD = 10 ** 9 + 7

def main():
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    print(eq6_1(x) * eq6_1(y) % MOD)

def eq6_1(x):
    n = len(x)
    res = 0
    for j in range(n):
        res += j * x[j]
        res %= MOD
    for i in range(n):
        res -= (n - i - 1) * x[i]
        res %= MOD
    return res

if __name__ == "__main__":
    main()

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

MOD = 1000000007

w = sum(
  (x[i+1] - x[i]) * (i + 1) * (n - 1 - i) % MOD 
  for i
  in range(n-1)
) % MOD 

h = sum(
  (y[i+1] - y[i]) * (i + 1) * (m - 1 - i) % MOD 
  for i
  in range(m-1)
) % MOD 

print(w*h % 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()))
ansx=0
cum=x[0]
for i in range(1,n):
    ansx+=(i*x[i]-cum+mod)%mod
    ansx%=mod
    cum+=x[i]
ansy=0
cum=y[0]
for i in range(1,m):
    ansy+=(i*y[i]-cum+mod)%mod
    ansy%=mod
    cum+=y[i]

print((ansx*ansy)%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

x.sort()
y.sort()

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

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

print((X*Y)%mod)

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

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

class mint:
    def __init__(self, x):
        self.__x = x % md

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

    def __add__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x + other)

    def __sub__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x - other)

    def __rsub__(self, other):
        return mint(other - self.__x)

    def __mul__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * other)

    __radd__ = __add__
    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, mint): other = other.__x
        return mint(self.__x * pow(other, md - 2, md))

    def __pow__(self, power, modulo=None):
        return mint(pow(self.__x, power, md))

md = 10 ** 9 + 7
def main():
    n, m = MI()
    xx = LI()
    yy = LI()
    byi = mint(0)
    byj = mint(0)
    for j in range(m - 1):
        byj += (j + 1) * (m - j - 1) * (yy[j + 1] - yy[j])
    for i in range(n - 1):
        byi += (xx[i + 1] - xx[i]) * (i + 1) * (n - i - 1)
    print(byi * byj)

main()

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

Solution (Python):
mod = 10 ** 9 + 7
n, m = map(int, input().split())
print((sum(map(lambda x, y: (2 * y - n + 1) * int(x), input().split(), range(n))) * sum(map(lambda x, y: (2 * y - m + 1) * int(x), input().split(), range(m))) % mod + mod) % mod)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

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


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)

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

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

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

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



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)

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

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

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)

Solution (Python):

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)

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

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

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)

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

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

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

Solution (Python):

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

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

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

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

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

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


if __name__ == '__main__':
    main()