Problem:


Score : 500 points

Problem Statement

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

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

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

Constraints

2 \leq n,m \leq 10^5

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

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

x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 3
1 3 4
1 3 6

Sample Output 1

60

The following figure illustrates this input:

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

Sample Input 2

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

Sample Output 2

835067060



Sample input:
3 3
1 3 4
1 3 6


Sample output:
60

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A

#B

#C
"""
n = I()
s = SR(n)
for i in range(n):
    for j in range(len(s[i])):
        while s[i][j] in s[i][:j]:
            s[i][j] = "0"+s[i][j]
ans = set(s[0])
for i in range(1,n):
    ans &= set(s[i])
ans = sorted(list(ans))
if len(ans) == 0:print()
else:
    for i in range(len(ans)):
        ans[i] = ans[i][-1]
    ans.sort()
    for i in range(len(ans)-1):
        print(ans[i],end = "")
    print(ans[-1])
"""

#D
n,m = LI()
ln = [n-1]
lm = [m-1]
a = n-1
b = m-1
while a > 2:
    a -= 2
    ln.append(ln[-1]+a)
while b > 2:
    b -= 2
    lm.append(lm[-1]+b)
la = len(ln)
lb = len(lm)
for i in range(la):
    if i or a == 2:
        ln.append(ln[la-i-1])
for i in range(lb):
    if i or b == 2:
        lm.append(lm[lb-i-1])
x,y = LIR(2)
lx = 0
ly = 0
for i in range(n-1):
    lx += ln[i]*(x[i+1]-x[i])%mod
    lx %= mod
for i in range(m-1):
    ly += lm[i]*(y[i+1]-y[i])%mod
    ly %= mod
ans = lx*ly%mod
print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

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

keisu = len(x)-1
keisu_kaisa = keisu
ans = 0
for i in range(1,n):
  #print(keisu)
  ans = ans + 1*(x[i]-x[i-1])*keisu
  keisu_kaisa = keisu_kaisa -2
  keisu = keisu +(keisu_kaisa)
  ans = ans % mod
  
keisu = len(y)-1
keisu_kaisa = keisu
ans_2 = 0
for i in range(1,m):
  ans_2 = ans_2 + ans*(y[i]-y[i-1])*keisu
  keisu_kaisa = keisu_kaisa -2
  keisu = keisu +(keisu_kaisa)
  ans_2 = ans_2 % mod  
print(ans_2)

Submission status: Accepted

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

keisu = len(x)-1
keisu_kaisa = keisu
ans = 0
for i in range(1,n):
  #print(keisu)
  ans = ans + 1*(x[i]-x[i-1])*keisu
  keisu_kaisa = keisu_kaisa -2
  keisu = keisu +(keisu_kaisa)
  ans = ans % mod
  
keisu = len(y)-1
keisu_kaisa = keisu
ans_2 = 0
for i in range(1,m):
  ans_2 = ans_2 + ans*(y[i]-y[i-1])*keisu
  keisu_kaisa = keisu_kaisa -2
  keisu = keisu +(keisu_kaisa)
  ans_2 = ans_2 % mod  
print(ans_2)

Submission status: Accepted

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

xsum = 0
xx = [int(i) for i in input().split()]
for i, x in enumerate(xx):
    xsum += (2 * i - n + 1) * x

ysum = 0
yy = [int(i) for i in input().split()]
for i, y in enumerate(yy):
    ysum += (2 * i - m + 1) * y

print(xsum * ysum % 1000000007)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

"""
参考：https://img.atcoder.jp/arc071/editorial.pdf
・各辺を加減する回数には規則性があるので、ループを1つ減らせる。
"""

import sys
def input(): return sys.stdin.readline().strip()
sys.setrecursionlimit(10 ** 9)

MOD = 10 ** 9 + 7

N, M = map(int, input().split())
xN = list(map(int, input().split()))
yM = list(map(int, input().split()))

# 辺の長さの合計を出す
x_sm = 0
for i in range(N):
    x_sm = (x_sm + i * xN[i] - (N-i-1) * xN[i]) % MOD
