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: Wrong Answer

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

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

X_diff = []
Y_diff = []

for i in range(len(X)-1):
    X_diff.append((X[i+1]-X[i]))
                  
for i in range(len(Y)-1):
    Y_diff.append((Y[i+1]-Y[i]))
    
    
def diff(l, n):
    a = 0
    for i in range(len(l) - n + 1):
        a += sum(l[i:i+n])
    return a

    
ans = 0
for i in range(n):
    for j in range(m):
        ans += diff(X_diff, i) * diff(Y_diff, j)
        
print(ans)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "));
xs = list(map(int, input().split(" ")));
ys = list(map(int, input().split(" ")));
modula=1000000007

sumx=0;
sumy=0;
answer=0;
for i in range(0, n-1):
    for j in range(i+1, n):
        for k in range(0, m-1):
            for L in range(k+1, m):
                answer = (((xs[j]-xs[i])%modula) * ((ys[L]-ys[k])%modula)) % modula

print(answer);

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "));
xs = list(map(int, input().split(" ")));
ys = list(map(int, input().split(" ")));
modula=1000000007

sumx=0;
for i in range(0, n-1):
    for j in range(i+1, n):
        sumx += (xs[j]-xs[i]) % modula

sumy=0;
for i in range(0, m-1):
    for j in range(i+1, m):
        sumy += (ys[j]-ys[i]) % modula

big, small = sumx, sumy;
if big < small:
    big, small = small, big;

delta = modula // big;

answer = 0;
while True:
    answer += (delta*big) % modula;
    small -= delta;
    if small<delta:
        break;

answer += small*big % modula;
print(answer);

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "));
xs = list(map(int, input().split(" ")));
ys = list(map(int, input().split(" ")));
print(xs, ys);
modula=1000000007

sumx=0;
for i in range(0, n-1):
    for j in range(i+1, n):
        sumx += (xs[j]-xs[i])

sumy=0;
for i in range(0, m-1):
    for j in range(i+1, m):
        sumy += (ys[j]-ys[i])

sumx = sumx % modula;
sumy = sumy % modula;
print((sumx*sumy) % modula)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "));
xs = list(map(int, input().split(" ")));
ys = list(map(int, input().split(" ")));
print(xs, ys);

sumx=0;
for i in range(0, n-1):
    for j in range(i+1, n):
        sumx += (xs[j]-xs[i])

sumy=0;
for i in range(0, m-1):
    for j in range(i+1, m):
        sumy += (ys[j]-ys[i])

print((sumx*sumy) % 1000000007)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "));
xs = list(map(int, input().split(" ")));
ys = list(map(int, input().split(" ")));
print(xs, ys);

sumx=0;
for i in range(0, n-1):
    for j in range(i+1, n):
        sumx += (xs[j]-xs[i]) % 1000000007

sumy=0;
for i in range(0, m-1):
    for j in range(i+1, m):
        sumy += (ys[j]-ys[i]) % 1000000007

print((sumx*sumy) % 1000000007)

Submission status: Wrong Answer

Solution code:
n, m = map(int, input().split(" "));
xs = list(map(int, input().split(" ")));
ys = list(map(int, input().split(" ")));
print(xs, ys);

sumx=0;
for i in range(0, n-1):
    for j in range(i+1, n):
        sumx += (xs[j]-xs[i]) % 1000000007

sumy=0;
for i in range(0, m-1):
    for j in range(i+1, m):
        sumx += (ys[j]-ys[i]) % 1000000007

print((sumx*sumy) % 1000000007)

Submission status: Wrong Answer

Solution code:

import numpy as np

n, m = map(int, input().split())
c = 1e9 + 7

x_n = np.array([int(e) for e in input().split()])
y_n = np.array([int(e) for e in input().split()])


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

x = x_sum % c

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

y = y_sum % c

ret = (x*y) % c

print(int(ret))

Submission status: Wrong Answer

Solution code:

import numpy as np

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

