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
#ABC58 D

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

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

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
ctx=0;cty=0
for i in range(n):
  ctx+=(2*i-n+1)*x[i]
  ctx%=mod
for i in range(m):
  cty+=(2*i-m+1)*y[i]
  cty%=mod
print((ctx*cty)%mod)

Submission status: Accepted

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

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

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

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


ans = (w * h) % MOD

print(ans)

Submission status: Accepted

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

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

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

    # print(xsum)
    # print(ysum)
    return ysum * xsum % mod


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

Submission status: Accepted

Solution code:
# 解説

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

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

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 = 0
for k, value in enumerate(x):
    x_sum += (2 * (k + 1) - n - 1) * value
    x_sum %= MOD

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

print ((x_sum * y_sum) % MOD)

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

# 前計算
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[-2::-1]

if M[0] % 2 == 0:
    M += M[::-1]
elif M[0] != 1:
    M += M[-2::-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)

Submission status: Accepted

Solution code:
class Mint:
    MOD = 1000000007  # Must be a prime
    CACHE_FACTORIALS = [1, 1]

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

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

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

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

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

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

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

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

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

    def __int__(self):
        return self.v

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

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

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

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

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

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

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

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

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


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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


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

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

    print(xtot*ytot % MOD)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    n = int(next(tokens))  # type: int
    m = int(next(tokens))  # type: int
    x = [int(next(tokens)) for _ in range(n)]  # type: "List[int]"
    y = [int(next(tokens)) for _ in range(m)]  # type: "List[int]"
    solve(n, m, x, y)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意


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

MOD = 10**9+7

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

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


#print(si,sj)

print(si*sj%MOD)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

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

# D
# 解説AC
# 数式をいじくりまわす必要

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

#Solve
if __name__ == '__main__':
    D()

Submission status: Accepted

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(n-1):X=(X+(x[i+1]-x[i])*(i+1)*(n-1-i))%(10**9+7)
for i in range(m-1):Y=(Y+(y[i+1]-y[i])*(i+1)*(m-1-i))%(10**9+7)
print(X*Y%(10**9+7))

Submission status: Accepted

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

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

sumX = 0
sumY = 0

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

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

ans = sumX * sumY % MOD
print(ans)

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 = 10 ** 9 + 7

sx = 0
pre = 0
for i in range(1, n) :
    pre += (x[i]-x[i-1]) * i
    sx += pre
    sx %= MOD
    
sy = 0
pre = 0
for i in range(1, m) :
    pre += (y[i]-y[i-1]) * i
    sy += pre
    sy %= MOD
    
print(sx*sy%MOD)

Submission status: Accepted

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

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

print((lx*ly)%mod)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
INF=10**9+7
n,m=map(int,input().split())
x_list=list(map(int,input().split()))
y_list=list(map(int,input().split()))
X=0
plus=0
for i in range(n-1):
    plus=(plus+(x_list[i+1]-x_list[i])*(i+1))%INF
    X=(X+plus)%INF
Y=0
plus=0
for i in range(m-1):
    plus=(plus+(y_list[i+1]-y_list[i])*(i+1))%INF
    Y=(Y+plus)%INF
ans=(X*Y)%INF
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

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

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

print((x_sum * y_sum) % mod)

Submission status: Accepted

Solution code:
mod=10**9+7
n,m=map(int,input().split())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
edge1=[]
for i in range(n-1):
  edge1.append(arr1[i+1]-arr1[i])
edge2=[]
for i in range(m-1):
  edge2.append(arr2[i+1]-arr2[i])
