Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0
cmb = [0]*N
cmb[K-1] = 1
for i in range(K, N):
    cmb[i] = cmb[i-1]*i//(i - K + 1)
for i in range(K, N):
    cmb[i] = cmb[i]%(10**9+7)
    
for i in range(N):
    fac = cmb[i] - cmb[N-i-1]
    num = (fac*a[i])%(10**9+7)
    ans += num
    ans = ans%(10**9+7)
print(ans)

Fixed submission:
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
mod = 10**9+7
ans = 0
cmb = [0]*N
x = [0]*N
cmb[K-1] = 1
def Pow(a, n):
    if n == 0: return 1
    if n%2 == 0:
        return (Pow(a, n//2))**2%mod
    else:
        return Pow(a, n-1)*a%mod
for i in range(1,N-K+1):
    x[i] = Pow(i, mod-2)
    
for i in range(K, N):
    cmb[i] = cmb[i-1]*i%mod*x[i-K+1]%mod
for i in range(N):
    fac = cmb[i] - cmb[N-i-1]
    num = fac*a[i]%mod
    ans += num
    ans = ans%(10**9+7)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0
cmb = [0]*N
cmb[K-1] = 1
for i in range(K, N):
    cmb[i] = cmb[i-1]*i//(i - K + 1)

for i in range(N):
    fac = (cmb[i] - cmb[N-i-1])%(10**9+7)
    num = (fac*a[i])%(10**9+7)
    ans += num
    ans = ans%(10**9+7)
print(ans)

Fixed submission:
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
mod = 10**9+7
ans = 0
cmb = [0]*N
x = [0]*N
cmb[K-1] = 1
def Pow(a, n):
    if n == 0: return 1
    if n%2 == 0:
        return (Pow(a, n//2))**2%mod
    else:
        return Pow(a, n-1)*a%mod
for i in range(1,N-K+1):
    x[i] = Pow(i, mod-2)
    
for i in range(K, N):
    cmb[i] = cmb[i-1]*i%mod*x[i-K+1]%mod
for i in range(N):
    fac = cmb[i] - cmb[N-i-1]
    num = fac*a[i]%mod
    ans += num
    ans = ans%(10**9+7)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0
cmb = [0]*N
cmb[K-1] = 1
for i in range(K, N):
    cmb[i] = cmb[i-1]*i//(i - K + 1)

for i in range(N):
    fac = (cmb[i] - cmb[N-i-1])%(10**9+7)
    ans += fac*a[i]
    ans = ans%(10**9+7)
print(ans)

Fixed submission:
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
mod = 10**9+7
ans = 0
cmb = [0]*N
x = [0]*N
cmb[K-1] = 1
def Pow(a, n):
    if n == 0: return 1
    if n%2 == 0:
        return (Pow(a, n//2))**2%mod
    else:
        return Pow(a, n-1)*a%mod
for i in range(1,N-K+1):
    x[i] = Pow(i, mod-2)
    
for i in range(K, N):
    cmb[i] = cmb[i-1]*i%mod*x[i-K+1]%mod
for i in range(N):
    fac = cmb[i] - cmb[N-i-1]
    num = fac*a[i]%mod
    ans += num
    ans = ans%(10**9+7)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0

def cmb(n, r):
    p = 10 ** 9 + 7
    M = 10 ** 6  
    fact = [1, 1]  
    factinv = [1, 1]
    inv = [0, 1]  
    for i in range(2, M + 1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

for i in range(N):
    fac = cmb(i, K-1) - cmb(N-i-1, K - 1)
    ans += fac*a[i]
print(ans%(10**9+7))

Fixed submission:
N, K = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
mod = 10**9+7
ans = 0
cmb = [0]*N
x = [0]*N
cmb[K-1] = 1
def Pow(a, n):
    if n == 0: return 1
    if n%2 == 0:
        return (Pow(a, n//2))**2%mod
    else:
        return Pow(a, n-1)*a%mod
for i in range(1,N-K+1):
    x[i] = Pow(i, mod-2)
    
for i in range(K, N):
    cmb[i] = cmb[i-1]*i%mod*x[i-K+1]%mod
for i in range(N):
    fac = cmb[i] - cmb[N-i-1]
    num = fac*a[i]%mod
    ans += num
    ans = ans%(10**9+7)
print(ans)