x_n = np.array([int(e) for e in input().split()])
y_n = np.array([int(e) for e in input().split()])


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

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

print(x_sum * y_sum % (1000000007))

Submission status: Wrong Answer

Solution code:
n, m = [int(x) for x in input().split(' ')]
hor = [int(x) for x in input().split(' ')]
ver = [int(x) for x in input().split(' ')]
area = 0
for i in range(n-1):
    stor = 0
    for j in range(m-1):
        stor += (hor[i+1] - hor[i]) * (ver[j+1] - ver[j]) * (j+1) * (n-j-1)
    area += stor * (i + 1) * (n-i-1)
    area = area % (10**9 + 7)
print(area)

Submission status: Wrong Answer

Solution code:
n, m = [int(x) for x in input().split(' ')]
hor = [int(x) for x in input().split(' ')]
ver = [int(x) for x in input().split(' ')]
area = 0
for i in range(n-1):
    for j in range(m-1):
        area += (i+1) * (((hor[i+1] - hor[i]) % (10 ** 9 + 7)) * ((ver[j+1] - ver[j]) % (10 ** 9 + 7))) % (10 ** 9 + 7) * (j+1) * (n-i-1) * (n-j-1)
        area = area % (10 ** 9 + 7)
print(area)

Submission status: Wrong Answer

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)-1):
  for j in range (i+1,len(x)):
    yoko+=x[j]-x[i]
for i in range (0,len(y)-1):
  for j in range (i+1,len(y)):
    tate+=y[j]-y[i]

print(yoko*tate)

Submission status: Wrong Answer

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 i in range(c[1]):
  tmp = i*f[i] - (c[1]-i-1)*f[i]
  ansy += tmp

print(ansx * ansy)

Submission status: Wrong Answer

Solution code:
a = list(map(int,"3 3".split(" ")))
b = list(map(int,"1 3 4".split(" ")))
c = list(map(int,"1 3 6".split(" ")))

x = 0
y = 0

for i in range(1,a[0]):
    x = x + b[i] - b[0]
    
xx = x
for i in range(1,a[0]-1):
    xx = xx - ((b[i] - b[i - 1]) * (a[0] - i))
    x = x + xx

for i in range(1,a[1]):
    y = y + c[i] - c[0]
    
yy = y
for i in range(1,a[1]-1):
    yy = yy - ((c[i] - c[i - 1]) * (a[1] - i))
    y = y + yy

ans = x * y
ans = ans % (10**9 + 7)

print(ans)

Submission status: Wrong Answer

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(1,a):
		s += i*b[i]
	for i in range(a):
		s -= (a-i)*b[i]
	return s%mod
r = (sumof(n,x)*sumof(m,y))%mod
print(r)

Submission status: Wrong Answer

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(1,a):
		s += i*b[i]
	for i in range(a):
		s -= (n-i)*b[i]
	return s%mod
r = (sumof(n,x)*sumof(m,y))%mod
print(r)

Submission status: Wrong Answer

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
for i in range(n):
	for j in range(i+1,n):
		dx = x[j]-x[i]
		for k in range(m):
			for l in range(k+1,m):
				dy = y[l]-y[k]
				r += dx*dy
print(r)

Submission status: Wrong Answer

Solution code:
from itertools import combinations

n, m = [int(i) for i in input().split(' ')]
x_list = [int(i) for i in input().split(' ')]
y_list = [int(i) for i in input().split(' ')]

total = 0
for x1, x2 in combinations(x_list, 2):
    for y1, y2 in combinations(y_list,2):
        total += (x2-x1)*(y2-y1)
        
print(int(total % (10**9+2)))

Submission status: Wrong Answer

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

ans_x=0
ans_y=0

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


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

print(ans_x * ans_y % mod_number)

Submission status: Wrong Answer

Solution code:
num = input().split(" ")
vertical = list(map(int, input().split()))
horizontal = list(map(int, input().split()))

