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):
N, M = map(int, input().split())
X = tuple(map(int, input().split()))
Y = tuple(map(int, input().split()))
MOD = 10 ** 9 + 7

X_sum = sum((2 * i - N + 1) * x for i, x in enumerate(X)) % MOD
Y_sum = sum((2 * i - M + 1) * y for i, y in enumerate(Y)) % MOD
print(X_sum * Y_sum % 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()
sx,sy=0,0
for k in range(1,n+1):
  sx+=(2*k-n-1)*x[k-1]
  sx%=mod
for k in range(1,m+1):
  sy+=(2*k-m-1)*y[k-1]
  sy%=mod
print((sx*sy)%mod)

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]

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

tmp1=0
tmp2=0
for i in range(n-1):
    tmp1+=(i+1)*(n-1-i)*(x[i+1]-x[i])
    tmp1%=mod
for j in range(m-1):
    tmp2+=(j+1)*(m-1-j)*(y[j+1]-y[j])
    tmp2%=mod


print(tmp1*tmp2%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

def f(Z):
    ans = 0
    for k,z in enumerate(Z):
        ans = (ans + k * z - (len(Z) - k - 1) * z) % MOD
    return ans

print(f(X) * f(Y) % MOD)

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

MOD = 10**9+7


def calc(v, l):
    r = 0
    for i in range(l):
        r = r + i*v[i] - (l-1-i)*v[i]
        r %= MOD
    return r


print((calc(x, n)*calc(y, m)) % MOD)

Solution (Python):
n,m = map(int,input().split())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
width,height = 0,0
a = -n-1
for k in range(1,n+1):
  width += x[k-1] * (2*k+a)
a = -m-1
for k in range(1,m+1):
  height += y[k-1] * (2*k+a)
width %= 1000000007
height %= 1000000007
print(width * height % 1000000007)

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

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 j in range(m-1):
    h += (y[j+1] - y[j]) * (j+1) * (m-j-1)
    h %= mod
print(h * w % mod)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
    
def main():
    n, m = LI()
    x = sorted(LI())
    y = sorted(LI())
    ans = 0
    for i in range(len(x)):
        k = i + 1
        ans += (k - 1 - n + k) * x[i]
    ysum = 0
    for j in range(len(y)):
        k = j + 1
        ysum += (k - 1 - m + k) * y[j]
    ans *= ysum
    ans %= mod
    print(ans)

main()

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

xsm = 0
for i, ex in enumerate(x, 1):
    xsm -= ex * (n - i)
    xsm += ex * (i - 1)
    xsm %= mod

ysm = 0
for i, ey in enumerate(y, 1):
    ysm -= ey * (m - i)
    ysm += ey * (i - 1)
    ysm %= mod

ans = xsm * ysm % mod
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,M=MI()
    x=LI()
    y=LI()
    dx=[0]*(N-1)
    dy=[0]*(M-1)
    for i in range(N-1):
        dx[i]=x[i+1]-x[i]
    for i in range(M-1):
        dy[i]=y[i+1]-y[i]
    
    NN=max(N,M)+3
    
    S=[0]*(NN+1)
    for i in range(1,NN+1):
        S[i]+=S[i-1]+i
        
    
    def calc(a,nm):
        if nm%2==1 and a==(nm+1)//2:
            return S[a]+S[a-1]
        else:
            return 2*S[a]+(nm-2*a)*a
    

    X=0
    for i in range(N-1):
        X+=calc(i+1,N-1)*dx[i]
        X%=mod
        
    Y=0
    for i in range(M-1):
        Y+=calc(i+1,M-1)*dy[i]
        Y%=mod
        
    print((X*Y)%mod)
    

main()

Solution (Python):
MOD = 1000000007

N, M = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, 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)]

sx = 0
sy = 0

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

for i in range(M - 1):
    sy += dy[i] * (i + 1) * (M - 1 - i)
    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()))
mod=10**9+7
ans,ans2=0,0 
for i in range(n):
    ans=(ans+(-n+2*i+1)*x[i])%mod
for i in range(m):
    ans2=(ans2+(-m+2*i+1)*y[i])%mod
print(ans*ans2%mod)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def main():
    N,M = map(int,input().split())
    x = list(map(int,input().split()))
    y = list(map(int,input().split()))
    MOD = 10**9+7
    
    ax,ay = 0,0
    for i,num in enumerate(x):
        ax += num*(i-(N-i-1))
        ax %= MOD
    
    for j,num in enumerate(y):
        ay += num*(j-(M-j-1))
        ay %= MOD
        
    print((ax*ay)%MOD)

