qyliang/CodeNanoFix huggingface dataset:
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

MOD = 10 ** 9 + 7

class Factorial:
    def __init__(self, n, mod):
        self.f = [1]
        self.mod = mod
        for j in range(1, n + 1):
            self.f.append(self.f[-1] * j % mod)
        self.i = [pow(self.f[-1], mod - 2, mod)]
        for j in range(n, 0, -1):
            self.i.append(self.i[-1] * j % mod)
        self.i.reverse()
    def factorial(self, j):
        return self.f[j]
    def ifactorial(self, j):
        return self.i[j]
    def comb(self, n, k):
        return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0

F = Factorial(N + 1, MOD).comb

tmp = 0

if K == 1:
    for a in A:
        tmp += a
        tmp %= MOD
    print (tmp)
    exit()

lst = [0] * N
K -= 1
for i in range(K, N):
    tmp += F(i - 1, K - 1)
    tmp %= MOD
    lst[i] = tmp

# print (lst)

ans = 0
for a, tmp in zip(A, lst):
    ans += a * tmp
    ans %= MOD

for index, a in enumerate(A):
    if N - 1 - index < 0:
        break
    ans -= a * (F(N - 1 - index, K))
    ans %= MOD

print (ans)

Fixed code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

MOD = 10 ** 9 + 7

class Factorial:
    def __init__(self, n, mod):
        self.f = [1]
        self.mod = mod
        for j in range(1, n + 1):
            self.f.append(self.f[-1] * j % mod)
        self.i = [pow(self.f[-1], mod - 2, mod)]
        for j in range(n, 0, -1):
            self.i.append(self.i[-1] * j % mod)
        self.i.reverse()
    def factorial(self, j):
        return self.f[j]
    def ifactorial(self, j):
        return self.i[j]
    def comb(self, n, k):
        return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod if n >= k else 0

F = Factorial(N + 1, MOD).comb

tmp = 0

if K == 1:
    print (0)
    exit()

lst = [0] * N
K -= 1
for i in range(K, N):
    tmp += F(i - 1, K - 1)
    tmp %= MOD
    lst[i] = tmp

# print (lst)

ans = 0
for a, tmp in zip(A, lst):
    ans += a * tmp
    ans %= MOD

for index, a in enumerate(A):
    if N - 1 - index < 0:
        break
    ans -= a * (F(N - 1 - index, K))
    ans %= MOD

print (ans)
Buggy code:
fac = [1, 1] + [0]*(N-1)
finv = [1, 1] + [0]*(N-1)
inv = [1, 1] + [0]*(N-1)

def cmbInit():
    for i in range(2, N+1):
        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 cmb(n, r):
    if n < r:
        return 0
    elif n < 0 or r < 0:
        return 0
    else:
        return fac[n] * (finv[r]*finv[n-r] % mod) % mod


N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
mod = 10**9+7
ans = 0
cmbInit()

for i in range(N-1, K-2, -1):
    ans = (ans + (A[i]-A[N-1-i]) * cmb(i, K-1)) % mod

print(ans)

Fixed code:
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
mod = 10**9+7
ans = 0
fac = [1, 1] + [0]*(N-1)
finv = [1, 1] + [0]*(N-1)
inv = [1, 1] + [0]*(N-1)


def cmbInit():
    for i in range(2, N+1):
        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 cmb(n, r):
    if n < r:
        return 0
    elif n < 0 or r < 0:
        return 0
    else:
        return fac[n] * (finv[r]*finv[n-r] % mod) % mod


cmbInit()

for i in range(N-1, K-2, -1):
    ans = (ans + (A[i]-A[N-1-i]) * cmb(i, K-1)) % mod

print(ans)
Buggy code:
def cmb(a,b,c):
    b = min(b,a-b)
    num = 1
    for i in range(b):
        num = num*(a-i) % c
    den = 1
    for i in range(b):
        den = den*(i+1) % c
    return num * pow(den,c-2,c) % c

mod = 10**9+7
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
ans = 0
c = cmb(n-1,k-1,mod)
if k == 1:
    print(0)
    exit()
for i in range(n-k+1):
    ans -= a[i] * c
    ans += a[-i-1] * c
    c = c * (n-k-i) //  (n-i-1)
print(ans)

Fixed code:
def cmb(a,b,c):
    b = min(b,a-b)
    num = 1
    for i in range(b):
        num = num*(a-i) % c
    den = 1
    for i in range(b):
        den = den*(i+1) % c
    return num * pow(den,c-2,c) % c