xlen = list()
ylen = list()
temp = 0

a = int(num[0])
b = int(num[1])

for i in range(a):
	for j in range(a - i):
		xlen.append(abs(vertical[i] - vertical[j]))

for i in range(b):
	for j in range(b - i):
		ylen.append(abs(horizontal[i] - horizontal[j]))

for i in range(len(xlen)):
	for j in range(len(ylen)):
		temp += xlen[i] * ylen[j]

ans = temp % (10**9 + 7)

print(ans)

Submission status: Wrong Answer

Solution code:
num = input().split()
vertical = input().split()
horizontal = input().split()

xmin = 10**9
xmax = -10**9
ymin = 10**9
ymax = -10**9

for i in range(int(num[0])):
	xmin = min(int(vertical[i]), xmin)
	xmax = max(int(vertical[i]), xmax)
for i in range(int(num[1])):
	ymin = min(int(horizontal[i]), ymin)
	ymax = max(int(horizontal[i]), ymax)

ans = ((xmax - xmin) * (ymax - ymin) * (int(num[0])-1) * (int(num[1])-1)) % (10**9 + 7)

print(ans)

Submission status: Wrong Answer

Solution code:
num = input().split()
vertical = input().split()
horizontal = input().split()

xmin = 10**9
xmax = -10**9
ymin = 10**9
ymax = -10**9

for i in range(int(num[0])):
	xmin = min(int(vertical[i]), xmin)
	xmax = max(int(vertical[i]), xmax)
for i in range(int(num[1])):
	ymin = min(int(horizontal[i]), ymin)
	ymax = max(int(horizontal[i]), ymax)

ans = (xmax - xmin) * (ymax - ymin) * (int(num[0])-1) * (int(num[1])-1)

print(ans)

Submission status: Wrong Answer

Solution code:
def rren(): return list(map(int, input().split()))
n, m = rren()
x = rren()
y = rren()
ans = 0
x_sum = 0
y_sum = 0
for now_x in range(n):
    x_sum += x[now_x] * (now_x - (n-1 - now_x))
for now_y in range(m):
    y_sum += y[now_y] * (now_y - (m-1 - now_y))

print(x_sum * y_sum)

Submission status: Wrong Answer

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

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

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

ans = sum_x * sum_y

divide_num = pow(10, 9) + 7
print(ans % divide_num)

Submission status: Wrong Answer

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


def list_n():
    return [int(e) for e in input().split()]


def list_s():
    return [s for e in input().split()]



def main(xs, ys):
    answer = 0
    max_x  = max(xs)
    max_y  = max(ys)
    min_x  = min(xs)
    min_y  = min(ys)
    
    def one_fold(xs, x=False):
        if len(xs) == 2:
            if x:
                return [max_x - min_x]
            else:
                return [max_y - min_y]
        else:
            return [xs[i] - xs[i - 1] for i in range(1, len(xs))]
    
    xxs = one_fold(xs)
    yys = one_fold(ys)

    while True:
        if len(xxs) == 1 and len(yys) == 1:
            break

        for x in xxs:
            for y in yys:
                answer += x * y

        if len(xxs) > len(yys):
            xxs = one_fold(xxs, x=True)
        else:
            yys = one_fold(yys)
    answer += max_x * max_y
    return answer % (pow(10, 9) + 7)

# print(
#     main(
#         [1, 3, 4],
#         [1, 3, 6]
#     )
# )

if __name__ == '__main__':
    n, m = list_n()
    xs   = list_n()
    ys   = list_n()
    print(main(xs, ys))

Submission status: Wrong Answer

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

result = (max(x) - min(x)) * (max(y) - min(y))
result *= ((n - 2) * (m - 2))

print(result % (10**9 + 7))

Submission status: Wrong Answer