if __name__ == "__main__":
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n,m = MAP()
x = LIST()
y = LIST()
xsum = 0
for i in range(n):
    xsum -= (n - 1 - i)* x[i]
    xsum += i * x[i]
    xsum %- mod
ysum = 0
for i in range(m):
    ysum -= (m - 1 - i)* y[i]
    ysum += i * y[i]
    ysum %= mod
print(xsum * ysum % mod)

Solution (Python):
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()))
f = 0
for j in range(m-1):
    f = (f + (y[j+1]-y[j])*(j+1)*(m-j-1)) % mod
ans = 0
for i in range(n-1):
    ans = (ans + (x[i+1]-x[i])*(i+1)*(n-i-1)*f) % mod
print(ans)

Solution (Python):
num = input().split(" ")
zahyou_x = input().split(" ")
zahyou_y = input().split(" ")
x_num = int(num[0])
y_num = int(num[1])
x_sum = 0
y_sum = 0

for i in range(x_num):
    x_sum += int(zahyou_x[i]) * (i*2 - x_num + 1)

for i in range(y_num):
    y_sum += int(zahyou_y[i]) * (i*2 - y_num + 1)
print(x_sum*y_sum%(10**9+7))

Solution (Python):
import sys

input = sys.stdin.readline


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


def read_list():
    return list(read_values())


def func(N, mod):
    F = [1]
    for i in range(1, N + 1):
        F.append(F[-1] * i % mod)
    return F


INV = {}


def inv(a, mod):
    if a in INV:
        return INV[a]
    r = pow(a, mod - 2, mod)
    INV[a] = r
    return r


def C(F, a, b, mod):
    return F[a] * inv(F[b], mod) * inv(F[a - b], mod) % mod


def main():
    mod = 10 ** 9 + 7
    N, M = read_values()
    X = read_list()
    Y = read_list()

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

    res_y = 0
    for i in range(M - 1):
        res_y += (i + 1) * (M - i - 1) * (Y[i + 1] - Y[i]) % mod
        res_y %= mod
    print(res_x * res_y % mod)


if __name__ == "__main__":
    main()

Solution (Python):
n,m=map(int,input().split())
lists=list(map(int,input().split()))
sub=list(map(int,input().split()))
mod=10**9+7
tate=0
yoko=0
for i in range(n):
    tate+=lists[i]*(2*(i+1)-1-n)
for j in range(m):
    yoko+=sub[j]*(2*(j+1)-1-m)
print(((tate%mod)*(yoko%mod))%mod)

Solution (Python):
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 
N = 10**4
g1 = [1, 1] 
g2 = [1, 1] 
inverse = [0, 1] 

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

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

X = [0] + sorted(list(map(int,input().split())))
Y = [0] + sorted(list(map(int,input().split())))
sum1 = 0
sum2 = 0
MOD = 10 ** 9 + 7
for i in range(1, n + 1):
    sum1 += ((i - 1)*X[i] - (n - i)*X[i]) % MOD
for i in range(1, m + 1):
    sum2 += ((i - 1)*Y[i] - (m - i)*Y[i])%MOD
print(sum1 * sum2 % MOD)

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

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7
n, m = lr()
X = np.array(lr())
Y = np.array(lr())
coef_x = np.arange(n)*2 - (n-1)
xsum = (X * coef_x % MOD).sum() % MOD
coef_y = np.arange(m)*2 - (m-1)
ysum = (Y * coef_y % MOD).sum() % MOD
answer = xsum * ysum % MOD
print(answer)

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7
n, m = lr()
X = np.array(lr())
Y = np.array(lr())
coef_x = np.arange(n)*2 - (n-1)
X *= coef_x
xsum = (X%MOD).sum() % MOD
coef_y = np.arange(m)*2 - (m-1)
Y *= coef_y
ysum = (Y%MOD).sum() % MOD
answer = xsum * ysum % MOD
print(answer % MOD)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) 
def inpm(): return map(int,input().split()) 
def inpl(): return list(map(int, input().split())) 
def inpls(): return list(input().split())  
def inplm(n): return list(int(input()) for _ in range(n)) 
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) 

def main():
    n,m = inpm()
    x = inpl()
    y = inpl()
    X = 0
    Y = 0
    cntX, cntY = n-1, m-1
    for i in range(n-1,-1,-1):
        X += x[i]*cntX
        X %= mod
        cntX -= 2
    for i in range(m-1,-1,-1):
        Y += y[i]*cntY
        Y %= mod
        cntY -= 2
    print((X*Y)%mod)


    
