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:
# https://atcoder.jp/contests/abc058/tasks/arc071_b
# 定式化してみると、独立に計算できる部分と、
# 前処理で累積和を求めておけばループ内でO(1)に減る部分と、
# 愚直に計算する部分に減る
# 式変形の端の部分が若干間違っていたので注意

import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


class ModInt:
    def __init__(self, x, MOD=10 ** 9 + 7):
        '''
        pypyじゃないと地獄みたいな遅さなので注意
        '''
        self.mod = MOD
        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, self.mod)
        else:
            return ModInt(self.x + other, self.mod)

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

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

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

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

    __radd__ = __add__

    def __rsub__(self, other):  # 演算の順序が逆
        if isinstance(other, ModInt):
            return ModInt(other.x - self.x, self.mod)
        else:
            return ModInt(other - self.x, self.mod)

    __rmul__ = __mul__

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

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


def read_ints():
    return list(map(int, read().split()))


from itertools import accumulate

n, m = read_ints()
X = read_ints()
Y = read_ints()
MOD = 10 ** 9 + 7


def mul_term(X: list, n):
    S = list(accumulate(reversed(X)))
    S.reverse()
    ret = ModInt(0)
    for i in range(n - 1):
        ret += S[i + 1] - (n - i - 1) * X[i]
    return ret


# print(mul_term(X, n), mul_term(Y, m))
print(mul_term(X, n) * mul_term(Y, m))

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

# 小区間ごとに個数を数えて足し合わせると，
# summationの中身がxとyについて独立になっている

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

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

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

print(xsum*ysum%mod)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,m = map(int,input().split())
mod = 10**9+7
x = [int(i) for i in input().split()]
y = [int(i) for i in input().split()]
ans_x = 0
ans_y = 0
for i,xi in enumerate(x):
  ans_x += i*xi-(n-1-i)*xi
  ans_x %= mod
for j,yi in enumerate(y):
  ans_y += j*yi-(m-1-j)*yi
  ans_y %= mod
print(ans_x*ans_y%mod)

Submission status: Accepted

Solution code:
def d_rectangles(MOD=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()]

    ans1 = sum([x * (2 * k - N - 1) for k, x in enumerate(X, 1)])
    ans2 = sum([y * (2 * k - M - 1) for k, y in enumerate(Y, 1)])
    return (ans1 * ans2) % MOD

print(d_rectangles())

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

Submission status: Accepted

Solution code:
# 解説AC
# 素直に立式してから因数分解
# そこから愚直に計算せず線形時間でできるように考える
MOD = 10**9 + 7


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()]
    xl = 0
    yl = 0
    for i, x in enumerate(X):
        xl += i*x - (N-i-1)*x
    for i, y in enumerate(Y):
        yl += i*y - (M-i-1)*y
    ans = xl*yl
    ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    n, m = map(int, input().split())
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    x0 = x[0]
    y0 = y[0]
    x = list(map(lambda i:i-x0, x))
    y = list(map(lambda i:i-y0, y))
    sumx = sum(x)
    totalx = sumx
    for i in range(1,n):
        sumx -= (x[i]-x[i-1])*(n-i)
        totalx += sumx
        totalx %= ((10**9)+7)
    sumy = sum(y)
    totaly = sumy
    for i in range(1,m):
        sumy -= (y[i]-y[i-1])*(m-i)
        totaly += sumy
        totaly %= ((10**9)+7)
    print((totalx*totaly)%((10**9)+7))
resolve()

Submission status: Accepted

Solution code:
n,m = map(int,input().split())
x = list(map(int,input().split()))
y = list(map(int,input().split()))
res = 0
mod = 10**9+7
for i in range(n):
    res += (i*x[i])%mod-(n-i-1)*x[i]%mod
    res %= mod
ans = 0
for i in range(m):
    ans += ((i)*y[i])%mod-((m-i-1)*y[i])%mod
    ans %= mod
ans *=res
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

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

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

print (tmpX * tmpY % MOD)

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

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

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

Submission status: Accepted

Solution code:
# solution
import io