y_sm = 0
for i in range(M):
    y_sm = (y_sm + i * yM[i] - (M-i-1) * yM[i]) % MOD

print(x_sm * y_sm % MOD)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x_list = tuple(map(int, input().split()))
y_list = tuple(map(int, input().split()))
mod = 10 ** 9 + 7
x_sum = 0
for i in range(1, n + 1):
    x_sum += ((i - 1) * x_list[i - 1] - (n - i) * x_list[i - 1]) % mod
y_sum = 0
for i in range(1, m + 1):
    y_sum += ((i - 1) * y_list[i - 1] - (m - i) * y_list[i - 1]) % mod
print(x_sum * y_sum % mod )

Submission status: Accepted

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

mulx = []
for i in range(1, n):
    mulx.append(i * (n - i))

ansx = 0
for i in range(n - 1):
    ansx += bx[i] * mulx[i]
    #ansx = ansx % mod
ansx = ansx * (m - 1)
#ansx = ansx % mod


by = []
for i in range(1, m):
    by.append(abs(y[i] - y[i - 1]))

muly = []
for i in range(1, m):
    muly.append(i * (m - i))


ans = 0
for i in range(m - 1):
    ans += by[i] * ansx * muly[i] // muly[0]
    ans = ans % mod

ans = ans % mod
print(ans)

Submission status: Accepted

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

mod = 10 ** 9 + 7

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

print(x_sum * y_sum % mod )

Submission status: Accepted

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

X_SUM, Y_SUM = 0, 0
for i, x in enumerate(X):
    X_SUM = (X_SUM + x * (i - (N - i - 1))) % mod

for i, y in enumerate(Y):
    Y_SUM = (Y_SUM + y * (i - (M - i - 1))) % mod

print((X_SUM * Y_SUM) % mod)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
modulo = 10**9+7

x,y = 0,0
for i in range(N):
    x += X[i]*i - X[i]*(N-i-1)
for i in range(M):
    y += Y[i]*i - Y[i]*(M-i-1)

print(x*y%modulo)

Submission status: Accepted

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

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

X=[]
for i in range(1,len(x)):
    X.append(x[i]-x[i-1])
    
Y=[]
for j in range(1,len(y)):
    Y.append(y[j]-y[j-1])

A=len(X)
for a in range(int((len(X)+1)/2)):
    X[a]*=A
    if X[a]==X[len(X)-1-a]:
        pass
    else:
        X[len(X)-1-a]*=A
    A=A+len(X)-2*(a+1)
    
SX=sum(X)

B=len(Y)
for b in range(int((len(Y)+1)/2)):
    Y[b]*=B
    if Y[b]==Y[len(Y)-1-b]:
        pass
    else:
        Y[len(Y)-1-b]*=B
    B=B+len(Y)-2*(b+1)
    
SY=sum(Y)

print((SX*SY)%(10**9+7))

Submission status: Accepted

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

XS = 0
for i, x in enumerate(X, start=1):
    XS += (x * ((i - 1) - (N - i))) % mod
YS = 0
for i, y in enumerate(Y, start=1):
    YS += (y * ((i - 1) - (M - i))) % mod

print(XS * YS % mod)

Submission status: Accepted

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

x=0

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

y = 0

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