Solution code:
a = list(map(int,input().split()))
x = list(map(int,input().split()))
y = list(map(int,input().split()))
xx = x[-1]-x[0]
x1 = (a[0]-1) * xx
yy = y[-1]-y[0]
y1 = (a[1]-1) * yy
x1 = x1%((10**9)+7)
y1 = y1%((10**9)+7)
print((x1*y1)%((10**9)+7))

Submission status: Wrong Answer

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


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

x = list(map(int,input().split()))
y = list(map(int,input().split()))
xx = (x[-1] - x[0]) % (pow(10,9)+7)
yy = (y[-1] - y[0]) % (pow(10,9)+7)
ans = ( xx * yy * (n+m-2)) % (pow(10,9)+7) 

print(ans)

Submission status: Wrong Answer

Solution code:
mod = 1000000007
n,m = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
N = 2**(n-2)
M = 2**(m-2)
print(((x[n-1]-x[0])*(y[m-1]-y[0])*N * M) % mod)

Submission status: Wrong Answer

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


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

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

ans = (((x[-1] - x[0]) * (y[-1]-y[0]) * (n+m-2)) )% (pow(10,9)+7)

print(ans)

Submission status: Wrong Answer

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

#s = [(x[i+1]-x[i])*(y[j+1]-y[j]) for i in range(n-1) for j in range(m-1)]

#for i in itertools.combinations(s)

area = 0
r = 10**9+7
print(r)
for xi in itertools.combinations(x,2):
    for yi in itertools.combinations(y,2):
        area = (area + (xi[1]-xi[0])*(yi[1]-yi[0]))%r

print(area)

Submission status: Wrong Answer

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

t = input().split(" ")
x = []
for i in range(n):
    x.append(int(t[i]))
    
t = input().split(" ")
y = []
for i in range(m):
    y.append(int(t[i]))

print(x)
print(y)

r = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        for k in range(m - 1):
            for l in range(k + 1, m):
                r += (x[j] - x[i]) * (y[l] - y[k])
print(r % (10 ** 9 + 7))

Submission status: Wrong Answer

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


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

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

ans = (x[-1] - x[0]) * (y[-1]-y[0]) * (n+m-2)

print(ans)

Submission status: Wrong Answer

Solution code:
nm = input().split()
n = int(nm[0])
m = int(nm[1])
X = input().split()
Y = input().split()
X = map(int, X)
X = list(X)
Y = map(int, Y)
Y = list(Y)
S = 0
for i in range(n - 1):
	for j in range(m - 1):
		s = (X[i + 1] - X[i]) * (Y[j + 1] - Y[j])
		s = (i + 1) * (n - i - 1) * (j + 1) * (m - j - 1) * s
		S += s
print(S)
ans = S % 1000000007
print(ans)

Submission status: Wrong Answer

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

import os
import sys
import math
import numpy as np

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


print(X)
print(Y)
# 1 つの最小長方形は (n-1)*(m-1) 回加算される

S = 0
for x in range(n-1):
	for y in range(m-1):
		var = (X[x+1]-X[x]) * (Y[y+1]-Y[y])
		# print(var)
		x_times = (x+1) * (n-x-1)
		y_times = (y+1) * (m-y-1)

		S += (var * x_times * y_times)

mod = 10 ** 9 + 7
print(S%mod)

Submission status: Wrong Answer

Solution code:
a = list(map(int,input().split()))
x = list(map(int,input().split()))
y = list(map(int,input().split()))
print((x[-1]-x[0])*(y[-1]-y[0])*(a[0]-1)*(a[1]-1))

Submission status: Time Limit Exceeded

Solution code:
import itertools


def solve():
    N, M = map(int, input().split())

    Xi = list(map(int, input().split()))
    Yi = list(map(int, input().split()))

    comb = list(itertools.combinations(Xi, 2))

    xs = list(map(lambda x: abs(x[0] - x[1]), comb))

    comb = list(itertools.combinations(Yi, 2))

    ys = list(map(lambda x: abs(x[0] - x[1]), comb))

    ans = 0
    for x in xs:
        for y in ys:
            ans += x * y

    return ans % (10**9 + 7)


