Buggy python submission (verdict: Time Limit Exceeded):
import math

def combinations_count(n, r):
    a = 1
    b = 1
    if (n != 0) & (n >= r):
        for i in range(n, n - r, -1):
            a *= i
            b *= i - n + r
        return a // b
    else:
        return 0

n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
mod = 10 ** 9 + 7
ans = 0
for index, value in enumerate(a):
    ans += (combinations_count(index, k - 1) - combinations_count(n - index - 1, k - 1)) * value % mod
print(ans % mod)

Fixed submission:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
mod = 10 ** 9 + 7
ans = 0

f = [1] * (n + 1)
inv = [1] * (n + 1)
for i in range(1, n + 1):
    f[i] = (f[i - 1] * i) % (mod)
inv[n] = pow(f[n], mod - 2, mod)
for i in range(n - 1, 0, -1):
    inv[i] = inv[i + 1] * (i + 1) % mod

def combination(n, r):
    if (n != 0) & (n >= r):
        return f[n] * inv[r] * inv[n - r] % mod
    return 0    
    
ans = 0
for index, value in enumerate(a):
    ans += (combination(index, k - 1) - combination(n - index - 1, k - 1)) * value
print(ans % mod)

Buggy python submission (verdict: Time Limit Exceeded):
import math

def combinations_count(n, r):
    if (n != 0) & (n >= r):
        return math.factorial(n) * pow((math.factorial(n - r) * math.factorial(r)), (mod - 2))
    else:
        return 0

n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
mod = 10 ** 9 + 7
ans = 0
for index, value in enumerate(a):
    ans += (combinations_count(index, k - 1) - combinations_count(n - index - 1, k - 1)) * value % mod
print(ans % mod)

Fixed submission:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
mod = 10 ** 9 + 7
ans = 0

f = [1] * (n + 1)
inv = [1] * (n + 1)
for i in range(1, n + 1):
    f[i] = (f[i - 1] * i) % (mod)
inv[n] = pow(f[n], mod - 2, mod)
for i in range(n - 1, 0, -1):
    inv[i] = inv[i + 1] * (i + 1) % mod

def combination(n, r):
    if (n != 0) & (n >= r):
        return f[n] * inv[r] * inv[n - r] % mod
    return 0    
    
ans = 0
for index, value in enumerate(a):
    ans += (combination(index, k - 1) - combination(n - index - 1, k - 1)) * value
print(ans % mod)

Buggy python submission (verdict: Time Limit Exceeded):
import math

def combinations_count(n, r):
    if (n != 0) & (n >= r):
        return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
    else:
        return 0

n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
mod = 10 ** 9 + 7
ans = 0
for index, value in enumerate(a):
    ans += (combinations_count(index, k - 1) - combinations_count(n - index - 1, k - 1)) * value
print(ans % mod)

Fixed submission:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
mod = 10 ** 9 + 7
ans = 0

f = [1] * (n + 1)
inv = [1] * (n + 1)
for i in range(1, n + 1):
    f[i] = (f[i - 1] * i) % (mod)
inv[n] = pow(f[n], mod - 2, mod)
for i in range(n - 1, 0, -1):
    inv[i] = inv[i + 1] * (i + 1) % mod

def combination(n, r):
    if (n != 0) & (n >= r):
        return f[n] * inv[r] * inv[n - r] % mod
    return 0    
    
ans = 0
for index, value in enumerate(a):
    ans += (combination(index, k - 1) - combination(n - index - 1, k - 1)) * value
print(ans % mod)