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

def mod_inverse(a, p):
    return pow(a, p - 2, p)

def combination(n, k, p):
    a = 1
    for i in range(n, n - k, -1):
        a *= i
        a %= p
    b = 1
    for i in range(k):
        b *= (i + 1)
        b %= p
    return (a * mod_inverse(b, p)) % p

a.sort()
max_x = 0
min_x = 0
for i in range(n - k + 1):
    temp = combination(k - 1 + i, k - 1, mod)
    max_x += (temp * a[k - 1 + i])
    min_x += (temp * a[n - k - i])
    max_x %= mod
    min_x %= mod
print((max_x - min_x) % mod)

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

fact = [1] * (n + 1)
revfact = [1] * (n + 1)
for i in range(n):
    fact[i + 1] = ((i + 1) * fact[i]) % mod
revfact[n] = pow(fact[n], mod - 2, mod)
for i in reversed(range(n)):
    revfact[i] = ((i + 1) * revfact[i + 1]) % mod
def combination(n, k, p):
    return (fact[n] * revfact[k] * revfact[n - k]) % p

a.sort()
max_x = 0
min_x = 0
for i in range(n - k + 1):
    temp = combination(k - 1 + i, k - 1, mod)
    max_x += (temp * a[k - 1 + i])
    min_x += (temp * a[n - k - i])
    max_x %= mod
    min_x %= mod
print((max_x - min_x) % mod)

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

def mod_inverse(a, p):
    mod = 1
    p_bin = bin(p-2)
    for i in range(len(p_bin) - 1, 1, -1):
        if i == len(p_bin) - 1:
            mul = a
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
        else:
            mul = mul ** 2
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
    return mod

def combination(n, k, p):
    a = 1
    for i in range(n, n - k, -1):
        a *= i
        a %= p
    b = 1
    for i in range(k):
        b *= (i + 1)
        b %= p
    return (a * mod_inverse(b, p)) % p

a.sort()
max_x = 0
min_x = 0
for i in range(n - k + 1):
    temp = combination(k - 1 + i, k - 1, mod)
    max_x += (temp * a[k - 1 + i])
    min_x += (temp * a[n - k - i])
    max_x %= mod
    min_x %= mod
print((max_x - min_x) % mod)

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

fact = [1] * (n + 1)
revfact = [1] * (n + 1)
for i in range(n):
    fact[i + 1] = ((i + 1) * fact[i]) % mod
revfact[n] = pow(fact[n], mod - 2, mod)
for i in reversed(range(n)):
    revfact[i] = ((i + 1) * revfact[i + 1]) % mod
def combination(n, k, p):
    return (fact[n] * revfact[k] * revfact[n - k]) % p

a.sort()
max_x = 0
min_x = 0
for i in range(n - k + 1):
    temp = combination(k - 1 + i, k - 1, mod)
    max_x += (temp * a[k - 1 + i])
    min_x += (temp * a[n - k - i])
    max_x %= mod
    min_x %= mod
print((max_x - min_x) % mod)

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

def mod_inverse(a, p):
    mod = 1
    p_bin = bin(p-2)
    for i in range(len(p_bin) - 1, 1, -1):
        if i == len(p_bin) - 1:
            mul = a
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
        else:
            mul = mul ** 2
            mul %= p
            mod *= (mul ** int(p_bin[i]))
            mod %= p
    return mod

def combination(n, k, p):
    a = 1
    for i in range(n, n - k, -1):
        a *= i
        a %= p
    b = 1
    for i in range(k):
        b *= (i + 1)
        b %= p
    return (a * mod_inverse(b, p)) % p

a.sort()
max_x = 0
min_x = 0
for i in range(n - k + 1):
    temp = combination(k - 1 + i, k - 1, mod)
    max_x += (temp * a[k - 1 + i])
    min_x += (temp * a[n - k - i])
    max_x %= mod
    min_x %= mod
print((max_x - min_x) % mod)

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

fact = [1] * (n + 1)
revfact = [1] * (n + 1)
for i in range(n):
    fact[i + 1] = ((i + 1) * fact[i]) % mod
revfact[n] = pow(fact[n], mod - 2, mod)
for i in reversed(range(n)):
    revfact[i] = ((i + 1) * revfact[i + 1]) % mod
def combination(n, k, p):
    return (fact[n] * revfact[k] * revfact[n - k]) % p

a.sort()
max_x = 0
min_x = 0
for i in range(n - k + 1):
    temp = combination(k - 1 + i, k - 1, mod)
    max_x += (temp * a[k - 1 + i])
    min_x += (temp * a[n - k - i])
    max_x %= mod
    min_x %= mod
print((max_x - min_x) % mod)