mod = 10**9+7
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
ans = 0
c = cmb(n-1,k-1,mod)
if k == 1:
    print(0)
    exit()
for i in range(n-k+1):
    ans -= a[i] * c
    ans += a[-i-1] * c
    ans %= mod
    c = (c * (n-k-i) * pow(n-i-1,mod-2,mod)) % mod
if ans < 0:
    ans += mod
print(ans)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))

fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % p


def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], p - 2, p) * pow(fac[k], p - 2, p) % p


A.sort(reverse=True)
result = 0
for i in range(N - K + 1):
    result += A[i] * mcomb(N - (i + 1), K - 1)
    result %= p

A.sort()
for i in range(N - K + 1):
    result -= A[i] * mcomb(N - (i + 1), K - 1)
    result %= p

# result += p
# result %= p
print(result)


Fixed code:
p = 1000000007

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

fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % p


def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], p - 2, p) * pow(fac[k], p - 2, p) % p


A.sort(reverse=True)
result = 0
for i in range(N - K + 1):
    result += A[i] * mcomb(N - (i + 1), K - 1)
    result %= p

A.reverse()
t = 0
for i in range(N - K + 1):
    t += A[i] * mcomb(N - (i + 1), K - 1)
    t %= p

result -= t
result %= p
# result += p
# result %= p
print(result)
Buggy code:
N, K = map(int, input().split())
*A, = map(int, input().split())
A.sort()

mod = 10**9+7
fac = [1]*(N+1)
rev = [1]*(N+1)
 
for i in range(1,N+1):
  fac[i] = i*fac[i-1]%mod
  rev[i] = pow(fac[i], mod-2, mod)
 
comb = lambda a,b:(fac[a]*rev[a-b]*rev[b])%mod

maxX, minX = 0, 0

for i in range(N-K+1):
    minX += A[i]*comb(N-i-1, K-1)
    minX %= mod

for j in range(K-1, N):
    maxX += A[j]*comb(j, K-1)
    maxX %= mod

print(maxX-minX)

Fixed code:
N, K = map(int, input().split())
*A, = map(int, input().split())
A.sort()

mod = 10**9+7
fac = [1]*(N+1)
rev = [1]*(N+1)
 
for i in range(1,N+1):
  fac[i] = i*fac[i-1]%mod
  rev[i] = pow(fac[i], mod-2, mod)
 
comb = lambda a,b:(fac[a]*rev[a-b]*rev[b])%mod

maxX, minX = 0, 0

for i in range(N-K+1):
    minX += A[i]*comb(N-i-1, K-1)

for j in range(K-1, N):
    maxX += A[j]*comb(j, K-1)

print((maxX-minX)%mod)
Buggy code:
mod = 10**9+7
fact = [1]
fact_inv = [1]
for i in range(10**5):
  new_fact = fact[-1]*(i+1)%mod
  fact.append(new_fact)
  fact_inv.append(pow(new_fact,mod-2,mod))

def mod_comb_k(n,k,mod):
  return fact[n]*fact_inv[k]%mod*fact_inv[n-k]%mod

N,K = map(int,input().split())
arr = list(map(int,input().split()))
arr = sorted(arr)
_max = 0
_min = 0
for i in range(K-1,N):
  _max += arr[i]*mod_comb_k(i,K-1,mod)
  _min += arr[i-K+1]*mod_comb_k(N-i+K-2,K-1,mod)
print(_max-_min)

Fixed code:
mod = 10**9+7
fact = [1]
fact_inv = [1]
for i in range(10**5):
  new_fact = fact[-1]*(i+1)%mod
  fact.append(new_fact)
  fact_inv.append(pow(new_fact,mod-2,mod))

def mod_comb_k(n,k,mod):
  return fact[n]*fact_inv[k]%mod*fact_inv[n-k]%mod

N,K = map(int,input().split())
arr = list(map(int,input().split()))
arr = sorted(arr)
_max = 0
_min = 0
for i in range(K-1,N):
  _max += arr[i]*mod_comb_k(i,K-1,mod)
  _min += arr[i-K+1]*mod_comb_k(N-i+K-2,K-1,mod)
print((_max-_min)%mod)
Buggy code:
import math

n,k = list(map(int, input().split(" ")))
nums  = list(map(int, input().split(" ")))
nums.sort()

