Buggy python submission (verdict: Runtime Error):
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 submission:
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 python submission (verdict: Runtime Error):
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 submission:
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 python submission (verdict: Time Limit Exceeded):
def modinv(a, mod):
    b = mod
    u = 1
    v = 0
    while(b):
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    
    u %= mod
    return u


def modcmb(n, r, mod):
    X = 1
    for i in range(n-r+1, n+1):
        X = X * i % mod
    
    Y = 1
    for i in range(1, r+1):
        Y = Y * i % mod
        
    res = X * modinv(Y, mod) % mod
    return res


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

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

print(ans)

Fixed submission:
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 python submission (verdict: Time Limit Exceeded):
def modinv(a, mod):
    b = mod
    u = 1
    v = 0
    while(b):
        t = a // b
        a -= t * b
        a, b = b, a
        u -= t * v
        u, v = v, u
    
    u %= mod
    return u


def modcmb(n, r, mod):
    X = 1
    for i in range(n-r+1, n+1):
        X = X * i % mod
    
    Y = 1
    for i in range(1, r+1):
        Y = Y * i % mod
        
    res = X * modinv(Y, mod) % mod
    return res


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

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

print(ans)

Fixed submission:
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)