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

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort()
b = 1
ans = 0
for i in range(k - 1, n):
    ans += b * (a[i] - a[n - i - 1])
    ans %= mod
    b = (i + 1) * b
    c = i + 1 - (k - 1)
    b = pow(c, mod - 2, mod) * b % mod
print(ans)

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

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort()
b = 1
ans = 0
for i in range(k - 1, n):
    ans += b * (a[i] - a[n - i - 1])
    ans %= mod
    b = (i + 1) * b
    c = i + 1 - (k - 1)
    b = pow(c, mod - 2, mod) * b % mod
print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort()
b = 1
ans = 0
for i in range(k - 1, n):
    ans += b * a[i]
    ans -= b * a[n - i - 1]
    ans %= mod
    b *= (i + 1) / (i + 1 - (k - 1))
    b = int(b)
print(ans)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort()
b = 1
ans = 0
for i in range(k - 1, n):
    ans += b * (a[i] - a[n - i - 1])
    ans %= mod
    b = (i + 1) * b
    c = i + 1 - (k - 1)
    b = pow(c, mod - 2, mod) * b % mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def comb(n, r, p):
    x, y = 1, 1
    for i in range(n, n - r, -1):
        x *= i
        y *= i + r - n
        x %= p
        y %= p
    return pow(y, p - 2, p) * x % p

n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
p = 10 ** 9 + 7
ans = 0
for i in range(n - k + 1):
    ans += (a[n - i - 1] - a[i]) * comb(n - 1 - i, k - 1, p)
    ans %= p
print(ans)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort()
b = 1
ans = 0
for i in range(k - 1, n):
    ans += b * (a[i] - a[n - i - 1])
    ans %= mod
    b = (i + 1) * b
    c = i + 1 - (k - 1)
    b = pow(c, mod - 2, mod) * b % mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def comb(n, r, p):
    x, y = 1, 1
    for i in range(n, n - r, -1):
        x *= i
        y *= i + r - n
        x %= p
        y %= p
    return pow(y, p - 2, p) * x % p

n, k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
p = 10 ** 9 + 7
ans = 0
for i in range(n - k + 1):
    ans += (a[n - i - 1] - a[i]) * comb(n - 1 - i, k - 1, p)
    ans %= p
print(ans)

Fixed submission:
n, k = map(int, input().split())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort()
b = 1
ans = 0
for i in range(k - 1, n):
    ans += b * (a[i] - a[n - i - 1])
    ans %= mod
    b = (i + 1) * b
    c = i + 1 - (k - 1)
    b = pow(c, mod - 2, mod) * b % mod
print(ans)