qyliang/CodeNanoFix huggingface dataset:
Buggy code:
from operator import itemgetter
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
K = [[int(i) for i in input().split()] for j in range(M)]
K.sort()
K.sort(key=itemgetter(1))
#B_C = list(map(list, zip(*K)))
A.sort()

ans = 0
changed = []
for i in K[::-1]:
    changed += [i[1]]*i[0]
    if len(changed) < N:
        continue
    else:
        while(len(changed) > N):
            changed.pop()
        break

print(changed.__len__(),A.__len__())
for i in range(N):
    if changed[i] <= A[i]:
        SUM = sum(changed[:i]) + sum(A[i:])
        print(SUM)
        exit()
else:
    print(sum(changed))


Fixed code:
from operator import itemgetter
N, M = map(int, input().split())
A = [int(i) for i in input().split()]
K = [[int(i) for i in input().split()] for j in range(M)]
K.sort()
K.sort(key=itemgetter(1))
#B_C = list(map(list, zip(*K)))
A.sort()

changed = []
for i in K[::-1]:
    changed += [i[1]]*i[0]
    if len(changed) < N:
        continue
    else:
        while(len(changed) > N):
            changed.pop()
        break
if len(changed) < N:
    changed += [0]*(N-len(changed))
#print(changed.__len__(),A.__len__())
for i in range(N):
    if changed[i] <= A[i]:
        SUM = sum(changed[:i]) + sum(A[i:])
        print(SUM)
        exit()
else:
    print(sum(changed))
Buggy 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()
    A=LI()
    CB=[[0,0]for _ in range(M)]
    A.sort()
    for i in range(M):
        CB[i][1],CB[i][0]=MI()
        
    CB.sort(reverse=True)
    now=0
    
    for i in range(N):
        if CB[now][1]<=0:
            now+=1
        ch=CB[now][0]
        if A[i]<ch:
            A[i]=ch
            CB[now][1]-=1
        else:
            break
        
    print(sum(A))
    

main()


Fixed 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()
    A=LI()
    CB=[[0,0]for _ in range(M)]
    A.sort()
    for i in range(M):
        CB[i][1],CB[i][0]=MI()
        
    CB.sort(reverse=True)
    now=0
    
    for i in range(N):
        if CB[now][1]<=0:
            now+=1
        
        if now>=M:
            break    
        
        ch=CB[now][0]
        if A[i]<ch:
            A[i]=ch
            CB[now][1]-=1
        else:
            break
        
    print(sum(A))
    

main()
Buggy code:
N, M = map(int,input().split())
A = [i for i in map(int,input().split())]
BC = [[i for i in map(int,input().split())] for j in range(M)]
A.sort()
BC.sort(reverse=True, key=lambda x:x[1])
print(A)
print(BC)

ans_arr = [0 for i in range(N)]

j = 0
for i in range(N):
  if j < M:
    if A[i] < BC[j][1]:
      ans_arr[i] = BC[j][1]
      BC[j][0] -= 1
      if BC[j][0] == 0:
        j+=1
    else:
      ans_arr[i] = A[i]
  else:
    ans_arr[i] = A[i]
      
print(sum(ans_arr))

Fixed code:
N, M = map(int,input().split())
A = [i for i in map(int,input().split())]
BC = [[i for i in map(int,input().split())] for j in range(M)]
A.sort()
BC.sort(reverse=True, key=lambda x:x[1])

ans_arr = [0 for i in range(N)]

j = 0
for i in range(N):
  if j < M:
    if A[i] < BC[j][1]:
      ans_arr[i] = BC[j][1]
      BC[j][0] -= 1
      if BC[j][0] == 0:
        j+=1
    else:
      ans_arr[i] = A[i]
  else:
    ans_arr[i] = A[i]
      
print(sum(ans_arr))
Buggy code:
rn = lambda :map(int, read().split())
n,m = rn()
A = list(rn())
l = len(A)
d = {}
for x in A:
    if x not in d:
        d[x]=0
    d[x] += 1
for i in range(m):
    b, c = rn()
    if c not in d:
        d[c]=0
    d[c] += b
A = list(d.items())
A.sort(reverse=True)
ans = 0
cnt = l
# print(A)
for v, c in A:
    tmp = min(cnt, c)
    ans += tmp * v
    cnt -= tmp
print(ans)

