Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate

mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = sorted([int(x) for x in input().split()])

table = [[1] * K]
for _ in range(N - K):
    tmp = accumulate(table[-1])
    table.append([x % mod for x in tmp])

comb = [table[i][K - 1] for i in range(N - K + 1)]
ans = 0
for i in range(N - K + 1):
    ans += comb[i] * (A[i + K - 1] - A[N - i - K])
    ans %= mod
print(ans)


Fixed submission:
from itertools import accumulate

mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = sorted([int(x) for x in input().split()])

f = [1 for _ in range(N + 1)]
inv = [1 for _ in range(N + 1)]
finv = [1 for _ in range(N + 1)]

for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod
comb = [f[n] * (finv[K - 1] * finv[n - (K - 1)] % mod) % mod for n in range(K - 1, N)]

ans = 0
for i in range(N - K + 1):
    ans += comb[i] * (A[i + K - 1] - A[N - i - K])
    ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate

mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = sorted([int(x) for x in input().split()])

table = [tuple([1] * K)]
for _ in range(N - K):
    tmp = accumulate(table[-1])
    table.append(tuple(x % mod for x in tmp))

comb = [table[i][K - 1] for i in range(N - K + 1)]
ans = 0
for i in range(N - K + 1):
    ans += comb[i] * (A[i + K - 1] - A[N - i - K])
    ans %= mod
print(ans)


Fixed submission:
from itertools import accumulate

mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = sorted([int(x) for x in input().split()])

f = [1 for _ in range(N + 1)]
inv = [1 for _ in range(N + 1)]
finv = [1 for _ in range(N + 1)]

for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod
comb = [f[n] * (finv[K - 1] * finv[n - (K - 1)] % mod) % mod for n in range(K - 1, N)]

ans = 0
for i in range(N - K + 1):
    ans += comb[i] * (A[i + K - 1] - A[N - i - K])
    ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import accumulate

mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = sorted([int(x) for x in input().split()])

table = [tuple([1] * K)]
for _ in range(N - K):
    tmp = accumulate(table[-1])
    table.append(tuple(x % mod for x in tmp))

comb = [table[i][K - 1] for i in range(N - K + 1)]
ans = 0
for i in range(N - K + 1):
    ans += comb[i] * (A[i + K - 1] - A[N - i - K])
    ans %= mod
print(ans)


Fixed submission:
from itertools import accumulate

mod = 10 ** 9 + 7

N, K = map(int, input().split())
A = sorted([int(x) for x in input().split()])

f = [1 for _ in range(N + 1)]
inv = [1 for _ in range(N + 1)]
finv = [1 for _ in range(N + 1)]

for i in range(2, N + 1):
    f[i] = f[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod
comb = [f[n] * (finv[K - 1] * finv[n - (K - 1)] % mod) % mod for n in range(K - 1, N)]

ans = 0
for i in range(N - K + 1):
    ans += comb[i] * (A[i + K - 1] - A[N - i - K])
    ans %= mod
print(ans)