mod = 10**9 + 7
f = [1]
for i in range(1,10**5+1):
    f.append(f[-1] * i)

def nCr(n,r):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

minSum = 0
maxSum = 0

for i in range(n-k+1):
    minSum += nums[i] * nCr(n-1-i, k-1)
    maxSum += nums[-i-1] * nCr(n-1-i,k-1)

print((maxSum - minSum + mod) % mod)

Fixed code:
import math

n,k = list(map(int, input().split(" ")))
nums  = list(map(int, input().split(" ")))
nums.sort()

mod = 10**9 + 7
f = [1]
for i in range(1,10**5+1):
    f.append(f[-1] * i % mod)

def nCr(n,r):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

minSum = 0
maxSum = 0

for i in range(n-k+1):
    minSum += nums[i] * nCr(n-1-i, k-1)
    maxSum += nums[-i-1] * nCr(n-1-i,k-1)

print((maxSum - minSum + mod) % mod)
Buggy code:
import time
MOD =  1000000007
MOD2 = 1000000005

facs = [1 for _ in range(50001)]
inv = [1 for _ in range(50001)]


for i in range(1, 50001):
	facs[i] = (facs[i-1] * i) % MOD

for i in range(1, 50001):
	inv[i] = pow(facs[i], MOD2, MOD)


def get_facs(x):
	return facs[x]


def get_inv(x):
	return inv[x]

def choose(n, k):

	if k==0 or n==k:
		return 1
	if n==0:
		return 0
	if k>n:
		return 0
	r = (get_facs(n)*get_inv(k)) % MOD
	r = (r*get_inv(n-k)) % MOD
	print(n, k, r)
	return r



def main():
	N, K = map(int, input().split())
	numbers = sorted(map(int, input().split()))
	if K==1:
		return 0
	if K==N:
		return numbers[0] - numbers[-1]

	score = 0
	for i, x in enumerate(numbers):
		s = ((choose(i, K-1) - choose(N-1-i, K-1)) * x)
		score = int((score+s))

	return int(score%MOD)
	
	
print(main())


Fixed code:
import time
MOD =  1000000007
MOD2 = 1000000005
MAX = 100001

facs = [1 for _ in range(MAX)]
inv = [1 for _ in range(MAX)]


for i in range(1, MAX):
	facs[i] = (facs[i-1] * i) % MOD

for i in range(1, MAX):
	inv[i] = pow(facs[i], MOD2, MOD)


def get_facs(x):
	return facs[x]


def get_inv(x):
	return inv[x]

def choose(n, k):

	if k==0 or n==k:
		return 1
	if n==0:
		return 0
	if k>n:
		return 0
	r = (get_facs(n)*get_inv(k)) % MOD
	r = (r*get_inv(n-k)) % MOD
	return r



def main():
	N, K = map(int, input().split())
	numbers = sorted(map(int, input().split()))
	if K==1:
		return 0
	if K==N:
		return abs(numbers[0] - numbers[-1])

	score = 0
	for i, x in enumerate(numbers):
		score += ((choose(i, K-1) - choose(N-1-i, K-1)) * x)

	return score%MOD
	
	
print(main())
Buggy code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

