Buggy python submission (verdict: Time Limit Exceeded):
import math
n, k = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7


A_asc = sorted(A)
A_desc = sorted(A, reverse=True)

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

MAX = 0
MIN = 0
i = 0
r = nCr(n, k)
while r > 0:
    t = nCr(n - 1 - i, k - 1)
    if r > t:
        MAX += (A_desc[i] * t) % mod
        MIN += (A_asc[i] * t) % mod
        r -= t
    else:
        MAX += (A_desc[i] * r) % mod
        MIN += (A_asc[i] * r) % mod
        r -= t
    i += 1

print((MAX - MIN + mod) % mod)

Fixed submission:
import math
n, k = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7


def com_init():
    MAX = 10 ** 5 + 1
    fac = [0] * MAX
    finv = [0] * MAX
    inv = [0] * MAX
    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
    return fac, finv

fac, finv = com_init()

def nCr(n,r):
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod 

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


MAX_SUM = 0
MIN_SUM = 0

A.sort() # asc
for j in range(n - k + 1): # 小さい方からk個
    MIN_SUM += A[j] * nCr(n - j - 1, k - 1)

A = A[::-1] # desc
for i in range(n - k + 1): #大きい方からk個
    MAX_SUM += A[i] * nCr(n - i - 1, k - 1)


print((MAX_SUM - MIN_SUM + mod) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
import math
n, k = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7


A_asc = sorted(A)
A_desc = sorted(A, reverse=True)

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

MAX = 0
i = 0
r = nCr(n, k)
while r > 0:
    t = nCr(n - 1 - i, k - 1)
    if r > t:
        MAX += (A_desc[i] * t) % mod
        r -= t
    else:
        MAX += (A_desc[i] * r) % mod
        r -= t
    i += 1

MIN = 0
i = 0
r = nCr(n, k)
while r > 0:
    t = nCr(n - 1 - i, k - 1)
    if r > t:
        MIN += (A_asc[i] * t) % mod
        r -= t
    else:
        MIN += (A_asc[i] * r) % mod
        r -= t
    i += 1

print((MAX - MIN + mod) % mod)

Fixed submission:
import math
n, k = map(int, input().split())
A = list(map(int, input().split()))
mod = 10 ** 9 + 7


def com_init():
    MAX = 10 ** 5 + 1
    fac = [0] * MAX
    finv = [0] * MAX
    inv = [0] * MAX
    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
    return fac, finv

fac, finv = com_init()

def nCr(n,r):
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod 

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


MAX_SUM = 0
MIN_SUM = 0

A.sort() # asc
for j in range(n - k + 1): # 小さい方からk個
    MIN_SUM += A[j] * nCr(n - j - 1, k - 1)

A = A[::-1] # desc
for i in range(n - k + 1): #大きい方からk個
    MAX_SUM += A[i] * nCr(n - i - 1, k - 1)


print((MAX_SUM - MIN_SUM + mod) % mod)