if __name__ == "__main__":
    main()

Solution (Python):
N, M = map(int, input().split())
MOD = 10**9+7
x = [int(c) for c in input().split()]
y = [int(c) for c in input().split()]
X = 0
Y = 0
for i, c in enumerate(x):
  X += i*c-(N-1-i)*c
  X %= MOD
for i, c in enumerate(y):
  Y += i*c-(M-1-i)*c
  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()))
sum_x = 0
sum_y = 0
for i in range(n):
    sum_x += x[i]*i
    sum_x -= x[i]*(n-1-i)
for i in range(m):
    sum_y += y[i]*i
    sum_y -= y[i]*(m-1-i)
mod = 10**9+7
print((sum_x%mod)*(sum_y%mod)%mod)

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

def solve(N, X):
    return sum((2 * k - N + 1) * x for k, x in enumerate(X)) % mod


N, M, *XY = map(int, open(0).read().split())
X, Y = XY[:N], XY[N:]

print(solve(N, X) * solve(M, Y) % mod)

Solution (Python):
m,n = map(int,input().split())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
MOD = 1000000007
sum_x = 0
sum_y = 0
for k in range(m):
  sum_x += (2*k +1 -m)*X[k]
sum_x %= MOD
for l in range(n):
  sum_y += (2*l + 1 - n)*Y[l]
sum_y %= MOD
Answer = (sum_x*sum_y) % MOD
print(Answer)

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


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

Solution (Python):
MOD=10**9+7
class Fp(int):
    def __new__(self,x=0):return super().__new__(self,x%MOD)
    def inv(self):return self.__class__(super().__pow__(MOD-2,MOD))
    def __add__(self,value):return self.__class__(super().__add__(value))
    def __sub__(self,value):return self.__class__(super().__sub__(value))
    def __mul__(self,value):return self.__class__(super().__mul__(value))
    def __floordiv__(self,value):return self.__class__(self*self.__class__(value).inv())
    def __pow__(self,value):return self.__class__(super().__pow__(value%(MOD-1), MOD))
    __radd__=__add__
    __rmul__=__mul__
    def __rsub__(self,value):return self.__class__(-super().__sub__(value))
    def __rfloordiv__(self,value):return self.__class__(self.inv()*value)
    def __iadd__(self,value):self=self+value;return self
    def __isub__(self,value):self=self-value;return self
    def __imul__(self,value):self=self*value;return self
    def __ifloordiv__(self,value):self=self//value;return self
    def __ipow__(self,value):self=self**value;return self
    def __neg__(self):return self.__class__(super().__neg__())

import sys
sys.stdin.readline

def main():    
    n, m = map(int, input().split())
    X = list(map(int, input().split()))
    Y = list(map(int, input().split()))
    n = Fp(n)
    m = Fp(m)
    print(sum((n - i) * i * (X[i] - X[i-1]) for i in range(1, n)) * sum((m - i) * i * (Y[i] - Y[i-1]) for i in range(1, m)))

if __name__ == "__main__":
    main()

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()
    ans=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):
        ans+=(xx[i+1]-xx[i])*(i+1)*(n-i-1)*byj

    print(ans)

main()

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

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

tmp_x = 0
tmp_y = 0
mod = 10**9+7
for i, xi in enumerate(x):
    tmp_x += (xi+1)*(2*i-(n-1))
    
tmp_x = tmp_x % mod

for i, yi in enumerate(y):
    tmp_y += (yi+1)*(2*i-(m-1))
    
tmp_y = tmp_y % mod

print((tmp_x*tmp_y)%mod)

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