def main():
	n,k,*A=map(int,read().split())
	A.sort()
	print(A)
	
	comb=[1]
	
	for i in range(n-k):
		comb.append(((comb[-1]*(k+i))//(i+1))%mod)
	amax=amin=0
	for i in range(n-k+1):
		amax+=(comb[i]*A[k-1+i])%mod
		amin+=(comb[i]*A[n-k-i])%mod
		amax%=mod
		amin%=mod
	ans=(amax-amin)%mod
	print(ans)
	
	
	
	
	





	

if __name__ == "__main__":
	main()


Fixed code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np


def main():
	n,k=map(int,input().split())
	A=list(map(int,input().split()))
	A.sort()
	
	comb=[1]
	for i in range(n-k):
		comb.append((comb[-1]*(k+i)*pow(i+1,mod-2,mod))%mod)
	ans=0
	
	for i in range(n-k+1):
		ans+=((comb[i]*A[k-1+i])-(comb[i]*A[n-k-i]))%mod
		ans=ans%mod
	print(ans)


if __name__ == "__main__":
	main()
Buggy code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
fac = {0: 1, 1: 1}
finv = {0: 1, 1: 1}
inv = {1: 1}
a.sort()
mi = [0] * n
ma = [0] * n
s_mi = 0
s_ma = 0

def comb_init():
    for i in range(2, n + 1):
        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 comb(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod

comb_init()

for i in range(n):
    nn = n - i - 1
    if nn < k - 1:
        break
    else:
        mi[i] = comb(nn, k - 1)
        ma[-(i + 1)] = mi[i] 
    s_mi += mi[i] * a[i]
    s_ma += ma[-(i + 1)] * a[-(i + 1)]    
print(s_ma - s_mi)

Fixed code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
fac = {0: 1, 1: 1}
finv = {0: 1, 1: 1}
inv = {1: 1}
a.sort()
mi = [0] * n
ma = [0] * n
s_mi = 0
s_ma = 0

def comb_init():
    for i in range(2, n + 1):
        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 comb(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % mod) % mod

comb_init()

for i in range(n):
    nn = n - i - 1
    if nn < k - 1:
        break
    else:
        mi[i] = comb(nn, k - 1)
        ma[-(i + 1)] = mi[i] 
    s_mi = (s_mi + mi[i] * a[i]) % mod
    s_ma = (s_ma + ma[-(i + 1)] * a[-(i + 1)]) % mod
print((s_ma - s_mi) % mod)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

inv_t = [0, 1] + [0] * (N - 2)
for i in range(2, N):
    inv_t[i] = inv_t[MOD % i] * (MOD - MOD // i) % MOD

A.sort()
ans = 0
C = 1
for i in range(K, N + 1):
    ans += (A[i - 1] - A[N - i]) * C
    ans %= MOD
    C = C * i * inv_t[i - K + 1]
    C %= MOD
print(ans)


Fixed code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7

inv_t = [0, 1] + [0] * (N - 1)
for i in range(2, N + 1):
    inv_t[i] = inv_t[MOD % i] * (MOD - MOD // i) % MOD

A.sort()
ans = 0
C = 1
for i in range(K, N + 1):
    ans += (A[i - 1] - A[N - i]) * C
    ans %= MOD
    C = C * i * inv_t[i - K + 1]
    C %= MOD
print(ans)
Buggy code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

p = 10**9 + 7
 
fac = [0] * (N + 33)
inv = [0] * (N + 33)
finv = [0] * (N + 33)

fac[0] = fac[1] = 1
inv[1] = 1
finv[0] = finv[1] = 1
 
for i in range(2, N+33):
  fac[i] = fac[i-1] * i % p
  inv[i] = p - inv[p%i] * (p // i) % p
  finv[i] = finv[i-1] * inv[i] % p

def comb(n, k):
  return fac[n] * finv[k] * finv[n-k] % p

ans = 0
ansm = 0

for i in range(K-1, N):
  ans = (ans + A[i] * comb(i, K-1)) % p
  print(i, A[i] * comb(i, K-1))

for i in range(N-K, -1, -1):
  ansm = (ansm + A[i] * comb(N-i-1, K-1)) % p

print((ans-ansm)%p)


Fixed code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()

p = 10**9 + 7
 
fac = [0] * (N + 33)
inv = [0] * (N + 33)
finv = [0] * (N + 33)

fac[0] = fac[1] = 1
inv[1] = 1
finv[0] = finv[1] = 1
 
for i in range(2, N+33):
  fac[i] = fac[i-1] * i % p
  inv[i] = p - inv[p%i] * (p // i) % p
  finv[i] = finv[i-1] * inv[i] % p

def comb(n, k):
  return fac[n] * finv[k] * finv[n-k] % p

ans = 0
ansm = 0

for i in range(K-1, N):
  ans = (ans + A[i] * comb(i, K-1)) % p

for i in range(N-K, -1, -1):
  ansm = (ansm + A[i] * comb(N-i-1, K-1)) % p

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

a=list(map(int,input().split()))
a.sort()
b=a[::-1]
ans=0
for i in range(k-1,n):
    ans+=((a[i]-b[i])*(comb(i,k-1)))%(10**9+7)
print(ans%(10**9+7))

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

a=list(map(int,input().split()))
a.sort()
b=a[::-1]
ans=0
for i in range(k-1,n):
    ans+=((a[i]-b[i])*(combination(i,k-1)))%(10**9+7)
print(ans%(10**9+7))
Buggy code:
import numpy as np
mod=10**9+7
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
A=np.array(A)

def cmb(n, k, mod, fac, ifac): #calc nCk under mod
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod

def make_tables(mod, n): #Make factorial and inverse tables under mod 
    fac = [1, 1] # factorial table
    ifac = [1, 1] #factorial of inverse table
    inverse = [0, 1] #inverse
    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac
  
fac,ifac = make_tables(mod,N+1)
MAX=0
MIN=0
for i,a in enumerate(A):
  MAX+=cmb(i, K-1, mod, fac, ifac)*a
  MAX%=mod
  MIN+=cmb(N-i-1, K-1, mod, fac, ifac)*a
  MIN%=mod
print((MAX-MIN)%mod)

Fixed code:
import numpy as np
mod=10**9+7
N,K=map(int,input().split())
A=list(map(int,input().split()))
A.sort()
A=np.array(A)

def cmb(n, k, mod, fac, ifac): #calc nCk under mod
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % mod

def make_tables(mod, n): #Make factorial and inverse tables under mod 
    fac = [1, 1] # factorial table
    ifac = [1, 1] #factorial of inverse table
    inverse = [0, 1] #inverse
    for i in range(2, n+1):
        fac.append((fac[-1] * i) % mod)
        inverse.append((-inverse[mod % i] * (mod//i)) % mod)
        ifac.append((ifac[-1] * inverse[-1]) % mod)
    return fac, ifac
  
fac,ifac = make_tables(mod,N+1)
MAX=0
MIN=0

for i,a in enumerate(A):
  if i>=K-1:
    MAX+=cmb(i, K-1, mod, fac, ifac)*a
    MAX%=mod
  if N-i-1>=K-1:
    MIN+=cmb(N-i-1, K-1, mod, fac, ifac)*a
    MIN%=mod
print((MAX-MIN)%mod)
Buggy code:
n,k=[int(i) for i in input().split()]
arr=[int(i) for i in input().split()]
ans=0
mod=10**9+7
arr.sort() 
c=[0 for i in range(n)]
r=k-1
c[r]=1
def modinv(x.mod):
    return pow(x,mod-2,mod)
for i in range(r+1,n):
    c[i]=((c[i-1]*(i))*modinv(i-r,mod))%mod
pos=0
neg=0
for i in range(n):
    pos+=(arr[-1-i]*c[-1-i])%mod
    neg+=(arr[i]*c[-1-i])%mod
    pos%=mod
    neg%=mod
print((pos-neg)%mod)


Fixed code:
n,k=[int(i) for i in input().split()]
arr=[int(i) for i in input().split()]
ans=0
mod=10**9+7
arr.sort() 
c=[0 for i in range(n)]
r=k-1
c[r]=1
def modinv(x,mod):
    return pow(x,mod-2,mod)
for i in range(r+1,n):
    c[i]=((c[i-1]*(i))*modinv(i-r,mod))%mod
pos=0
neg=0
for i in range(n):
    pos+=(arr[-1-i]*c[-1-i])%mod
    neg+=(arr[i]*c[-1-i])%mod
    pos%=mod
    neg%=mod
print((pos-neg)%mod)
Buggy code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)

MOD = 10 ** 9 + 7


def pow(a, n):
    if n == 0:
        return 1
    else:
        temp = pow(a, n // 2)
        if n % 2 == 0:
            return temp * temp % MOD
        else:
            return temp * temp * a % MOD
        
        
ans = 0
combinations = list()
for j, i in enumerate(range(k - 1, n)):
    if i == (k - 1):
        combinations.append(1)
    else:
        combinations.append(combinations[-1] * i // pow(i - k + 1, MOD - 2))
    combinations[-1] = combinations[-1] % MOD
    # print(i, 'C', k - 1, '=', combinations[-1])

# print(combinations)
# print(A)
combinations = combinations[::-1]
for j, i in enumerate(range(k - 1, n)):
    ans += (A[-j - 1] * combinations[j] % MOD)

for j, i in enumerate(range(k - 1, n)):
    ans -= (A[j] * combinations[j] % MOD)
    if ans < 0:
        ans = ans + (- ans // MOD + 1) * MOD

ans %= MOD
print(ans)


Fixed code:
n, k = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)

MOD = 10 ** 9 + 7


def pow(a, n):
    if n == 0:
        return 1
    else:
        temp = pow(a, n // 2)
        if n % 2 == 0:
            return temp * temp % MOD
        else:
            return temp * temp * a % MOD
        
        
ans = 0
combinations = list()
for j, i in enumerate(range(k - 1, n)):
    if i == (k - 1):
        combinations.append(1)
    else:
        combinations.append(combinations[-1] * i * pow(i - k + 1, MOD - 2))
    combinations[-1] = combinations[-1] % MOD
    # print(i, 'C', k - 1, '=', combinations[-1])

# print(combinations)
# print(A)
combinations = combinations[::-1]
for j, i in enumerate(range(k - 1, n)):
    ans += (A[-j - 1] * combinations[j] % MOD)

for j, i in enumerate(range(k - 1, n)):
    ans -= (A[j] * combinations[j] % MOD)
    if ans < 0:
        ans = ans + (- ans // MOD + 1) * MOD

ans %= MOD
print(ans)
Buggy code:
def modinv2(a,m):
    return pow(a,m-2,m)

N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
P = 10**9+7

cominv = [0 for i in range(N-1)]
for i in range(N-1):
    if i+1 == K:
        cominv[i] = 1
    elif i == K:
        cominv[i] = (i+1)%P
    elif i > K:
        a = modinv2(i-1,P)
        cominv[i] = (cominv[i-1]*a%P)*(i+1)%P

#print(cominv)
ans = 0

for i in range(N):
    if i < K:
        b = A[i]*cominv[N-i-2]%P
        ans = (ans-b)%P
    if i >= N-K:
        c = A[i]*cominv[i-1]%P
        ans = (ans+c)%P

print(ans)



Fixed code:
def modinv2(a,m):
    return pow(a,m-2,m)

N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
P = 10**9+7

cominv = [0 for i in range(N-1)]
for i in range(N-1):
    if i+2 == K:
        cominv[i] = 1
    elif i+1 == K:
        cominv[i] = (i+1)%P
    elif i+1 > K:
        a = modinv2(i-K+2,P)
        cominv[i] = cominv[i-1]*(i+1)*a%P

#print(cominv)
ans = 0

for i in range(N):
    if i <= N-K:
        b = A[i]*cominv[N-i-2]%P
        ans = (ans-b+P)%P
    if i >= K-1:
        c = A[i]*cominv[i-1]%P
        ans = (ans+c)%P

print(ans)
Buggy 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, k = map(int, input().split())
a = list( map(int, input().split()))

ans = 0


a.sort()

print(a)

for j in range(n):
    i = j +1
    if j <= n-k:
        minX = a[i-1]
        ans -= a[i-1]*cmb(n-i,k-1,mod)%mod
    if k -1 <= i:
        maxX = a[i-1]
        ans += a[i-1]*cmb(i-1,k-1,mod)%mod

print(ans%mod)




Fixed 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**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 )

n, k = map(int, input().split())
a = list( map(int, input().split()))

ans = 0


a.sort()

for j in range(n):
    i = j +1
    if j <= n-k:
        minX = a[i-1]
        ans -= a[i-1]*cmb(n-i,k-1,mod)%mod
    if k -1 <= i:
        maxX = a[i-1]
        ans += a[i-1]*cmb(i-1,k-1,mod)%mod

print(ans%mod)
Buggy code:
MOD = 1000000007
MAX = 200000    # max n

fac = [0 for _ in range(MAX)]    # = n! (mod MOD)
finv = [0 for _ in range(MAX)]    # = 1/r! (mod MOD)
inv = [0 for _ in range(MAX)]    # = 1/r (mod MOD)


# init
def COMinit():
    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


# nCk, n < [0, MAX), k < [0, n]
def COM(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD


n, k = map(int, input().split())
A = [int(a) for a in input().split()]
A.sort()
ans = 0
for i in range(n):
  if n-(i+1) < k-1:
    break
  ans += ((A[n-i-1]-A[i])*COM(n-i-1, k-1)) % MOD
  ans %= MOD
print(ans)

Fixed code:
MOD = 1000000007
MAX = 200000    # max n

fac = [0 for _ in range(MAX)]    # = n! (mod MOD)
finv = [0 for _ in range(MAX)]    # = 1/r! (mod MOD)
inv = [0 for _ in range(MAX)]    # = 1/r (mod MOD)


# init
def COMinit():
    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


# nCk, n < [0, MAX), k < [0, n]
def COM(n, k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD


COMinit()
n, k = map(int, input().split())
A = [int(a) for a in input().split()]
A.sort()
ans = 0
for i in range(n):
    if n - (i + 1) < k - 1:
        break
    ans += ((A[n - i - 1] - A[i]) * COM(n - i - 1, k - 1)) % MOD
    ans %= MOD
print(ans)