Problem:


Score : 500 points

Problem Statement

We have a grid of squares with N rows and M columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.

If we place the K pieces on squares (x_1, y_1), (x_2, y_2), ..., and (x_K, y_K), the cost of this arrangement is computed as:

\sum_{i=1}^{K-1} \sum_{j=i+1}^K (|x_i - x_j| + |y_i - y_j|)

Find the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 10^9+7.

We consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.

Constraints

2 \leq N \times M \leq 2 \times 10^5

2 \leq K \leq N \times M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the sum of the costs of all possible arrangements of the pieces, modulo 10^9+7.

Sample Input 1

2 2 2

Sample Output 1

8

There are six possible arrangements of the pieces, as follows:

((1,1),(1,2)), with the cost |1-1|+|1-2| = 1

((1,1),(2,1)), with the cost |1-2|+|1-1| = 1

((1,1),(2,2)), with the cost |1-2|+|1-2| = 2

((1,2),(2,1)), with the cost |1-2|+|2-1| = 2

((1,2),(2,2)), with the cost |1-2|+|2-2| = 1

((2,1),(2,2)), with the cost |2-2|+|1-2| = 1

The sum of these costs is 8.

Sample Input 2

4 5 4

Sample Output 2

87210

Sample Input 3

100 100 5000

Sample Output 3

817260251

Be sure to print the sum modulo 10^9+7.



Sample input:
2 2 2


Sample output:
8

Submission status: Runtime Error