Fixed code:
read = input 
rn = lambda :map(int, read().split())
n,m = rn()
A = list(rn())
l = len(A)
d = {}
for x in A:
    if x not in d:
        d[x]=0
    d[x] += 1
for i in range(m):
    b, c = rn()
    if c not in d:
        d[c]=0
    d[c] += b
A = list(d.items())
A.sort(reverse=True)
ans = 0
cnt = l
# print(A)
for v, c in A:
    tmp = min(cnt, c)
    ans += tmp * v
    cnt -= tmp
print(ans)
Buggy code:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*b
    idx += b
print(ad)

Fixed code:
n, m = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
bc = [list(map(int,input().split())) for _ in range(m)]
bc.sort(key=lambda x:x[1], reverse=True)

ad = 0
idx = 0
for b, c in bc:
    for i in range(min(b, n-idx)):
        if a[idx+i] < c:
            continue
        else:
            ad += c*i
            idx += i
            print(ad + sum(a[idx:]))
            exit()
    ad += c*min(b, n-idx)
    idx += min(b, n-idx)
print(ad + sum(a[idx:]))
Buggy code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
b = []
for i in range(M):
    b.append(list(map(int, input().split())))
ans = 0
A.sort()
b.sort(key=lambda x: x[1], reverse=True)
j = 0
for i in range(M):
    if b[i][1] <= A[j] or j == N:
        break
    while j < N and b[i][0]:
        ans += b[i][1]
        b[i][0] -= 1
        j += 1
print(ans + sum(A[j:]))

Fixed code:
N, M = map(int, input().split())
A = list(map(int, input().split()))
b = []
for i in range(M):
    b.append(list(map(int, input().split())))
ans = 0
A.sort()
b.sort(key=lambda x: x[1], reverse=True)
j = 0
for i in range(M):
    if j >= N or b[i][1] <= A[j]:
        break
    while j < N and b[i][0] and b[i][1] > A[j]:
        ans += b[i][1]
        b[i][0] -= 1
        j += 1
if j >= N:
    print(ans)
else:
    print(ans + sum(A[j:]))
Buggy code:
import numpy as np
from collections import Counter

N, M = map(int, input().split())
A = list(map(int, input().split()))
B = []
C = []
for _ in range(M):
    b1, c1 = map(int, input().split())
    B.append(b1)
    C.append(c1)

A = list(Counter(A).items())

for i in range(len(A)):
    C.append(A[i][0])
    B.append(A[i][1])
 
d = np.argsort(C)[::-1]
B = [B[i] for i in d]
C = [C[i] for i in d]

i = 0
c = 0
ans = 0
while True:
    if c + B[i] < N:
        ans += C[i] * B[i]
        i += 1
        c += B[i]
    else:
        ans += C[i] * (N - c)
        break
print(ans)


Fixed code:
import numpy as np
from collections import Counter

N, M = map(int, input().split())
A = list(map(int, input().split()))
B = []
C = []
for _ in range(M):
    b1, c1 = map(int, input().split())
    B.append(b1)
    C.append(c1)

A = list(Counter(A).items())

for i in range(len(A)):
    C.append(A[i][0])
    B.append(A[i][1])
 
d = np.argsort(C)[::-1]
B = [B[i] for i in d]
C = [C[i] for i in d]

i = 0
c = 0
ans = 0
while True:
    if c + B[i] < N:
        ans += C[i] * B[i]
        c += B[i]
        i += 1
    else:
        ans += C[i] * (N - c)
        break
print(ans)
Buggy code:
N, M = map(int, input().split())

A = list(map(int, input().split()))

L = [list(map(int, input().split())) for _ in range(M)]

L.sort(reverse=True, key=lambda x:x[1])
A.sort()



ans = 0
i = 0
j = 0
k = 0
for i in range(N):
    if A[i] < L[j][1]:
        ans += L[j][1]
        k += 1
        
    else:
        ans += A[i]
        
    if k == L[j][0]:
        k = 0
        j += 1
        
print(ans)
    

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

A = list(map(int, input().split()))

L = [list(map(int, input().split())) for _ in range(M)]

L.sort(reverse=True, key=lambda x:x[1])
A.sort()


ans = 0
i = 0
j = 0
k = 0
for i in range(N):
    if j < M:
        if A[i] < L[j][1]:
            ans += L[j][1]
            k += 1
            
        else:
            ans += A[i]
        
        
        if k == L[j][0]:
            k = 0
            j += 1
            
    else:
        ans += A[i]
        

        