print(x*y%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
MOD = 10 ** 9 + 7
numx = 0
for i in range(N):
    numx += -(N - i - 1) * X[i] + i * X[i]
numy = 0
for i in range(M):
    numy += -(M - i - 1) * Y[i] + i * Y[i]

print(numy * numx % MOD)

Submission status: Accepted

Solution code:
#058-D
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+=i*x[i]-(n-i-1)*x[i]
for i in range(m):
    sy+=i*y[i]-(m-i-1)*y[i]
sx=sx%(10**9+7)
sy=sy%(10**9+7)
ans=sx*sy%(10**9+7)
print(ans)

Submission status: Accepted

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

xtotal, ytotal = 0, 0
for i in range(n-1):
    xarea = x[i+1] - x[i]
    xright, xleft =  n - i - 1, i + 1
    xtotal += (xarea * xright * xleft) %mod
    xtotal %= mod
for j in range(m-1):
    yarea = y[j+1] - y[j]
    yup = m - j -1
    ydown = j + 1
    ytotal += (yarea * yup * ydown) % mod
    ytotal % mod

print((xtotal * ytotal) % mod)

Submission status: Accepted

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

MOD = 10 ** 9 + 7

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

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

print( (x_sum * y_sum) % MOD )

Submission status: Accepted

Solution code:
Q = 10**9 + 7
def zen(A,k):
    jiku = 0
    for i in range(k):
        jiku += i*A[i] - (k-1-i)*A[i]
    return jiku%Q
n, m = map( int, input().split())
X = list(map( int, input().split()))
Y = list(map( int, input().split()))
ans = zen(X,n)*zen(Y,m)
ans %= Q
print(ans)

Submission status: Accepted

Solution code:
def wa(ka, li):
    t = 0
    i = 0
    while ka > 0:
        t = (t + (li[-(i + 1)] - li[i]) * (ka - 1)) % (10 ** 9 + 7)
        ka -= 2
        i += 1
    return t


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

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def main():
    n, m = map(int, input().split())
    x_list = list(sorted(list(map(int, input().split()))))
    y_list = list(sorted(list(map(int, input().split()))))
    MOD = 10 ** 9 + 7

    x = 0
    for i, d in enumerate(range(-(n - 1), n, 2)):
        x += d * x_list[i]
        x %= MOD

    y = 0
    for i, d in enumerate(range(-(m - 1), m, 2)):
        y += d * y_list[i]
        y %= MOD
        
    print((x * y) % MOD)




if __name__ == '__main__':
    main()

Submission status: Accepted

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

sum_x = 0
sum_y = 0

for i in range(n):
    sum_x += (2 * i + 1 - n) * x[i] % mod
for j in range(m):
    sum_y += (2 * j + 1 - m) * y[j] % mod

print((sum_x * sum_y) % mod)

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
X = list(map(int,input().split()))
Y = list(map(int,input().split()))
MOD = 10**9+7

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

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

print((w_sum * h_sum) % MOD)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

def li(): return [int(x) for x in stdin.readline().split()]
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return [float(x) for x in stdin.readline().split()]
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(ns())
def nf(): return float(ns())

MOD = 10**9+7

n,m = li()
x = li()
y = li()

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

x_coeff = [n-(2*i+1) for i in range(n)]
y_coeff = [m-(2*i+1) for i in range(m)]

x_sum = 0
y_sum = 0

for xi, xcoefi in zip(x, x_coeff):
    x_sum += (xi * xcoefi)
    x_sum %= MOD
    
for yi, ycoefi in zip(y, y_coeff):
    y_sum += (yi * ycoefi)
    y_sum %= MOD

ans = x_sum * y_sum
print(ans%MOD)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  8 11:35:12 2018

@author: maezawa
"""

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

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

ay = 0
for j in range(1,m):
    ay += (m-j)*j*(y[j]-y[j-1])%(10**9+7)
    
print(ax*ay%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

N,M = inpl()
xx = inpl()
yy = inpl()

l = 0
r = M-1
ySUM = 0
while True:
	if r-l <= 2:
		ySUM += (yy[r]-yy[l])*(r-l)%mod
		break
	else:
		ySUM += (yy[r]-yy[l])*(r-l)%mod
		r,l = r-1,l+1

l = 0
r = N-1
xSUM = 0
while True:
	if r-l <= 2:
		xSUM += (xx[r]-xx[l])*(r-l)%mod
		break
	else:
		xSUM += (xx[r]-xx[l])*(r-l)%mod
		r,l = r-1,l+1

print(xSUM*ySUM%mod)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, X, Y):
    MOD = 10**9+7

    xs = 0
    for i in range(N):
        xs += (i-(N-(i+1)))*X[i]
        xs %= MOD
    ys = 0
    for i in range(M):
        ys += (i-(M-(i+1)))*Y[i]
        ys %= MOD
    return xs*ys % MOD


def main():
    N, M = read_int_n()
    X = read_int_n()
    Y = read_int_n()
    print(slv(N, M, X, Y))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

MOD = 10**9+7

X_sum = 0
for i in range(n):
    X_sum += (2*i-n+1)*X[i]
    X_sum = X_sum % MOD
    
Y_sum = 0
for j in range(m):
    Y_sum += (2*j-m+1)*Y[j]
    Y_sum = Y_sum % MOD
    
print(X_sum * Y_sum % MOD)

Submission status: Accepted

Solution code:
I=lambda:list(map(int,input().split()))
I()
F=lambda x:sum(j*(len(x)-1-2*i)for i,j in enumerate(x))
print(F(I())*F(I())%(10**9+7))

Submission status: Accepted

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

MOD = 10**9+7

x_diff = 0
y_diff = 0

for i in range(N):
    x_diff += (2*i-N+1)*X[i]%MOD
    
for i in range(M):
    y_diff += (2*i-M+1)*Y[i]%MOD
    
print(x_diff*y_diff%MOD)

Submission status: Accepted

Solution code:
I=lambda:[int(i)for i in input().split()]
n,m=I()
N=I()
M=I()
F=lambda x,y:sum(j*(y-1-2*i)for i,j in enumerate(x))
print(F(N,n)*F(M,m)%(10**9+7))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#x軸の選び方がm*(m-1)/2,y軸がn*(n-1)/2、よって長方形の面積計算はO(m^2*n^2)で当然のTLE。
#サンプル１を例にして考える。
#まずx軸の辺は2,1,3の3通り(m*(m-1)/2=3)、同様にy軸は2,3,5。
#ans=2*2+2*3+2*5+1*2+1*3+1*5+3*2+3*3+3*5=60
#上記の式はans=(2+1+3)*(2+3+5)=60と変形できることに気付く。これでO(m|n^2)まで計算量を落とせる。
#ただし、m<=10**5なので、まだTLE。ここでm*(m-1)/2通りの各辺の長さを効率的に求めることを考える。
#m0,m1,m2がソートされていたとして、3通りの辺はm1-m0,m2-m0,m2-m1。
#ここで各点が加算に使われる場合と減算に使われる場合に法則があると気付く。
#具体的に長さNの数列において、点iが加算に使われる回数はi回,減算に使われる回数は(N-i-1)回
#つまりans+=A[i]*i-A[i]*(N-i-1)としてi=0~N-1までの総和を取ればよいことが分かる。
#これはO(N)なので、全体としてO(N+N)=O(N)で答えを求められると思われる。

MOD=10**9+7

N,M=map(int,input().split())
X=[int(x) for x in input().split()]
Y=[int(y) for y in input().split()]

#Y軸に平行な線分の集合の総和を求める
sumX=0
for i in range(N):
  sumX+=(X[i]*i-X[i]*(N-i-1))%MOD
  sumX%=MOD
  
#X軸に平行な線分の集合の総和を求める
sumY=0
for i in range(M):
  sumY+=(Y[i]*i-Y[i]*(M-i-1))%MOD
  sumY%=MOD
  
print((sumX*sumY)%MOD)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
X = [int(x) for x in input().split()]
Y = [int(x) for x in input().split()]
def f(a, n):
    return sum([(n - 1 - 2*i) * x for i, x in enumerate(reversed(a))])
print(f(X, n) * f(Y, m) % (10**9 + 7))

Submission status: Accepted

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

Submission status: Accepted

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

def s(n,i):
	return i * (n-(i-1))

def xx(x,n):
	t = 0
	for i in range(n-1):
		t = (t + s(n-1,i+1) * (x[i+1]-x[i])) % P
	return t

R = (xx(x,n) * xx(y,m)) % P 
print(R)

Submission status: Accepted

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

mod = 10 ** 9 + 7
a = 0
for k in range(n):
    a += k * x[k] - (n - (k + 1)) * x[k]
b = 0
for k in range(m):
    b += k * y[k] - (m - (k + 1)) * y[k]

print(a * b % mod)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sum_x = 0
sum_y = 0
for i in range(n):
    sum_x += (n-1-i*2)*X[i] 
for i in range(m):
    sum_y += (m-1-i*2)*Y[i]
sum_x = sum_x%MOD
sum_y = sum_y%MOD
print((sum_x*sum_y)%MOD)

Submission status: Accepted

Solution code:
n,m = (int(i) for i in input().split())
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
difx,dify,num,num2,mod = [],[],0,0,10**9+7
for i in range(1,n): difx.append(x[i]-x[i-1])
for i in range(1,m): dify.append(y[i]-y[i-1])
for i in range(n-1): num = (num+(i+1)*(n-i-1)*difx[i])%mod
for i in range(m-1): num2 = (num2+(i+1)*(m-i-1)*dify[i])%mod
print(num*num2%mod)

Submission status: Accepted

Solution code:
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(1,n+1):
    sx += (x[i - 1]*(i - 1)) - (x[i - 1] * (n - i))
for s in range(1,m+1):
    sy += (y[s - 1]*(s - 1)) - (y[s - 1] * (m - s))
print(abs(sx * sy)%1000000007)

Submission status: Accepted

Solution code:
# coding=utf-8

line = input().split(" ")
n = int(line[0])
m = int(line[1])

x = 0
line1 = input().split(" ")
for (k,nn) in enumerate(line1):
    nn = int(nn)
    x += k*nn - (n-k-1)*nn


y = 0
line2 = input().split(" ")
for (k,nn) in enumerate(line2):
    nn = int(nn)
    y += k*nn - (m-k-1)*nn

print((x*y)%(10**9+7))

Submission status: Accepted

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

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

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


print x_sum*y_sum%(10**9+7)

Submission status: Accepted

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

sum_x = sum_y = 0

l = 0
r = (n-1)

while l < r:
	sum_x += ((r-l) * abs(X[r] - X[l]))
	r -= 1
	l += 1

l = 0
r = (m-1)
while l < r:
	sum_y += ((r-l) * abs(Y[r] - Y[l]))
	r -= 1
	l += 1

print((sum_x*sum_y) % mod)

Submission status: Accepted

Solution code:
def d_igeta(N, M, X, Y):
    tmp1 = 0
    tmp2 = 0
    for k, x in enumerate(X):
        k += 1
        tmp1 += (k - 1) * x - (N - k) * x
    for k, y in enumerate(Y):
        k += 1
        tmp2 += (k - 1) * y - (M - k) * y
    return (tmp1 * tmp2) % (10**9 + 7)

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()]
print(d_igeta(N, M, X, Y))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
a = [x[i+1] - x[i] for i in range(n-1)]
b = [y[i+1] - y[i] for i in range(m-1)]

MOD = int(1e9+7)
ai = sum([a[i] * (i+1) * max((0, (n-1-i))) % MOD for i in range(n-1)])
bi = sum([b[i] * (i+1) * max((0, (m-1-i))) % MOD for i in range(m-1)])
ans = ai * bi % MOD
print(ans)

Submission status: Accepted

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

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

MODULO = 10**9 +7

def MSum(x, y):
    return (x + y) % MODULO

def MPro(x, y):
    return (x * y) % MODULO

def foo(zs, k):
    s = 0
    for i in range(k):
       s = MSum(s, MPro(MPro(zs[i], k-i), i+1))
    return s

def diff(zs, k):
    previous = None
    rs = [0] * (k-1)
    for i, z in enumerate(zs):
        if i == 0:
            previous = z
            continue
        rs[i-1] = z - previous
        previous = z
    return rs

dx = diff(xs, n)
dy = diff(ys, m)

print(MPro(foo(dx, n-1), foo(dy, m-1)))

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
xs = list(map(int, input().split()))
ys = list(map(int, input().split()))
MOD = int(1e9 + 7)
row = sum([(i+1) * (n-i-1) * (xs[i+1] - xs[i]) % MOD for i in range(n-1)]) % MOD
col = sum([(i+1) * (m-i-1) * (ys[i+1] - ys[i]) % MOD for i in range(m-1)]) % MOD
ans = row * col % MOD
print(ans)

Submission status: Accepted

Solution code:
s=input()
s=s.split()
n,m=int(s[0]),int(s[1])
x=[int(i) for i in input().split()]
y=[int(i) for i in input().split()]
mod =10**9 + 7
xrange=0
for i in range(n):
    xrange-=x[i]*(n-i*2-1)
yrange=0
for i in range(m):
    yrange-=(m-i*2-1)*y[i];
ans=(xrange*yrange)%mod
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

q = 10**9+7

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

print(  ((x_sum * y_sum )%q) )

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M = map(int,input().split())
xs = list(map(int,input().split()))
ys = list(map(int,input().split()))
MOD = 10**9+7

total_w = total_h = 0
for n in range(1,N):
    w = xs[n] - xs[n-1]
    w = (w * n * (N-n)) % MOD
    total_w = (total_w + w) % MOD
for n in range(1,M):
    h = ys[n] - ys[n-1]
    h = (h * n * (M-n)) % MOD
    total_h = (total_h + h) % MOD
ans = (total_w * total_h) % MOD
print(ans)

Submission status: Accepted

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

for k in range(1,n+1):
    a += (k-1)*x[k-1] - (n-k)*x[k-1]
for j in range(1,m+1):
    b += (j-1)*y[j-1] - (m-j)*y[j-1]

res = a*b
print(a*b%Mod)

Submission status: Accepted

Solution code:
from collections import defaultdict

MOD = 10 ** 9 + 7


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

    a = 0
    for k in range(1, n + 1):
        a += (k - 1) * x_list[k - 1] - (n - k) * x_list[k - 1]

    b = 0
    for k in range(1, m + 1):
        b += (k - 1) * y_list[k - 1] - (m - k) * y_list[k - 1]

    print((a % MOD * b % MOD) % MOD)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('d1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


n, m = read_int_list()
x = read_int_list()
y = read_int_list()


def compute_sum(q, a):
    return sum([(2 * p - q - 1) * a[p - 1] for p in range(1, q + 1)])


M = 10 ** 9 + 7
s = compute_sum(n, x)
t = compute_sum(m, y)
res = s * t % M
print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n,m= map(int, input().split())
x=list(map(int, input().split()))
y=list(map(int, input().split()))
yoko=0
tate=0
for i in range (0,len(x)):
  yoko+=(n-2*i-1)*x[i]
for i in range (0,len(y)):
  tate+=(m-2*i-1)*y[i]

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

Submission status: Accepted

Solution code:
### D - 井井井 / ###　やりなおし
x, y = [int(i) for i in input().split()]
xl = [int(i) for i in input().split()]
yl = [int(i) for i in input().split()]

xx = sum(  [ i*xl[i] - (x-i-1)*xl[i] for i in range(len(xl)) ]  )
yy = sum(  [ i*yl[i] - (y-i-1)*yl[i] for i in range(len(yl)) ]  )
ans = (xx * yy) % (10**9 + 7)
print(ans)

Submission status: Accepted

Solution code:
b = input().split()
c = [int(b[i]) for i in range(len(b))]

d = input().split()
e = [int(d[i]) for i in range(c[0])]

r = input().split()
f = [int(r[i]) for i in range(c[1])]

ansx = 0
for i in range(c[0]):
  tmp = i*e[i] - (c[0]-i-1)*e[i]
  ansx += tmp

ansy = 0
for j in range(c[1]):
  tmp = j*f[j] - (c[1]-j-1)*f[j]
  ansy += tmp

print((ansx * ansy) % (10**9 + 7))

Submission status: Accepted

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

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

def sum_length(x, n):
    all_sum = 0
    prev_sum = 0
    for i in range(1, n):
        cur_sum = prev_sum + (x[i] - x[i-1]) * i
        all_sum = all_sum + cur_sum
        prev_sum = cur_sum
    return all_sum

sumx = sum_length(x, n)
sumy = sum_length(y, m)
print(sumx * sumy % 1000000007)

Submission status: Accepted

Solution code:
import sys
n,m = [int(x) for x in sys.stdin.readline().split()]
x = [int(x) for x in sys.stdin.readline().split()]
y = [int(x) for x in sys.stdin.readline().split()]
r = 0
mod = 10**9+7
def sumof(a,b):
	s = 0
	for i in range(a):
		s += (2*(i+1)-a-1)*b[i]
		s %= mod
	return s
r = (sumof(n,x)*sumof(m,y))%mod
print(r)