Solution code:
def power_mod(a,b,mod=10**9+7):
    i,temp,box=0,b,[]
    while(2**i<=b):
        i+=1
    for j in range(i-1,-1,-1):
        box=[[j,temp//2**j]]+box
        temp-=2**j*(temp//2**j)
    box[0].append(a)
    ans=box[0][1]*a%mod
    for j in range(1,i):
        box[j].append(box[j-1][2]**2%mod)
        if box[j][1]==1:
            ans=(ans*box[j][2])%mod
    return ans
def n_func(n,mod=10**9+7):
    ans=1
    for i in range(1,n+1):
        ans=(ans*i)%mod
    return ans
def nPr(n,r,mod=10**9+7):
    ans=n_func(n-r,mod)
    ans=power_mod(ans,mod-2,mod)
    return ans*n_func(n,mod)%mod
def nCr(n,r,mod=10**9+7):
    ans=n_func(n-r,mod)*n_func(r,mod)%mod
    ans=power_mod(ans,mod-2,mod)
    return ans*n_func(n,mod)%mod

N,M,K=map(int,input().split())
keisuu=nCr(N*M-2,K-2,mod)

sum_=0
for i in range(N):
    a=min(abs(i),abs(N-i-1))
    b=max(abs(i),abs(N-i-1))
    sum_+=(M**2)*((a*(a+1)//2)+(b*(b+1)//2))
for i in range(M):
    a=min(abs(i),abs(M-i-1))
    b=max(abs(i),abs(M-i-1))
    sum_+=(N**2)*((a*(a+1)//2)+(b*(b+1)//2))
print((keisuu * (sum_//2))%mod)

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

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

def mCn(m,n):
    return math.factorial(m)//(math.factorial(n)*math.factorial(m-n)) % mod

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

N1 = 10**5
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

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


ans = 0
for i in range(1,M):
    ans += (M-i)*N**2*i*(cmb(M*N-2,K-2,mod))
    if ans > mod:
        ans = ans % mod

for i in range(1,N):
    ans += (N-i)*M**2*i*(cmb(M*N-2,K-2,mod))
    if ans > mod:
        ans = ans % mod

print(ans)

Submission status: Runtime Error

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

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

def mCn(m,n):
    return math.factorial(m)//(math.factorial(n)*math.factorial(m-n)) % mod

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

N1 = 10**4
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

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


ans = 0
for i in range(1,M):
    ans += (M-i)*N**2*i*(cmb(M*N-2,K-2,mod))
    if ans > mod:
        ans = ans % mod

for i in range(1,N):
    ans += (N-i)*M**2*i*(cmb(M*N-2,K-2,mod))
    if ans > mod:
        ans = ans % mod

print(ans)

Submission status: Runtime Error

Solution code:

"""
xi,xjのペアにおいて、このペアは何通り出てくるか
sum(全通り)sum(i)sum(j){|xi-xj|+|yi-yj|}
和なので、どんな順序でもいい

sum(i)sum(j)sum(全通り){|xi-xj|}
sum(i)sum(j)sum(全通り){|yi-yj|}

あるi,jで固定して、sum(全通り){|xi-xj|}を求めるヤツ
固定したi,jが現れる回数は、

pat = NM-2_C_K-2 こいつはどこでも一緒

pat*sum(i)sum(j){|xi-xj|}を計算する
すべてのi,j(i>j)ペアにおいて、xi,xjの和は
sum(i=1,N){i*(N-i)}
"""
from math import factorial

N,M,K = map(int,input().split())
mod = 10**9+7
fa = 1
num = N*M - 2
# 固定したときの通り数
cut = min(K-2, N*M-K)]
# このfor文は少ないほうが良い
for i in range(num,1,-1):
    if num-i == cut:
        break
    fa *= i
pat = fa//factorial(cut)

# print(pat)
# xi-xj
n_x = M**2*((N-1)*N*(N+1))//6
n_y = N**2*((M-1)*M*(M+1))//6
print(pat*(n_x+n_y)%mod)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h> 

using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF INT_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()

ll modpow(ll x,ll n){
  if(n==0)return 1;
  if(n%2)return (modpow(x,n-1)*x)%MOD;
  else {
    ll half=n/2;
    ll halfpow=modpow(x,n/2)%MOD;
    return (halfpow*halfpow)%MOD;
  }
}

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  
  int N,M,K;
  cin>>N>>M>>K;
  vector<ll>fac(N*M+1);
  fac[0]=1;
  for(int i=1;i<=N*M;i++)fac[i]=(fac[i-1]*i)%MOD;

  ll inv=modpow((fac[N*M-2-(K-2)]*fac[K-2])%MOD,MOD-2)%MOD;
  ll comb=(fac[N*M-2]*inv)%MOD;
  ll x=0;
  for(int i=1;i<N;i++)x+=((N-i)*i*comb)%MOD;
  ll y=0;
  for(int i=1;i<M;i++)y+=((M-i)*i*comb)%MOD;
  x*=M*M%MOD;
  y*=N*N%MOD;
  cout<<(x+y)%MOD<<endl;
  return 0;
}

Submission status: Runtime Error

Solution code:
mod=10**9+7
def mod_combination(n, k, mod):
    def ext_gcd(a, b):
        if b == 0:return a, 1, 0
        else:d,x,y = ext_gcd(b,a%b);x-=(a//b)*y 
        return d,y,x
    p,q=1,1
    for i in range(n-k+1,n+1):
        p=(p*i)%mod
    for i in range(2,k+1):
        q=(q*i)%mod
    return int(p*(ext_gcd(q, mod)[1]%mod)%mod)


n,m,k=map(int,input().split())
print(((pow(m,2,mod)*cmb(n*m-2,k-2,mod)*sum((n-i)*i%mod for i in range(1,n))%mod)+pow(n,2,mod)*cmb(n*m-2,k-2,mod)*sum((m-i)*i%mod for i in range(1,m))%mod)%mod)

Submission status: Runtime Error

Solution code:
from math import factorial
mod=10**9+7
def cmb(n, r):
    return factorial(n) // factorial(r) // factorial(n - r)%mod

n,m,k=map(int,input().split())
print((pow(m,2,mod)*cmb(n*m-2,k-2)*sum((n-i)*i for i in range(1,n)))+pow(n,2,mod)*cmb(n*m-2,k-2)*sum((m-i)*i for i in range(1,m)))%mod)

Submission status: Runtime Error

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

P = 100005
fact = [0] * P
fact[0] = 1
for i in range(1, P):
    fact[i] = fact[i - 1] * i
    fact[i] %= MOD

rfact = [0] * P
rfact[P - 1] = pow(fact[P - 1], MOD - 2, MOD)
for i in range(P - 2, -1, -1):
    rfact[i] = rfact[i + 1] * (i + 1)
    rfact[i] %= MOD


def comb(n, k):
    return fact[n] * rfact[k] * rfact[n - k] % MOD


num = 0
for d in range(1, N):
    num += d * (N - d) * (M ** 2)
    num %= MOD

for d in range(1, M):
    num += d * (M - d) * (N ** 2)
    num %= MOD

num *= comb(N * M - 2, K - 2)
print(num % MOD)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
ans = 0
MOD = 10**9+7
from scipy.special import comb
C = comb(m*n-2,k-2,exact=True)%MOD
for i in range(1,n):
    ans += (i*(n-i)*(m**2))*C
for i in range(1,m):
    ans += (i*(m-i)*(n**2))*C
print(ans%MOD)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
Max = n*m
#二項係数とその逆元テーブルを作る前処理
fac = [0]*(Max)
finv = [0]*(Max)
inv = [0]*(Max)
fac[0]=fac[1]=1
finv[0]=finv[1]=1
inv[1]=1
for i in range(2,Max):
        fac[i] = fac[i-1] * i % mod
        inv[i] = mod - inv[mod%i]*(mod//i)%mod
        finv[i] = finv[i-1]*inv[i]%mod
#O(1)でmod計算した組合せ数を計算
def C(n,r):
    if n < r:
        return 0
    if n < 0 or r < 0 :
        return 0
    return fac[n]*(finv[r]*finv[n-r]%mod)%mod
#mod計算した順列の計算
def P(n,r):
    if n < r:
        return 0
    if n<0 or r<0:
        return 0
    return (fac[n]*finv[n-r])%mod


c = C(n*m-2,k-2)
res = 0

for i in range(1,n):
    res += i * (n-i) *(m**2)*c
    res %= mod

for i in range(1,m):
    res += i*(m-i)*(n**2)*c
    res %= mod
print(res)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
ans = 0
MOD = 10**9+7
import  numpy as np
x = np.array([i for i in range(1,n+1)]*m)
y = np.array([i for i in range(1,m+1)]*n)
npabs = np.frompyfunc(abs,1,1)
for i in range(len(x)-1):
    x_ = x[i:]-x[i]
    x_ = np.sum(npabs(x_))
    y_ = y[i:]-y[i]
    y_ = np.sum(npabs(y_))
    ans += x_+y_
from scipy.special import comb
print((ans*comb(m*n-2,k-2,exact=True))%MOD)

Submission status: Runtime Error

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,k = map(int,input().split())
nm = n*m%(10**9+7)
k = k%(10**9+7)
a = (cmb(nm-2,k-2,mod)%(10**9+7))*(nm*(m*(n**2-1)+n*(m**2-1)))//6
print(a%(10**9+7))

Submission status: Runtime Error

Solution code:
from math import factorial
N, M, K = map(int, input().split())

MOD = 10**9+7

def comb(n, r):
    r = n-r if n-r < r else r
    if r == 0:
        return 1
    ndfact = 1
    for i in range(n, n-r, -1):
        ndfact *= i
    return ndfact // factorial(r)

p = comb(N*M-2, K-2) % MOD
ans = 0
for i in range(N):
    for j in range(M):
        if i == 0 and j == 0:
            continue
        d = i+j
        cnt = (N-i) * (M-j)
        if i != 0 and j != 0:
            cnt *= 2
        ans += d * cnt
        asn %= MOD

print((ans*p)%MOD)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
mod = 10**9+7
ans = 0

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 = 2*10**5
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 )

for d in range(1,N):
    ans += d*(N-d)*M**2 % mod
for d in range(1,M):
    ans += d*(M-d)*N**2 % mod
ans *= cmb(N*M-2, K-2, mod) % mod
print(ans)

Submission status: Runtime Error

Solution code:
N, M, K = [int(item) for item in input().split()]
MOD = 10**9 + 7

n = 10*6 
fac = [1] + [0] * n
fac_inv = [1] + [0] * n
for i in range(1, n+1):
    fac[i] = fac[i-1] * (i) % MOD
    # Fermat's little theorem says
    # a**(p-1) mod p == 1
    # then, a * a**(p-2) mod p == 1
    # it means a**(p-2) is inverse element
    fac_inv[i] = fac_inv[i-1] * pow(i, MOD-2, MOD) % MOD

def mod_nCr(n, r):
    if n == 0 and r == 0:
        return 1
    if n < r or n < 0:
        return 0
    tmp = fac_inv[n-r] * fac_inv[r] % MOD
    return tmp * fac[n] % MOD 

total = 0
for i in range(M):
    total += (M - i) * N * N * mod_nCr(M * N - 2, K - 2) * i
    total %= MOD  
for i in range(N):
    total += (N - i) * M * M * mod_nCr(M * N - 2, K - 2) * i
    total %= MOD  
print(total)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())

MOD = 10**9+7
fact = [1 for i in range(N+M+1)]
for i in range(2, N*M+1):
    fact[i] = fact[i-1] * i

def comb(n, r):
    return fact[n] // fact[n-r] // fact[r]

p = comb(N*M-2, K-2)
ans = 0
for i in range(N):
    for j in range(M):
        if i == 0 and j == 0:
            continue
        d = i+j
        #print(i, j, calc_rec_num(N, M, 0, 0, i, j))
        cnt = (N-i) * (M-j)
        if i != 0 and j != 0:
            cnt *= 2
        ans += d * cnt
        #if ans > DIV:
        #    ans %= DIV
        #print(ans)

print((ans*p)%MOD)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
mod = 10**9+7
ans = 0
def combination(a,b, mod):
    b = min(b, a-b)
    ans = 1
    for i in range(0,b):
        ans *= (a-i)/(i+1) % mod
    return int(ans)
for d in range(1,N):
    ans += d*(N-d)*M**2 % mod
for d in range(1,M):
    ans += d*(M-d)*N**2 % mod
ans *= combination(N*M-2, K-2, mod) % mod
print(ans)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())

MOD = 10**9+7
fact = [1 for i in range(N+1)]
for i in range(2, N+1):
    fact[i] = fact[i-1] * i

def comb(n, r):
    return fact[n] // fact[n-r] // fact[r]

p = comb(N*M-2, K-2)
ans = 0
for i in range(N):
    for j in range(M):
        if i == 0 and j == 0:
            continue
        d = i+j
        #print(i, j, calc_rec_num(N, M, 0, 0, i, j))
        cnt = (N-i) * (M-j)
        if i != 0 and j != 0:
            cnt *= 2
        ans += d * cnt
        #if ans > DIV:
        #    ans %= DIV
        #print(ans)

print((ans*p)%MOD)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())

MOD = 10**9+7
fact = [1 for i in range(N+1)]
for i in range(2, N+1):
    fact[i] = fact[i-1] * i

def comb(n, r):
    return fact[n] // fact[n-r] // fact[r]

p = comb(K-2, N*M-2)
ans = 0
for i in range(N):
    for j in range(M):
        if i == 0 and j == 0:
            continue
        d = i+j
        #print(i, j, calc_rec_num(N, M, 0, 0, i, j))
        cnt = (N-i) * (M-j)
        if i != 0 and j != 0:
            cnt *= 2
        ans += d * cnt
        #if ans > DIV:
        #    ans %= DIV
        #print(ans)

print((ans*p)%MOD)

Submission status: Runtime Error

Solution code:
setrecursionlimit(10**9)
#input = sys.stdin.readline
from itertools import accumulate
def inpl(): return list(map(int, input().split()))
MOD = 10**9 + 7
N, M, K = inpl()

X = sum([n*(n+1)//2 for n in range(1, N)])*M*(M)%MOD
Y = sum([m*(m+1)//2 for m in range(1, M)])*N*(N)%MOD


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


g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

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

print((X+Y)*cmb(N*M-2, K-2, MOD)%MOD)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())

DIV = 10**9+7

def comb(l, r):
    if l == 0:
        return 1
    val = 1
    for i in range(r, r-l, -1):
        val *= i
    for i in range(l, 0, -1):
        val //= i
    return val


#p = comb(K-2, N*M-2)
#print(K-2, N*M-2, p)
ans = 0
for i in range(N):
    for j in range(M):
        if i == 0 and j == 0:
            continue
        d = i+j
        #print(i, j, calc_rec_num(N, M, 0, 0, i, j))
        cnt = (N-i) * (M-j)
        if i != 0 and j != 0:
            cnt *= 2
        ans += d * cnt
        #if ans > DIV:
        #    ans %= DIV
        #print(ans)

print((ans*p)%DIV)

Submission status: Runtime Error

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

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

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

N,M,K=map(int,input().split())
keisuu=cmb(N*M-2,K-2,mod)

sum_=0
for i in range(N):
    a=min(abs(i),abs(N-i-1))
    b=max(abs(i),abs(N-i-1))
    sum_+=(M**2)*((a*(a+1)//2)+(b*(b+1)//2))
for i in range(M):
    a=min(abs(i),abs(M-i-1))
    b=max(abs(i),abs(M-i-1))
    sum_+=(N**2)*((a*(a+1)//2)+(b*(b+1)//2))
print(keisuu * (sum_//2))

Submission status: Runtime Error

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

N,M,K=map(int,input().split())
keisuu=cmb(N*M-2,K-2,10**9+7)

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

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

keisuu=cmb(N*M-2,K-2,mod)

sum_=0
for i in range(N):
    a=min(abs(i),abs(N-i-1))
    b=max(abs(i),abs(N-i-1))
    sum_+=(M**2)*((a*(a+1)//2)+(b*(b+1)//2))
for i in range(M):
    a=min(abs(i),abs(M-i-1))
    b=max(abs(i),abs(M-i-1))
    sum_+=(N**2)*((a*(a+1)//2)+(b*(b+1)//2))
print(keisuu * (sum_//2))

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())

DIV = 10**9+7

def comb(l, r):
    if l == 0:
        return 1
    val = 1
    for i in range(r, r-l, -1):
        val *= i
    for i in range(l, 0, -1):
        val /= i
    return val

def calc_rec_num(N, M, xi, xj, yi, yj):
    cnt = 0
    cnt += N - yi
    cnt += M - yj
    # rotate
    cnt *= 2
    return cnt


p = comb(K-2, N*M-2)
ans = 0
for i in range(N):
    for j in range(M):
        if i == 0 and j == 0:
            continue
        d = i+j
        ans += d * calc_rec_num(N, M, 0, 0, i, j) * p

print(ans % DIV)

Submission status: Runtime Error

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


c = (((n*n-1)+2)2)%1000000007
d = ((n*(n-1))/4)%1000000007
la = (c*d*m*m)%1000000007

e = (((m*m-1)+2)//2)%1000000007
f = ((m*(m-1))/4)%1000000007
ja = (e*f*n*n)%1000000007

print((la+ja)%1000000007)

Submission status: Runtime Error

Solution code:



N,M=[int(c) for c in input().split()]
A=[int(c) for c in input().split()]
A.sort()


def find(X,t,s):#Xのうち, tよりも小さい最大のインデックスを返す
    if(len(X)==0):
		return s
    elif(X[-1]<t):
      	return s+len(X)
	elif(len(X)==1):
		if(X[0]<=t):
			return s
		else:
			return s-1
	elif(X[len(X)//2]<=t):
		return find(X[len(X)//2:],t,s+len(X)//2)
	else:
		return find(X[:len(X)//2],t,s)



for i in range(M):
	b,c=[int(c) for c in input().split()]

	id=find(A,c,0)

	if(id==-1):
		continue
	elif(b<=id):
		A=A[b:id+1]+[c]*b+A[id+1:]
	else:
		A=[c]*id+A[id:]
print(sum(A))

Submission status: Runtime Error

Solution code:
def dist(arr, n):
	res = 0; su = 0
	for i in range(n):
		res = (res + (arr[i] * i) - su) % mod
		su = (su + arr[i]) % mod

	return res % mod

n,m,k = map(int,input().split())
mod = int(1e9+7)
fact = [1]
for i in range(1,int(1e5)+1):
	fact.append((fact[-1] * i)%mod)

x = []
y = []

for i in range(1,n+1):
	for j in range(1,m+1):
		x.append(i)
		y.append(j)

y.sort()

tot = (dist(x,n*m) + dist(y,n*m))%mod
N = n*m - 2
Need = k-2
num = fact[N]
den = (fact[Need] * fact[N-Need])%mod
ncr = (num * pow(den,mod-2,mod)) % mod
ans = (tot * ncr) % mod
print(ans)

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
O=10**9+7
f=lambda n:1if n<1else n*f(n-1)%O;
print(f(N*M-2)*pow(6*f(N*M-K)*f(K-2),O-2,O)*N*M*(N*M-1)*(N+M)%O)