if __name__ == '__main__':
    res = solve()
    print(res)

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
x = [x for x in map(int, input().split())]
y = [y for y in map(int, input().split())]
ans = 0
for i in range(n-1):
    for j in range(m-1):
        v = (i+1)*(n-1-i) * (j+1)*(m-1-j) #面積係数
        x_width = x[i+1] - x[i]
        y_width = y[j+1] - y[j]
        ans += v * x_width * y_width #面積係数*最小長方形面積
print(ans%1000000007)

Submission status: Time Limit Exceeded

Solution code:
def ABC058D(n, m, x, y):
    s = 0
    for i1 in range(n-1):
        for i2 in range(i1+1, n):
            w = x[i2] - x[i1]
            for j1 in range(m-1):
                for j2 in range(j1+1, m):
                    h = y[j2] - y[j1]
                    s += w * h
    print(s % 1000000007)

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

Submission status: Time Limit Exceeded

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

X=0
Y=0
for i in range(0,n):
    for j in range(i+1,n):
        X+=x[j]-x[i]
        
for i in range(0,m):
    for j in range(i+1,m):
        Y+=y[j]-y[i]
        
mod=10**9+7
print(X*Y%mod)

Submission status: Time Limit Exceeded

Solution code:
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 = sorted(list(map(int,input().split())))
Y = sorted(list(map(int,input().split())))
ans = 0
for i in range(n - 1):
    for j in range(m - 1):
        S = (X[i + 1] - X[i]) * (Y[j + 1] - Y[j])
        num = (i + 1) * (n - (i + 1)) * (j + 1) * (m - (j + 1))
        S *= num
        S %= 10 ** 9 + 7
        ans += S
        ans %= 10 ** 9 + 7
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n, m = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

total = 0
for S in itertools.combinations(A, 2):
    for T in itertools.combinations(B, 2):
        total += (S[1] - S[0]) * (T[1] - T[0])
        total = total % 1000000007
print(total)

Submission status: Time Limit Exceeded

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
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()) # n=1
def inpm(): return map(int,input().split()) # x=1,y=2
def inpl(): return list(map(int, input().split())) # a=[1,2,3,4,5,...,n]
def inpls(): return list(input().split())  # a=['1','2','3',...,'n']
def inplm(n): return list(int(input()) for _ in range(n)) # x=[] 複数行
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)] # [[2,2,2,2],[1,1,1,1],[3,3,3,3]] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) # [[1,1,1,1],[2,2,2,2],[3,3,3,3]] 

def main():
    n,m = inpm()
    x = inpl()
    y = inpl()
    N = [0 for _ in range(n-1)]
    M = [0 for _ in range(m-1)]
    cnt = n-1
    plus = 0
    for i in range(n-1):
        plus += cnt
        N[i] = plus
        N[-i-1] = plus
        cnt -= 2
        if cnt <= 0:
            break
    cnt = m-1
    plus = 0
    for i in range(m-1):
        plus += cnt
        M[i] = plus
        M[-i-1] = plus
        cnt -= 2
        if cnt <= 0:
            break
    ans = 0
    for i in range(n-1):
        for j in range(m-1):
            area = ((x[i+1]-x[i])*(y[j+1]-y[j])) % mod
            area = (area*N[i]) % mod
            area = (area*M[j]) % mod
            ans = (ans+area) % mod
    print(ans)   
    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
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)
    for i in range(n-1):
        for j in range(m-1):
            s=(xx[i+1]-xx[i])*(yy[j+1]-yy[j])
            ans+=s*(i+1)*(n-i-1)*(j+1)*(m-j-1)
    print(ans)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