print(ans)
Buggy code:
N, M = list(map(int, input().split()))
A_list = list(map(int, input().split()))

card = []
card_num = []

for a in A_list:
    card.append(a)
    card_num.append(1)
    
for _ in range(M):
    B, C = list(map(int, input().split()))
    card.append(C)
    card_num.append(B)
    
union = zip(card, card_num)
sorted_union = sorted(union, key=lambda x:x[0])[-1::-1]
sorted_card, sorted_card_num = zip(*sorted_union)a

ans = 0
current_num = 0
for c, c_num in zip(sorted_card, sorted_card_num):
    if current_num + c_num <= N:
        current_num += c_num
        ans += c * c_num
    else:
        current_num = N
        ans += c * (N - current_num)
print(ans)

Fixed code:
N, M = list(map(int, input().split()))
A_list = list(map(int, input().split()))

card = []
card_num = []

for a in A_list:
    card.append(a)
    card_num.append(1)
        
for _ in range(M):
    B, C = list(map(int, input().split()))
    card.append(C)
    card_num.append(B)
    
union = zip(card, card_num)
sorted_union = sorted(union, key=lambda x:x[0])[-1::-1]
sorted_card, sorted_card_num = zip(*sorted_union)

ans = 0
current_num = 0
for c, c_num in zip(sorted_card, sorted_card_num):
    if current_num + c_num < N:
        current_num += c_num
        ans += c * c_num
    else:
        ans += c * (N - current_num)
        current_num = N
        break
print(ans)
Buggy code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n, m = na()
a = na()
cb = []
for i in range(m):
    b, c = na()
    cb.append([c, b])

a.sort()
cb.sort(reverse=True)

cur = 0
for i in range(n):
    c, b = cb[cur]
    if a[i] < c:
        a[i] = c
        b -= 1
        if b == 0:
            cur += 1
        cb[cur][1] = b
    else:
        break

print(sum(a))

Fixed code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n, m = na()
a = na()
cb = []
for i in range(m):
    b, c = na()
    cb.append([c, b])

a.sort()
cb.sort(reverse=True)

cur = 0
for i in range(n):
    if cur >= len(cb):
        break
    c, b = cb[cur]
    if a[i] < c:
        a[i] = c
        b -= 1
        cb[cur][1] = b
        if b == 0:
            cur += 1
    else:
        break

print(sum(a))
Buggy code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
#bc=[]

import heapq  
heapq.heapify(a) 

bc=[]
for _ in range(m):
    b,c=map(int,input().split())
    bc.append((b,c))

bc.sort(reverse=True)
for item in bc:
    b=item[0]
    c=item[1]    
    for _ in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
            print(sum(list(a)))
            exit()
#print(a)
print(sum(list(a)))

Fixed code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
#bc=[]

import heapq  
heapq.heapify(a) 

bc=[]
for _ in range(m):
    b,c=map(int,input().split())
    bc.append((c,b))

bc.sort(reverse=True)
for item in bc:
    b=item[1]
    c=item[0]    
    for _ in range(b):
        temp=heapq.heappop(a)
        if temp<c:
            heapq.heappush(a,c)
        else:
            heapq.heappush(a,temp)
            print(sum(list(a)))
            exit()
#print(a)
print(sum(list(a)))
Buggy code:
mod = 10 ** 9 + 7
class nCr():
    def __init__(self, n):
        self.n = n
        self.fa = [1] * (self.n + 1)
        self.fi = [1] * (self.n + 1)
        for i in range(1, self.n + 1):
            self.fa[i] = self.fa[i - 1] * i % mod
            self.fi[i] = pow(self.fa[i], mod - 2, mod)
    def comb(self, n, r):
        if n < r:return 0
        if n < 0 or r < 0:return 0
        return self.fa[n] * self.fi[r] % mod * self.fi[n - r] % mod
n, m, k = map(int, input().split())
ans = 0
c = nCr(n + m)
for i in range(n):
    ans += i * (n - i) * m * m
    ans %= mod
for i in range(m):
    ans += i * (m - i) * n * n
    ans %= mod
print(ans * c.comb(n * m - 2, k - 2) % mod)

Fixed code:
mod = 10 ** 9 + 7
class nCr():
    def __init__(self, n):
        self.n = n
        self.fa = [1] * (self.n + 1)
        self.fi = [1] * (self.n + 1)
        for i in range(1, self.n + 1):
            self.fa[i] = self.fa[i - 1] * i % mod
            self.fi[i] = pow(self.fa[i], mod - 2, mod)
    def comb(self, n, r):
        if n < r:return 0
        if n < 0 or r < 0:return 0
        return self.fa[n] * self.fi[r] % mod * self.fi[n - r] % mod
