Buggy python submission (verdict: Time Limit Exceeded):
# from scipy.special import comb
from scipy.misc import comb
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
comb_tmp = []
for i in range(k - 1, n):
    comb_tmp.append(comb(i, k - 1, exact=True))
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * comb_tmp[i + 1 - k]
min_a = 0
for i in range(n - k + 1):
    min_a += a[i] * comb_tmp[n - k - i]
print(int((max_a - min_a) % 1000000007))

Fixed submission:
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i % 1000000007)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
a = a[::-1]
min_a = 0
for i in range(k - 1, n):
    min_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
print(int((max_a - min_a) % 1000000007))

Buggy python submission (verdict: Time Limit Exceeded):
# from scipy.special import comb
from scipy.misc import comb
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * comb(i, k - 1, exact=True)
min_a = 0
for i in range(n - k + 1):
    min_a += a[i] * comb(n - i - 1, k - 1, exact=True)
print(int((max_a - min_a) % 1000000007))

Fixed submission:
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i % 1000000007)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
a = a[::-1]
min_a = 0
for i in range(k - 1, n):
    min_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
print(int((max_a - min_a) % 1000000007))

Buggy python submission (verdict: Runtime Error):
# from scipy.special import comb
from scipy.misc import comb
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * comb(i, k - 1)
min_a = 0
for i in range(n - k + 1):
    min_a += a[i] * comb(n - i - 1, k - 1)
print(int((max_a - min_a) % 1000000007))

Fixed submission:
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i % 1000000007)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
a = a[::-1]
min_a = 0
for i in range(k - 1, n):
    min_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
print(int((max_a - min_a) % 1000000007))

Buggy python submission (verdict: Runtime Error):
# from scipy.special import comb
from scipy.misc import comb
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
print(a)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * comb(i, k - 1)
min_a = 0
for i in range(n - k + 1):
    min_a += a[i] * comb(n - i - 1, k - 1)
print(int((max_a - min_a) % 1000000007))

Fixed submission:
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i % 1000000007)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
a = a[::-1]
min_a = 0
for i in range(k - 1, n):
    min_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
print(int((max_a - min_a) % 1000000007))

Buggy python submission (verdict: Runtime Error):
from scipy.special import comb
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
print(a)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * comb(i, k - 1)
min_a = 0
for i in range(n - k + 1):
    min_a += a[i] * comb(n - i - 1, k - 1)
print(int((max_a - min_a) % 1000000007))

Fixed submission:
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i % 1000000007)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
a = a[::-1]
min_a = 0
for i in range(k - 1, n):
    min_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
print(int((max_a - min_a) % 1000000007))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
com = list(combinations(a, k))
ans = 0
for s in com:
    ans += max(s) - min(s)
print(ans % 1000000007)

Fixed submission:
n, k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
a = sorted(a)
fact = [1]
for i in range(1, n + 1):
    fact.append(fact[-1] * i % 1000000007)
max_a = 0
for i in range(k - 1, n):
    max_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
a = a[::-1]
min_a = 0
for i in range(k - 1, n):
    min_a += a[i] * fact[i] * pow(fact[i - k + 1], 1000000005, 1000000007) * pow(fact[k - 1], 1000000005, 1000000007) % 1000000007
print(int((max_a - min_a) % 1000000007))