c1=[0]*(m-1)
tmp=0
if (m-1)%2==0:
  for i in range((m-1)//2):
    tmp+=(m-1)-2*i
    c1[i]=tmp
    c1[-(i+1)]=tmp
else:
  for i in range((m-1)//2+1):
    tmp+=(m-1)-2*i
    if i==(m-1)//2:
      c1[i]=tmp
    else:
      c1[i]=tmp
      c1[-(i+1)]=tmp
area=0
if (m-1)%2==0:
  for i in range((m-1)//2):
    area+=edge2[i]*c1[i]
    area+=edge2[-(i+1)]*c1[i]
else:
  for i in range((m-1)//2+1):
    if i==(m-1)//2:
      area+=edge2[i]*c1[i]
    else:
      area+=edge2[i]*c1[i]
      area+=edge2[-(i+1)]*c1[i]
c2=[0]*(n-1)
tmp=0
if (n-1)%2==0:
  for i in range((n-1)//2):
    tmp+=(n-1)-2*i
    c2[i]=tmp
    c2[-(i+1)]=tmp
else:
  for i in range((n-1)//2+1):
    tmp+=(n-1)-2*i
    if i==(n-1)//2:
      c2[i]=tmp
    else:
      c2[i]=tmp
      c2[-(i+1)]=tmp
ans=0
if (n-1)%2==0:
  for i in range((n-1)//2):
    ans+=area*edge1[i]*c2[i]
    ans+=area*edge1[-(i+1)]*c2[i]
    ans%=mod
else:
  for i in range((n-1)//2+1):
    if i==(n-1)//2:
      ans+=area*edge1[i]*c2[i]
    else:
      ans+=area*edge1[i]*c2[i]
      ans+=area*edge1[-(i+1)]*c2[i]
print(ans%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

x0 = 0
for i in range(n - 1):
    x0 += (x[i + 1] - x[i]) * (i + 1) * (n - i - 1)
    x0 %= mod
y0 = 0
for i in range(m - 1):
    y0 += (y[i + 1] - y[i]) * (i + 1) * (m - i - 1)
    y0 %= mod
print(x0 * y0 % mod)

Submission status: Accepted

Solution code:


def main():
    x_num, y_num = map(int, input().split())
    data_x = list(map(int, input().split()))
    data_y = list(map(int, input().split()))
    mod = 10 ** 9 + 7

    x_sum = 0
    for i in range(x_num):
        x_sum += (2 * i + 1 - x_num) * data_x[i]
        x_sum %= mod

    y_sum = 0
    for i in range(y_num):
        y_sum += (2 * i + 1 - y_num) * data_y[i]
        y_sum %= mod

    print((x_sum * y_sum) % mod)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m = list(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 - 1):
  t = x[i + 1] - x[i]
  X = (X + (i + 1) * (n - i - 1) * t) % MOD
for i in range(m - 1):
  t = y[i + 1] - y[i]
  Y = (Y + (i + 1) * (m - i - 1) * t) % MOD
print(X * Y % MOD)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)

MOD = 1000000007  # type: int

def solve(n: int, m: int, x: "List[int]", y: "List[int]"):
    def make_count(n):
        nc = [0] * n
        nc[0] = nc[n - 1] = n
        for i in range(1, (n - 1) // 2 + 1):
            nc[i] = nc[n - i - 1] = (nc[i - 1] + (n - 2 * i)) % MOD
        return nc

    nc = make_count(n - 1)
    mc = make_count(m - 1)
    xs = 0
    for i in range(n - 1):
        xs += (x[i + 1] - x[i]) * nc[i]
        xs %= MOD

    ret = 0
    for j in range(m - 1):
        ret += (y[j + 1] - y[j]) * xs * mc[j]
        ret %= MOD
    print(ret)
    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))  # type: int
    m = int(next(tokens))  # type: int
    x = [ int(next(tokens)) for _ in range(n) ]  # type: "List[int]"
    y = [ int(next(tokens)) for _ in range(m) ]  # type: "List[int]"
    solve(n, m, x, y)

if __name__ == '__main__':
    main()

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.sort()
y.sort()

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

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

print(xd*yd%mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
X = list(map(int, sys.stdin.readline().split()))
Y = list(map(int, sys.stdin.readline().split()))

xd = [b - a for a, b in zip(X[:-1], X[1:])]
yd = [b - a for a, b in zip(Y[:-1], Y[1:])]

#  あるエリアが何回カウントされるか
xc = itertools.accumulate(range(len(xd), -len(xd), -2))
yc = itertools.accumulate(range(len(yd), -len(yd), -2))

x = 0
for d, c in zip(xd, xc):
    x = (x + d * c) % MOD
y = 0
for d, c in zip(yd, yc):
    y = (y + d * c) % MOD

print(x * y % MOD)

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))
X = list(map(int, sys.stdin.readline().split()))
Y = list(map(int, sys.stdin.readline().split()))


def ModInt(mod):
    class _ModInt:
        def __init__(self, value):
            self.value = value % mod

        def __add__(self, other):
            if isinstance(other, _ModInt):
                return _ModInt(self.value + other.value)
            else:
                return _ModInt(self.value + other)

        def __radd__(self, other):
            return self.__add__(other)

        def __mul__(self, other):
            if isinstance(other, _ModInt):
                return _ModInt(self.value * other.value)
            else:
                return _ModInt(self.value * other)

        def __truediv__(self, other):
            # TODO: 実装
            raise NotImplementedError()

        def __repr__(self):
            return str(self.value)

    return _ModInt


MI = ModInt(MOD)
X = np.diff(X)
Y = np.diff(Y)
X = np.array([MI(x) for x in X])
Y = np.array([MI(y) for y in Y])

#  あるエリアが何回カウントされるか
xc = np.arange(len(X), -len(X), -2).cumsum()
yc = np.arange(len(Y), -len(Y), -2).cumsum()

print(np.dot(X, xc) * np.dot(Y, yc))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
X=list(map(int,input().split()))
Y=list(map(int,input().split()))
x=[]
MOD=10**9+7
for i in range(n-1):
    x.append(X[i+1]-X[i])
y=[]
for i in range(m-1):
    y.append(Y[i+1]-Y[i])
xlen=0
for i in range(n-1):
    xlen+=((i+1)*((n-i-1)*x[i])%MOD)%MOD
ylen=0
for j in range(m-1):
    ylen+=(((j+1)*((m-j-1)*y[j])%MOD)%MOD)
print((xlen*ylen)%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_tmp = 0
for i in range(n):
    x_tmp += (2*i-n+1) * X[i]
y_tmp = 0
for i in range(m):
    y_tmp += (2*i-m+1) * Y[i]

print(x_tmp * y_tmp % mod)

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
print(sum((2*i-n-1)*x[i-1]for i in range(1,n+1))*sum((2*i-m-1)*y[i-1]for i in range(1,m+1))%mod)

Submission status: Accepted

Solution code:
mod=10**9+7
n,m=map(int,input().split())
X=tuple(map(int,input().split()))
Y=tuple(map(int,input().split()))
ans_x,ans_y=0,0
for i,x in enumerate(X):
    ans_x+=(2*i+1-n)*x
    ans_x%=mod
for i,y in enumerate(Y):
    ans_y+=(2*i+1-m)*y
    ans_y%=mod
print((ans_x*ans_y)%mod)

Submission status: Accepted

Solution code:
n,m = [ int(it) for it in input().split() ]
xli = [ int(it) for it in input().split() ]
yli = [ int(it) for it in input().split() ]

dxli = [0]*( len(xli) -1 )
dyli = [0]*( len(yli) -1 )
for i in range( len(xli) - 1 ):
  dxli[i] = xli[i+1] - xli[i]
for i in range( len(yli) - 1 ):
  dyli[i] = yli[i+1] - yli[i]

MOD = 1000000007
Sx = 0
Sy = 0
for i in range( len(dxli) ):
  Sx += dxli[i] * ( i + 1 ) * ( len(dxli) - i )
  Sx %= MOD
for i in range( len(dyli) ):
  Sy += dyli[i] * ( i + 1 ) * ( len(dyli) - i )
  Sy %= MOD
print ( (Sx*Sy)%MOD )

Submission status: Accepted

Solution code:
Mod = 10**9+7
n = [0,0]
n[0],n[1] = map(int,input().split())
a = [0,0]
for j in range(2):
  x = list(map(int,input().split()))
  s = 0
  for i in range(n[j]):
    s = (s+x[i]*(2*i+1-n[j]))%Mod
  a[j] = s
print((a[0]*a[1])%Mod)

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()))
a = 0
for i in range(n):
  a = (a+x[i]*(2*i+1-n))%Mod
b = 0
for i in range(m):
  b = (b+y[i]*(2*i+1-m))%Mod
print((a*b)%Mod)

Submission status: Accepted

Solution code:
def main():
    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()]
    dx = []
    dy = []
    for i in range(n-1):
        dx.append(x[i+1]-x[i])
    for i in range(m-1):
        dy.append(y[i+1]-y[i])

    alpha = [(i+1)*(n-i-1) for i in range((n+1)//2)]
    beta = [(i+1)*(m-i-1) for i in range((m+1)//2)]

    sum_dx = sum([dx[i] * alpha[min(i, n-i-2)] for i in range(n-1)])
    sum_dy = sum([dy[i] * beta[min(i, m-i-2)] for i in range(m-1)])

    print((sum_dx * sum_dy)%(10**9+7))

main()

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

x,y=[],[]
for i,u in enumerate(X[:-1]):
    x.append(X[i+1]-u)
for i,v in enumerate(Y[:-1]):
    y.append(Y[i+1]-v)
wx = [(i+1)*(n-i-1)%mod for i in range(n-1)]
wy = [(i+1)*(m-i-1)%mod for i in range(m-1)]
print((sum([wx[i]*x[i] for i in range(n-1)])%mod)*(sum([wy[i]*y[i] for i in range(m-1)])%mod)%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 = 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))

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
xx = 0
yy = 0
for i, xi in enumerate(x):
    xx += (2 * i + 1 - n) * xi
for i, yi in enumerate(y):
    yy += (2 * i + 1 - m) * yi
print(xx * yy % MOD)

Submission status: Accepted

Solution code:
import numpy as np

MOD = 10**9+7

n,m = map(int,input().split())
X = np.array([int(x) for x in input().split()], dtype = np.int64)
Y = np.array([int(x) for x in input().split()], dtype = np.int64)

coef_X = np.arange(n)*2-(n-1)
X_sum = np.sum(np.mod(X * coef_X,MOD))%MOD

coef_Y = np.arange(m)*2-(m-1)
Y_sum = np.sum(np.mod(Y * coef_Y,MOD))%MOD

answer = (X_sum*Y_sum)%MOD
print(answer)

Submission status: Accepted

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

def F(j,i):
    return (i*(j-i))%INF
dx = []
px = 0
sx = 0
for x in map(int,input().split()):
    dx.append(x-px)
    px = x
dy = []
py = 0
sy = 0
for y in map(int,input().split()):
    dy.append(y-py)
    py = y

for i in range(1,n):
    sx = (sx+(dx[i]*F(n,i))%INF)%INF 
for i in range(1,m):
    sy = (sy+(dy[i]*F(m,i))%INF)%INF
print((sx*sy)%INF)

Submission status: Accepted

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

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

Y_sum = 0
for i, y in enumerate(Y, start=1):
    Y_sum += y * (M - i - i + 1)
    Y_sum %= 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()))
mod=10**9+7

X.sort()
Y.sort()

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

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

print(x*y%mod)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    X = list(map(int, input().split()))
    Y = list(map(int, input().split()))

    X_com = 0
    for i in range(1, N+1):
        X_com += (i - 1) * X[i-1] - (N - i) * X[i-1]

    Y_com = 0
    for i in range(1, M+1):
        Y_com += (i - 1) * Y[i-1] - (M - i) * Y[i-1]

    return (X_com * Y_com) % (10**9 + 7)


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

Submission status: Accepted

Solution code:
def mul_mod(mod: int, *args)->int:
    ret = 1
    for a in args:
        ret = (ret * a) % mod
    return ret


def rectangles(n: int, m: int, X: list, Y: list)->int:
    MOD = 10**9 + 7

    DX = []
    x0 = X[0]
    for x in X[1:]:
        DX.append((x-x0) % MOD)
        x0 = x

    DY = []
    y0 = Y[0]
    for y in Y[1:]:
        DY.append((y-y0) % MOD)
        y0 = y

    dx_sum = sum(mul_mod(MOD, i+1, n-1-i, dx) for i, dx in enumerate(DX))
    dy_sum = sum(mul_mod(MOD, i+1, m-1-i, dy) for i, dy in enumerate(DY))

    return (dx_sum * dy_sum) % MOD


if __name__ == "__main__":
    n, m = map(int, input().split())
    X = [int(s) for s in input().split()]
    Y = [int(s) for s in input().split()]
    ans = rectangles(n, m, X, Y)
    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

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

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

Submission status: Accepted

Solution code:
MOD = 10**9 + 7


def dup(x, y):
    return x + x*y + 1 - y**2


def main():
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    tmp = 0
    for i in range(n-1):
        tmp = (tmp + (x[i+1] - x[i]) * dup(n-2, i)) % MOD
    ans = 0
    for i in range(m-1):
        ans = (ans + (y[i+1] - y[i]) * dup(m-2, i) * tmp) % MOD
        ans = ans % MOD
    print(ans)


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=0
for i in range(N):
    x+=((i-(N-i-1))*X[i])%mod
    x%=mod
y=0
for j in range(M):
    y+=((j-(M-j-1))*Y[j])%mod
    y%=mod
print((x*y)%mod)

Submission status: Accepted

Solution code:
a,b = map(int, input().split())
x = list(map(int, input().split()))
y = list(map(int, input().split()))
s = 0 
k = 0
for i,t in enumerate(reversed(x)):
    s+= (a-i-1)*t
    s-= i*t
for i,t in enumerate(reversed(y)):
    k+= (b-i-1)*t
    k-= i*t
print((s*k)%(10**9+7))

Submission status: Accepted

Solution code:
mod=10**9+7
def f(a):
    c=0
    for i in range(len(a)):
        c+=a[i]-a[0]
    d=c
    for i in range(1,len(a)-1):
        d+=c-(len(a)-i)*(a[i]-a[i-1])
        c-=(len(a)-i)*(a[i]-a[i-1])
    return d%mod
n,m=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
print(f(a)*f(b)%mod)

Submission status: Accepted

Solution code:
N, M = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]

mod = 10**9 + 7

X = 0
Y = 0
for i in range(N - 1):
    X += (A[i + 1] - A[i]) * (i + 1) * (N - 1 - i)
    X %= mod
for j in range(M - 1):
    Y += (B[j + 1] - B[j]) * (j + 1) * (M - 1 - j)
    Y %= mod
print(X * Y % mod)