n, m, k = map(int, input().split())
ans = 0
c = nCr(n * m)
for i in range(n):
    ans += i * (n - i) * m * m
    ans %= mod
for i in range(m):
    ans += i * (m - i) * n * n
    ans %= mod
print(ans * c.comb(n * m - 2, k - 2) % mod)
Buggy code:
N,M,K=map(int,input().split())
P=1000000007
f=[0]*(N*M+1)
f[0]=1
for i in range(N*M):
    f[i+1]=(i+1)*f[i]%P
ans=0
for A in [N,M]:
    for i in range(1,A):
        ans = (ans+(N^M^A)**2*i*(i+1)//2)%P
print(ans*f[N*M-2]*pow(f[K-2],P-2,P)*pow(f[N-K],P-2,P)%P)

Fixed code:
N,M,K = map(int, input().split())
P=1000000007
MPL = 1
f = 1
for i in range(1, N*M-1):
    MPL = MPL * i % P
    MPL %= P
    if i == K - 2:
        f *= pow(MPL, P - 2, P)
    if i == N*M - K:
        f *= pow(MPL, P - 2, P)
MPL = MPL * f % P
ans = 0
for A in [N, M]:
    width = N ^ M ^ A
    for i in range(1, A):
        ans = (ans + width * width * i * (i + 1) // 2) % P
print(ans * MPL % P)
Buggy code:
MOD = 10 ** 9 + 7
n, m, k = map(int, input().split())
def f(i, j):
    return i*(i-1)*(i+1)//6 * j ** 2
def c(a, b):
    r = 1
    for i in range(a, a - b, -1):
        r = r * i // (a + 1 - i)
        r %= MOD
    return r
print((f(n, m) + f(m, n)) * c(n*m - 2, k - 2) % MOD)


Fixed code:
MOD = 10 ** 9 + 7
n, m, k = map(int, input().split())
def f(i, j):
    return i*(i-1)*(i+1)//6 * j ** 2
def c(a, b):
    r = 1
    for i in range(a, a - b, -1):
        r = r * i % MOD
    t = 1
    for i in range(1, b + 1):
        t = t * i % MOD
    return r * pow(t, MOD - 2, MOD)
print((f(n, m) + f(m, n)) * c(n*m - 2, k - 2) % MOD)
Buggy code:
MOD=10**9+7
UPPERLIMIT=3*10**5
MODMUL=[1, 1]+[0]*(UPPERLIMIT-1)
for i in range(2, UPPERLIMIT+1):
  MODMUL[i]=MODMUL[i-1]*i%MOD
MODDIV=[1]*UPPERLIMIT+[pow(MODMUL[-1], MOD-2, MOD)]
for i in range(UPPERLIMIT, 0, -1):
  MODDIV[i-1]=MODDIV[i]*i%MOD
def MODCOMB(n, r):
  if n<r or n<1:
    return 0
  else:
    return (((MODMUL[n]*MODDIV[r])%MOD)*MODDIV[n-r])%MOD

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

ans=0
k=MODCOMB(N*M-2, K-2)
P=min(N, M)
Q=max(N, M)

for sa in range(1, P):
  ans=(ans+sa*(P-sa)*M*M*k)%MOD
  ans=(ans+sa*(P-sa)*N*N*k)%MOD
  
for sa in range(P, Q):
  ans=(ans+sa*(Q-sa)*P*P*k)%MOD

print(ans)

Fixed code:
MOD=10**9+7
UPPERLIMIT=3*10**5
MODMUL=[1, 1]+[0]*(UPPERLIMIT-1)
for i in range(2, UPPERLIMIT+1):
  MODMUL[i]=MODMUL[i-1]*i%MOD
MODDIV=[1]*UPPERLIMIT+[pow(MODMUL[-1], MOD-2, MOD)]
for i in range(UPPERLIMIT, 0, -1):
  MODDIV[i-1]=MODDIV[i]*i%MOD
def MODCOMB(n, r):
  if n<r or n<1:
    return 0
  else:
    return (((MODMUL[n]*MODDIV[r])%MOD)*MODDIV[n-r])%MOD

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

ans=0
k=MODCOMB(N*M-2, K-2)
for sa in range(1, N):
  ans=(ans+sa*(N-sa)*M*M*k)%MOD
    
for sa in range(1, M):
  ans=(ans+sa*(M-sa)*N*N*k)%MOD
    
print(ans)
Buggy code:
n, m, k = map(int, input().split())
mod = 10**9 + 7

fac = [1] * n*m
finv = [1] * n*m
inv = [1] * n*m

ans = 0
def COMinit():
    for i in range(2, n*m):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def COM(n, k):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod
COMinit()
for i in range(1, n):
    ans += (COM(n*m - 2, k-2)) * ((n-i)*i * m**2)
for j in range(1, m):
    ans += (COM(n*m - 2, k-2)) * ((m-j)*j * n**2)
print(ans)

Fixed code:
n, m, k = map(int, input().split())
mod = 10**9 + 7

fac = [1] * n*m
finv = [1] * n*m
inv = [1] * n*m

ans = 0
def COMinit():
    for i in range(2, n*m):
        fac[i] = fac[i - 1] * i % mod
        inv[i] = mod - inv[mod % i] * (mod // i) % mod
        finv[i] = finv[i - 1] * inv[i] % mod


def COM(n, k):
    if n < 0 or k < 0 or n < k:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod
COMinit()
for i in range(1, n):
    ans += (COM(n*m - 2, k-2)) * ((n-i)*i * m**2)%mod
for j in range(1, m):
    ans += (COM(n*m - 2, k-2)) * ((m-j)*j * n**2)%mod
print(ans%mod)
Buggy code:
n,m,k=map(int,input().split())
mod=10**9+7
f=[1]
for i in range(2*10**5+7):f+=[f[-1]*(i+1)%mod]
def comb(a,b):return f[a]*pow(f[b],mod-2,mod)*pow(f[a-b],mod-2,mod)%mod
ans=0
for i in range(1,n):
  ans+=(i*(n-i)*m**2)*(comb(n+m-2,k-2))
  ans%=mod
for i in range(1,m):
  ans+=(i*(m-i)*n**2)*(comb(n+m-2,k-2))
  ans%=mod
print(ans)

Fixed code:
n,m,k=map(int,input().split())
mod=10**9+7
f=[1]
for i in range(2*10**5+7):f+=[f[-1]*(i+1)%mod]
def comb(a,b):return f[a]*pow(f[b],mod-2,mod)*pow(f[a-b],mod-2,mod)%mod
ans=0
for i in range(1,n):
  ans+=(i*(n-i)*m**2)*(comb(n*m-2,k-2))
  ans%=mod
for i in range(1,m):
  ans+=(i*(m-i)*n**2)*(comb(n*m-2,k-2))
  ans%=mod
print(ans)
Buggy code:
MOD = 10 ** 9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


def modCombi(n, r):
    global MOD
    return (modFacts[n] * invs[n - r] * invs[r]) % MOD


N, M, K = map(int, input().split())
modFacts, invs = prepare(N * M)
var = modCombi(N * M - 2, K - 2)

ans = 0
if N < M:
    N, M = M, N
L = [0] * (N + 1)
for i in range(1, N + 1):
    L[i] = L[i - 1] + i
for x in range(1, N + 1):
    Sx = (M * (L[N - x] + L[x - 1])) % MOD
    Sy = (N * (L[M - x] + L[x - 1])) % MOD
    ans += ((Sx + Sy) * M) % MOD

print((ans * invs[2] * var) % MOD)



Fixed code:
MOD = 10 ** 9 + 7


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


def modCombi(n, r):
    global MOD
    return (modFacts[n] * invs[n - r] * invs[r]) % MOD


N, M, K = map(int, input().split())
modFacts, invs = prepare(N * M)
var = modCombi(N * M - 2, K - 2)

ans = 0
if N < M:
    N, M = M, N
L = [0] * (N + 1)
for i in range(1, N + 1):
    L[i] = L[i - 1] + i
for x in range(1, N + 1):
    for y in range(1, M + 1):
        Sx = (M * (L[N - x] + L[x - 1])) % MOD
        Sy = (N * (L[M - y] + L[y - 1])) % MOD
        ans += (Sx + Sy) % MOD

print((ans * invs[2] * var) % MOD)
Buggy code:
import math

inf = 7 + (10 ** 9)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)


n, m, k = map(int, input().split())
ans = 0
for i in range(m-1):
    for j in range(i+1, m):
        ans += (combinations_count((n*m)-2, k-2) * n * n * (j - i))
        ans = ans % inf
for i in range(n-1):
    for j in range(i+1, n):
        ans += combinations_count((n*m)-2, k-2) * m * m * (j - i)
        ans = ans % inf
ans = ans % inf
print(ans)

Fixed code:
import math
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


n, m, k = map(int, input().split())
cmb = combinations_count(n * m - 2, k - 2)
cmb %= 10 ** 9 + 7
ans = 0
for i in range(1, n):
    ans += i * cmb * m * m * (n - i)
    ans %= 10 ** 9 + 7

for i in range(1, m):
    ans += i * cmb * n * n * (m - i)
    ans %= 10 ** 9 + 7

print(ans % (10 ** 9 + 7))
Buggy code:
MOD = 10 ** 9 + 7

def mod_combination(n, k):
    def extended_gcd(a, b):
        if b == 0:
            return (a, 1, 0)
        d, x, y = extended_gcd(b, a % b)
        return (d, y, x - (a // b) * y)
    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 p * (extended_gcd(q, MOD)[1] % MOD) % MOD

def mod_pow(a, b):
    return pow(a, b, MOD)


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

X = mod_pow(M, 2) * (mod_pow(N, 3) - N) % MOD
Y = mod_pow(N, 2) * (mod_pow(M, 3) - M) % MOD

comb = mod_combination(N * M - 2, K - 2)

ans = ((X + Y) % MOD) // 6 * comb % MOD

print(ans)


Fixed code:
MOD = 10 ** 9 + 7

def mod_combination(n, k):
    def extended_gcd(a, b):
        if b == 0:
            return (a, 1, 0)
        d, x, y = extended_gcd(b, a % b)
        return (d, y, x - (a // b) * y)
    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 p * (extended_gcd(q, MOD)[1] % MOD) % MOD

def mod_pow(a, b):
    return pow(a, b, MOD)


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

X = mod_pow(M, 2) * (mod_pow(N, 3) - N) % MOD
Y = mod_pow(N, 2) * (mod_pow(M, 3) - M) % MOD

comb = mod_combination(N * M - 2, K - 2)

ans = (X + Y) * mod_pow(6, MOD - 2) * comb % MOD

print(ans)
Buggy code:
mod = 1000000007
import sys
input = sys.stdin.readline
N, M, K = map(int, input().split())


def cmb(n, r):
    if n<0 or r<0 or r>n:
        return None
    r = min(r, n-r)
    if r == 0:
        return 0
    if r == 1:
        return n

    numerator = [0]*r
    denominator = [0]*r
    for k in range(r):
        numerator[k] = n-r+k+1
        denominator[k] = k+1
    for p in range(2, r+1):
        pivot = denominator[p-1]
        if pivot > 1:
            offset = (n-r) % p
            for k in range(p-1, r, p):
                numerator[k-offset] //= pivot
                denominator[k] //= pivot
    res = 1
    for n in numerator:
        if n > 1:
            res *= n
    return res

N2 = N**2
M2 = M**2
Xpattern = [(x * (N-x)) % mod for x in range(N)]
Ypattern = [(y * (M-y)) % mod for y in range(M)]
coef = cmb(N*M-2, K-2) % mod

print(((sum(Xpattern)*M2+sum(Ypattern)*N2)*coef)%mod)

Fixed code:
mod = 1000000007
import sys
input = sys.stdin.readline
N, M, K = map(int, input().split())


def cmb(n, r):
    if n<0 or r<0 or r>n:
        return None
    r = min(r, n-r)
    if r == 0:
        return 1
    if r == 1:
        return n
    numerator = [0]*r
    denominator = [0]*r
    for k in range(r):
        numerator[k] = n-r+k+1
        denominator[k] = k+1
    for p in range(2, r+1):
        pivot = denominator[p-1]
        if pivot > 1:
            offset = (n-r) % p
            for k in range(p-1, r, p):
                numerator[k-offset] //= pivot
                denominator[k] //= pivot
    res = 1
    for n in numerator:
        if n > 1:
            res *= n
    return res

N2 = N**2
M2 = M**2
Xpattern = [(x * (N-x)) % mod for x in range(N)]
Ypattern = [(y * (M-y)) % mod for y in range(M)]
coef = cmb(N*M-2, K-2)

print(((sum(Xpattern)*M2+sum(Ypattern)*N2)*coef)%mod)