data=lambda:list(map(int,input().split()))
data()
q=lambda x:sum(j*(len(x)-1-2*i)for i,j in enumerate(x))
print(q(data())*q(data())%(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
ans = 0
temp1 = 0
temp2 = 0
for i in range(n):
    temp1 += i*x[i] -(n-(i+1))*x[i]
    temp1 %= MOD

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

ans = temp1*temp2
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
ans=0
def sum_S(a,z):
    ans=0
    for i in range(len(z)):
        z_i=z[i]*(2*i+1-a)
        ans+=z_i
    return ans
ans_x=sum_S(n,x)
ans_y=sum_S(m,y)
res=(ans_x*ans_y)%mod
print(res)

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

cnt = 0
for i in range(M - 1):
    l = Y[i + 1] - Y[i]
    var = (i + 1) * (M - (i + 1))
    cnt += l * var
    cnt %= MOD

ans = 0
for i in range(N - 1):
    l = X[i + 1] - X[i]
    var = (i + 1) * (N - (i + 1))
    ans += l * var * cnt
    ans %= MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = 10**9 + 7
def main():
    n, m = map(int, input().split())
    n, m = n-1, m-1
    x = list(map(int, input().split()))
    y = list(map(int, input().split()))
    a, b = 0, 0
    for i in range(n):
        tmp = (x[i+1] - x[i])
        tmp *= (1+i) * (n-i) % MOD
        a += tmp
        a %= MOD
    for i in range(m):
        tmp = (y[i+1] - y[i])
        tmp *= (1+i) * (m-i) % MOD
        b += tmp
        b %= MOD
    print(a*b % MOD)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

X_sum = sum((2 * i - N + 1) * x for i, x in enumerate(X)) % MOD
Y_sum = sum((2 * i - M + 1) * y for i, y in enumerate(Y)) % MOD
print(X_sum * Y_sum % MOD)

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

Submission status: Accepted

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

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

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

print(tmp1*tmp2%mod)

Submission status: Accepted

Solution code:
# 解説AC
N,M = map(int, input().split())
X = [int(i) for i in input().split()]
Y = [int(i) for i in input().split()]

MOD = 10 ** 9 + 7

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

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

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


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


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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

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

main()

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

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

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

ans = xsm * ysm % mod
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

def main():
    mod=10**9+7
    N,M=MI()
    x=LI()
    y=LI()
    dx=[0]*(N-1)
    dy=[0]*(M-1)
    for i in range(N-1):
        dx[i]=x[i+1]-x[i]
    for i in range(M-1):
        dy[i]=y[i+1]-y[i]
    
    NN=max(N,M)+3
    #S[a]は1~aまでの和
    S=[0]*(NN+1)
    for i in range(1,NN+1):
        S[i]+=S[i-1]+i
        
    #端からa番目の要素を何回使う?
    def calc(a,nm):
        if nm%2==1 and a==(nm+1)//2:#aが中央の場合
            return S[a]+S[a-1]
        else:
            return 2*S[a]+(nm-2*a)*a
    

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

main()

Submission status: Accepted

Solution code:
MOD = 1000000007

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

dx = [X[i + 1] - X[i] for i in range(N - 1)]
dy = [Y[i + 1] - Y[i] for i in range(M - 1)]

sx = 0
sy = 0

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

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

print(sx * sy % MOD)

Submission status: Accepted

Solution code:
# 解説AC
n,m=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
mod=10**9+7
ans,ans2=0,0 #sum[0<=i<j<n](x[j]-x[i])を求める.
for i in range(n):
    ans=(ans+(-n+2*i+1)*x[i])%mod
for i in range(m):
    ans2=(ans2+(-m+2*i+1)*y[i])%mod
print(ans*ans2%mod)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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


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


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


INV = {}


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


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


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

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

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

Submission status: Accepted

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


    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7

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


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

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

Submission status: Accepted

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

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
#preprocess
#y_culculate
y_sum = 0
ans = 0
x_sum = 0
for i in range(1,m):
    h = y[i] - y[i-1]
    y_sum += h * i * (m - i)
    y_sum %= mod
for i in range(1,n):
    w = x[i] - x[i-1]
    x_sum += w * i * (n - i)
    x_sum %= mod
print((x_sum * y_sum) % mod)

Submission status: Accepted

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

import sys
sys.stdin.readline

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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)
    byj=mint(0)
    for j in range(m - 1):
        byj+=(j+1)*(m-j-1)*(yy[j+1]-yy[j])
    for i in range(n-1):
        ans+=(xx[i+1]-xx[i])*(i+1)*(n-i-1)*byj

    print(ans)

main()

Submission status: Accepted

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

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

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

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

print((tmp_x*tmp_y)%mod)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
mod=10**9+7
n,m=map(int,input().split())
x=list(map(int,input().split()))
y=list(map(int,input().split()))
xsum=0
for i in range(n):
    xsum+=x[-i-1]*(n-2*i-1)%mod
ysum=0
for i in range(m):
    ysum+=y[-i-1]*(m-2*i-1)%mod

print(xsum*ysum%mod)

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

Submission status: Accepted

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

import sys
from operator import add

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

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

    def __init__(self, n, func, intv, A=[]):
        """
        :param n: 要素数(0-indexed)
        :param func: 値の操作に使う関数(min, max, add, gcdなど)
        :param intv: 要素の初期値(単位元)
        :param A: 初期化に使うリスト(オプション)
        """
        self.n = n
        self.func = func
        self.intv = intv
        # nより大きい2の冪数
        n2 = 1
        while n2 < n:
            n2 <<= 1
        self.n2 = n2
        self.tree = [self.intv] * (n2 << 1)
        # 初期化の値が決まっている場合
        if A:
            # 1段目(最下段)の初期化
            for i in range(n):
                self.tree[n2+i] = A[i]
            # 2段目以降の初期化
            for i in range(n2-1, -1, -1):
                self.tree[i] = self.func(self.tree[i*2], self.tree[i*2+1])

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

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

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

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

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

# 最初の座標を基準として0にすると累積和っぽくなる
A = [a-A[0] for a in A]
B = [b-B[0] for b in B]

# 縦横独立に、全区間総和を取る
st1 = SegTree(N, add, 0, A)
st2 = SegTree(M, add, 0, B)
h = w = 0
# 左端を固定して右端は区間取得する
for i in range(N-1):
    h += st1.query(i+1, N) - st1.get(i) * (N-i-1)
    h %= MOD
for i in range(M-1):
    w += st2.query(i+1, M) - st2.get(i) * (M-i-1)
    w %= MOD
ans = h * w % MOD
print(ans)