Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
A = list(map(int, input().split()))

A = sorted(A)

def cmb(m, n):
    if m - n < n:
        n = m - n
    if n == 0:
        return 1
    if n == 1:
        return m
    
    numerator = [m - n + k + 1 for k in range(n)]
    denominator = [k + 1 for k in range(n)]

    for p in range(2, n+1):
        pivot = denominator[p-1]
        if pivot > 1:
            offset = (m-n) % p
            for k in range(p-1, n, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    
    result = 1
    for k in range(n):
        if numerator[k] > 1:
            result *= int(numerator[k])
            
            # mod(10**9+7)
            # result = result % (10**9+7)

    return result

ans = 0
CMB = [[] for _ in range(n)]
for i in range(n):
    if i <= k:
        for j in range(i, k):
            CMB[i].append(cmb(i, j))

for i in range(n):
    if n-i-1 < k-1:
        pass
    else:
        ans -= A[i] * CMB[n-i-1][k-1]
    if i < k-1:
        pass
    else:
        ans += A[i] * cmb[i][k-1]

ans = ans % (10**9 + 7)
print(ans)

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

mod = 10**9 + 7

# factorial
factorial = [1,1]
# factorial inverse
factorial_inverse = [1,1]
# inverse for calculation
inverse = [0,1]

for i in range(2, n+1):
    factorial.append((factorial[-1]*i) % mod)
    inverse.append((- inverse[mod % i] * (mod//i)) % mod)
    factorial_inverse.append(factorial_inverse[-1] * inverse[-1] % mod)

def Binominal_theorem(n, k, mod):
    if n < k:
        return 0
    if (n < 0) or (k < 0):
        return 0
    return factorial[n] * factorial_inverse[k] * factorial_inverse[n-k] % mod

ans_min = 0
for i in range(n-k+1):
    ans_min += (A[i] * Binominal_theorem(n-i-1, k-1, mod)) % mod

A = A[::-1]
ans_max = 0
for i in range(n-k+1):
    ans_max += (A[i] * Binominal_theorem(n-i-1, k-1, mod)) % mod

ans = (ans_max - ans_min) % mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
A = list(map(int, input().split()))

A = sorted(A)

def cmb(m, n):
    if m - n < n:
        n = m - n
    if n == 0:
        return 1
    if n == 1:
        return m
    
    numerator = [m - n + k + 1 for k in range(n)]
    denominator = [k + 1 for k in range(n)]

    for p in range(2, n+1):
        pivot = denominator[p-1]
        if pivot > 1:
            offset = (m-n) % p
            for k in range(p-1, n, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    
    result = 1
    for k in range(n):
        if numerator[k] > 1:
            result *= int(numerator[k])
            
            # mod(10**9+7)
            # result = result % (10**9+7)

    return result

ans = 0
for i in range(n):
    if n-i-1 < k-1:
        pass
    else:
        ans -= A[i] * cmb(n-i-1, k-1)
    if i < k-1:
        pass
    else:
        ans += A[i] * cmb(i, k-1)

ans = ans % (10**9 + 7)
print(ans)

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

mod = 10**9 + 7

# factorial
factorial = [1,1]
# factorial inverse
factorial_inverse = [1,1]
# inverse for calculation
inverse = [0,1]

for i in range(2, n+1):
    factorial.append((factorial[-1]*i) % mod)
    inverse.append((- inverse[mod % i] * (mod//i)) % mod)
    factorial_inverse.append(factorial_inverse[-1] * inverse[-1] % mod)

def Binominal_theorem(n, k, mod):
    if n < k:
        return 0
    if (n < 0) or (k < 0):
        return 0
    return factorial[n] * factorial_inverse[k] * factorial_inverse[n-k] % mod

ans_min = 0
for i in range(n-k+1):
    ans_min += (A[i] * Binominal_theorem(n-i-1, k-1, mod)) % mod

A = A[::-1]
ans_max = 0
for i in range(n-k+1):
    ans_max += (A[i] * Binominal_theorem(n-i-1, k-1, mod)) % mod

ans = (ans_max - ans_min) % mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
A = list(map(int, input().split()))

A = sorted(A)

def cmb(m, n):
    if m - n < n:
        n = m - n
    if n == 0:
        return 1
    if n == 1:
        return m
    
    numerator = [m - n + k + 1 for k in range(n)]
    denominator = [k + 1 for k in range(n)]

    for p in range(2, n+1):
        pivot = denominator[p-1]
        if pivot > 1:
            offset = (m-n) % p
            for k in range(p-1, n, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    
    result = 1
    for k in range(n):
        if numerator[k] > 1:
            result *= int(numerator[k])
            
            # mod(10**9+7)
            # result = result % (10**9+7)

    return result

ans = 0
for i in range(n):
    if n-i-1 < k-1:
        pass
    else:
        ans -= A[i] * cmb(n-i-1, k-1)
    if i < k-1:
        pass
    else:
        ans += A[i] * cmb(i, k-1)

ans = ans % (10**9 + 7)
print(ans)

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

mod = 10**9 + 7

# factorial
factorial = [1,1]
# factorial inverse
factorial_inverse = [1,1]
# inverse for calculation
inverse = [0,1]

for i in range(2, n+1):
    factorial.append((factorial[-1]*i) % mod)
    inverse.append((- inverse[mod % i] * (mod//i)) % mod)
    factorial_inverse.append(factorial_inverse[-1] * inverse[-1] % mod)

def Binominal_theorem(n, k, mod):
    if n < k:
        return 0
    if (n < 0) or (k < 0):
        return 0
    return factorial[n] * factorial_inverse[k] * factorial_inverse[n-k] % mod

ans_min = 0
for i in range(n-k+1):
    ans_min += (A[i] * Binominal_theorem(n-i-1, k-1, mod)) % mod

A = A[::-1]
ans_max = 0
for i in range(n-k+1):
    ans_max += (A[i] * Binominal_theorem(n-i-1, k-1, mod)) % mod

ans = (ans_max - ans_min) % mod
print(ans)