Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

f = [1, 1]
for i in range(2, N+1):
    f.append(f[i-1] * i)

fk = pow(f[K-1], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1
    q = K - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)


Fixed submission:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

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

q = K - 1
fk = pow(f[q], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7

A.sort()

ans = A[K-1] - A[-K]

tmp = 1

a = 1

for i, Ak in enumerate(A[K:]):
    a = a * (tmp+K-1) // tmp
    ans += a * (Ak - A[N-(K+i)-1])
    ans %= MOD
    tmp += 1

print(int(ans))


Fixed submission:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

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

q = K - 1
fk = pow(f[q], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7

A.sort()

ans = A[K-1] - A[-K]

tmp = 1

a = 1

for i, Ak in enumerate(A[K:]):
    a = a * (tmp+K-1) // tmp
    ans += a * (Ak - A[N-(K+i)-1])
    if ans > MOD:
        ans %= MOD
    tmp += 1

print(int(ans))


Fixed submission:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

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

q = K - 1
fk = pow(f[q], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)

Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7

A.sort()

ans = A[K-1] - A[-K]

tmp2 = 1
tmp3 = K

a = 1

for k in range(K, N):
    a = a * tmp3 / tmp2
    ans += a * (A[k] - A[N-k-1])
    if ans > MOD:
        ans %= MOD
    tmp2 += 1
    tmp3 += 1

print(int(ans))


Fixed submission:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

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

q = K - 1
fk = pow(f[q], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7

A.sort()

ans = A[K-1] - A[-K]

tmp2 = 1
tmp3 = K

a = 1

for k in range(K, N):
    a = a * tmp3 // tmp2
    ans += a * (A[k] - A[N-k-1])
    if ans > MOD:
        ans %= MOD
    tmp2 += 1
    tmp3 += 1

print(int(ans))


Fixed submission:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

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

q = K - 1
fk = pow(f[q], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))

MOD = 10 ** 9 + 7

A.sort()

ans = A[K-1] - A[-K]

tmp2 = 1
tmp3 = K

a = 1

for k in range(K, N):
    a = a * tmp3 // tmp2
    ans += a * (A[k] - A[N-k-1])
    if ans > MOD:
        ans %= MOD
    tmp2 += 1
    tmp3 += 1

print(int(ans))


Fixed submission:
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

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

q = K - 1
fk = pow(f[q], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)