print(xsum*ysum%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
sx, sy = 0, 0
for i in range(n):
  t = 2*i+1-n
  t *= X[i]
  t %= MOD
  sx += t
  sx %= MOD
for j in range(m):
  t = 2*j+1-m
  t *= Y[j]
  t %= MOD
  sy += t
  sy %= MOD
print(sx*sy%MOD)

Solution (Python):


import sys
from operator import add

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

class SegTree:
    """
    セグメント木
    1.update:  i番目の値をxに更新する
    2.query: 区間[l, r)の値を得る
    """

    def __init__(self, n, func, intv, A=[]):
        """
        :param n: 要素数(0-indexed)
        :param func: 値の操作に使う関数(min, max, add, gcdなど)
        :param intv: 要素の初期値(単位元)
        :param A: 初期化に使うリスト(オプション)
        """
        self.n = n
        self.func = func
        self.intv = intv
        
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.intv] * (n2 << 1)
        
        if A:
            
            for i in range(n):
                self.tree[n2+i] = A[i]
            
            for i in range(n2-1, -1, -1):
                self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])

    def update(self, i, x):
        """
        i番目の値をxに更新
        :param i: index(0-indexed)
        :param x: update value
        """
        i += self.n2
        self.tree[i] = x
        while i > 0:
            i >>= 1
            self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])

    def query(self, a, b):
        """
        [a, b)の値を得る
        :param a: index(0-indexed)
        :param b: index(0-indexed)
        """
        l = a + self.n2
        r = b + self.n2
        s = self.intv
        while l < r:
            if r & 1:
                r -= 1
                s = self.func(s, self.tree[r])
            if l & 1:
                s = self.func(s, self.tree[l])
                l += 1
            l >>= 1
            r >>= 1
        return s

    def get(self, i):
        """ 一点取得 """
        return self.tree[i+self.n2]

    def all(self):
        """ 全区間[0, n)の取得 """
        return self.tree[1]

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


A = [a-A[0] for a in A]
B = [b-B[0] for b in B]


st1 = SegTree(N, add, 0, A)
st2 = SegTree(M, add, 0, B)
h = w = 0

for i in range(N-1):
    h += st1.query(i+1, N) - st1.get(i) * (N-i-1)
    h %= MOD
for i in range(M-1):
    w += st2.query(i+1, M) - st2.get(i) * (M-i-1)
    w %= MOD
ans = h * w % MOD
print(ans)

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

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

Solution (Python):

import sys

MOD = 1000000007  


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

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  
    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):
mod=1000000007
n,m=map(int,input().split())
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]

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

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

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

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

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

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

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

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

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

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

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

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

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

print((x_sum*y_sum)%MOD)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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


lsm = rsm = 0
for i in range(N):
    lsm += A[i] * (N-i-1)
    rsm += A[i] * i
    lsm %= MOD
    rsm %= MOD
h = rsm - lsm
lsm = rsm = 0
for i in range(M):
    lsm += B[i] * (M-i-1)
    rsm += B[i] * i
    lsm %= MOD
    rsm %= MOD
w = rsm - lsm

ans = h * w % MOD
print(ans)

Solution (Python):


import sys
from itertools import accumulate

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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


A2 = []
for i in range(N-1):
    A2.append(A[i+1] - A[i])
B2 = []
for i in range(M-1):
    B2.append(B[i+1] - B[i])
A2 = [0] + list(accumulate(A2))
B2 = [0] + list(accumulate(B2))


lsm = rsm = 0
for i in range(N):
    lsm += A2[i] * (N-i-1)
    rsm += A2[i] * i
    lsm %= MOD
    rsm %= MOD
h = rsm - lsm
lsm = rsm = 0
for i in range(M):
    lsm += B2[i] * (M-i-1)
    rsm += B2[i] * i
    lsm %= MOD
    rsm %= MOD
w = rsm - lsm

ans = h * w % MOD
print(ans)

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

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

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

print(X * Y % mod)

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

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

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

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

X = list(map(int, input().split()))
Dx = []
for i in range(n-1):
    Dx.append(X[i+1]-X[i])

S = [0]
for x in Dx:
    S.append((S[-1]+x) % mod)

T = [0]
for s in S:
    T.append((T[-1]+s) % mod)

Ux = 0
for i in range(1, n):
    Ux += (S[i]*i-T[i]) % mod
    



Y = list(map(int, input().split()))
Dy = []
for i in range(m-1):
    Dy.append(Y[i+1]-Y[i])

S = [0]
for y in Dy:
    S.append((S[-1]+y) % mod)

T = [0]
for s in S:
    T.append((T[-1]+s) % mod)

Uy = 0
for i in range(1, m):
    Uy += (S[i]*i-T[i]) % mod
    



print(Ux*Uy%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
def f(N,i):
  return (i*(N-i+1))%mod
def l(L,i):
  return (L[i]-L[i-1])%mod
def lf(L):
  N = len(L)
  return sum([(l(L,i)*f(N-1,i))%mod  for i in range(1,N)])
print(lf(x)*lf(y)%mod)

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

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

print((h_total*w_total)%mod)

Solution (Python):

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

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


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

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


print(sum_x*sum_y%MOD)

Solution (Python):
import sys
import numpy as np

MOD = 10 ** 9 + 7

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

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

Solution (Python):
import sys
import numpy as np

MOD = 10 ** 9 + 7

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

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

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

    ans = sdx * sdy % MOD
    return ans

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