#mint
class ModInt:
    def __init__(self, x):
        self.x = x % mod
    
    def __str__(self):
        return str(self.x)
    
    __repr__ = __str__

    def __add__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x + other.x)
        else:
            return ModInt(self.x + other)

    __radd__ = __add__
        
    def __sub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x - other.x)
        else:
            return ModInt(self.x - other)

    def __rsub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(other.x - self.x)
        else:
            return ModInt(other - self.x)

    def __mul__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * other.x)
        else:
            return ModInt(self.x * other)

    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, mod-2,mod))
        else:
            return ModInt(self.x * pow(other, mod - 2, mod))
            
    def __rtruediv(self, other):
        if isinstance(other, self):
            return ModInt(other * pow(self.x, mod - 2, mod))
        else:
            return ModInt(other.x * pow(self.x, mod - 2, mod))
            

    def __pow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(self.x, other.x, mod))
        else:
            return ModInt(pow(self.x, other, mod))
            

    def __rpow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(other.x, self.x, mod))
        else:
            return ModInt(pow(other, self.x, mod))

def main():
    N,M=MI()
    X=LI()
    Y=LI()
    ans = ModInt(0)
    for i,x in enumerate(X[:-1]):
        for j,y in enumerate(Y[:-1]):
            s = (X[i+1]-x) * (Y[j+1]-y)
            ans += (i+1) * (N-(i+1)) * (j+1) * (M-(j+1)) * s
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
mod = int(1e9+7)
n, m = [int(num) for num in input().split()]

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

sumx = 0
for i in range(n-1):
    sumx += sum(x[i+1:]) - x[i] * ((n-1) - (i+1) + 1)
sumy = 0
for k in range(m-1):
    sumy += sum(y[k+1:]) - y[k] * ((m-1) - (k+1) + 1)

ans = ((sumx % mod) * (sumy % mod)) % mod

print(ans)

Submission status: Time Limit Exceeded

Solution code:
mod = int(1e9+7)
n, m = [int(num) for num in input().split()]

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

sumx = 0
for i in range(n-1):
    sumx += (sum(x[i+1:]) - x[i] * ((n-1) - (i+1) + 1)) % mod
sumy = 0
for k in range(m-1):
    sumy += (sum(y[k+1:]) - y[k] * ((m-1) - (k+1) + 1)) % mod

ans = ((sumx % mod) * (sumy % mod)) % mod

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
mod = int(1e9+7)
n, m = [int(num) for num in input().split()]

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

sumx = 0
for cmb in combinations(x, 2):
    sumx += cmb[1] - cmb[0]

sumy = 0
for cmb in combinations(y, 2):
    sumy += cmb[1] - cmb[0]

ans = (sumx % mod) * (sumy % mod) % mod

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
mod = int(1e9+7)
n, m = [int(num) for num in input().split()]

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

sumx = 0
for cmb in combinations(x, 2):
    sumx += (cmb[1] - cmb[0]) % mod

sumy = 0
for cmb in combinations(y, 2):
    sumy += (cmb[1] - cmb[0]) % mod

ans = sumx * sumy % mod

print(ans)

Submission status: Time Limit Exceeded

Solution code:
p=10**9+7

n,m=map(int,input().split())
x=list(map(int,input().split()))
w=[x[i+1]-x[i] for i in range(n-1)]
y=list(map(int,input().split()))
h=[y[k+1]-y[k] for k in range(m-1)]

ans=0

for i in range (n-1):
    for k in range(m-1):
        ans=(ans+w[i]*h[k]*(1+i)*(n-i-1)*(1+k)*(m-k-1))%p

print(ans)

Submission status: Time Limit Exceeded

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

total = 0
for l in range(len(y)-1):
	for k in range(len(x)-1):
		for i in range(k+1,len(x)):
			for j in range(l+1,len(y)):
				total += (abs(x[i]-x[k])*abs(y[j]-y[l]))%1000000007
				total %= 1000000007

print(total%1000000007)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# 前計算
for i in range(n - 1):
    x[i] = x[i + 1] - x[i]

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

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

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

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

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

# print (x)
# print (y)
# print (N)
# print (M)

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

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


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

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

print ((X_sum * Y_sum) % MOD)