Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

'''
fact = [1]
i = 1
while i < n:
    fact.append(fact[i-1] * i)
    i += 1 

def comb_fac(n, k):
    return fact[n] // (fact[n-k] * fact[k])
'''
def comb(n, k):
    n1 = n+1
    r = min(k, n-k)
    numer = denom = 1
    i = 1
    while i < r+1:
        numer = numer * (n1-i) % mod
        denom = denom * i % mod
        i += 1
    
    #memo[n][k] = numer * pow(denom, mod - 2, mod) % mod
    #return numer // denom
    return numer * pow(denom, mod - 2, mod) % mod


amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
mod = 10**9 + 7

fact = [1]
i = 1
while i < n:
    fact.append(fact[i-1] * i)
    i += 1 

def comb_fac(n, k):
    return fact[n] // (fact[n-k] * fact[k])

def comb(n, k):
    n1 = n+1
    r = min(k, n-k)
    numer = denom = 1
    i = 1
    while i < r+1:
        numer = numer * (n1-i) % mod
        denom = denom * i % mod
        i += 1
    
    #memo[n][k] = numer // denom
    #memo[n][k] = numer * pow(denom, mod - 2, mod) % mod
    return numer // denom


amax = 0
i = n-1
while i >= k-1:
    amax += (alist[i] * comb_fac(i, k-1))%mod
    i -= 1

amin = 0
i = 0
while i <= n-k:
    amin += (alist[i] * comb_fac(n-i-1, k-1))%mod
    i += 1
print((amax - amin) % mod)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
mod = 10**9 + 7

fact = [1]
i = 1
while i < n:
    fact.append(fact[i-1] * i)
    i += 1 

def comb_fac(n, k):
    return fact[n] // (fact[n-k] * fact[k])

amax = 0
i = n-1
while i >= k-1:
    amax += alist[i] * comb_fac(i, k-1)
    i -= 1

amin = 0
i = 0
while i <= n-k:
    amin += alist[i] * comb_fac(n-i-1, k-1)
    i += 1
print((amax - amin) % mod)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

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

n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
mod = 10**9 + 7
memo = [[None]*k for _ in range(n)]

def comb(n, k, memo):
    if memo[n][k]:
        return memo[n][k]
    n1 = n+1
    r = min(k, n-k)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n1-i)%mod
        denom = denom * i % mod
    
    memo[n][k] = numer * pow(denom, mod - 2, mod) % mod
    return memo[n][k]

    #return math.factorial(n) // (math.factorial(n-k) * math.factorial(k))

amax = 0
i = n-1
while i >= k-1:
    amax += alist[i] * comb(i, k-1, memo)
    i -= 1

amin = 0
i = 0
while i <= n-k:
    amin += alist[i] * comb(n-i-1, k-1, memo)
    i += 1
print((amax - amin) % mod)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

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

n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
mod = 10**9 + 7

def comb(n, k):
    n1 = n+1
    r = min(k, n-k)
    numer = denom = 1
    for i in range(1, r+1):
        numer = numer * (n1-i)%mod
        denom = denom * i % mod
    return numer * pow(denom, mod - 2, mod) % mod

    #return math.factorial(n) // (math.factorial(n-k) * math.factorial(k))

amax = 0
i = n-1
while i >= k-1:
    amax += alist[i] * comb(i, k-1)
    i -= 1

amin = 0
i = 0
while i <= n-k:
    amin += alist[i] * comb(n-i-1, k-1)
    i += 1

print((amax - amin) % mod)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

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

n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
divide = 10**9 + 7

def comb(n, k):
    return math.factorial(n) // (math.factorial(n-k) * math.factorial(k))

amax = 0
i = n-1
while i >= k-1:
    amax += alist[i] * comb(i, k-1)
    i -= 1

amin = 0
i = 0
while i <= n-k:
    amin += alist[i] * comb(n-i-1, k-1)
    i += 1

print((amax - amin) % divide)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

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

n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
alist_rev = alist[::-1]
divide = 10**9 + 7

def comb(n, k):
    return math.factorial(n) // (math.factorial(n-k) * math.factorial(k))



amax = 0
i = n-1
while i >= k-1:
    amax += alist[i] * comb(i, k-1)
    i -= 1

amin = 0
i = 0
while i <= n-k:
    amin += alist[i] * comb(n-i-1, k-1)
    i += 1

print((amax - amin) % divide)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

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

n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
alist_rev = alist[::-1]
divide = 10**9 + 7

def comb(s, n):
    return len(list(itertools.combinations(s, n)))

amax = 0
i = 0
while i <= n-k:
    amax += alist_rev[i] * comb(alist_rev[i+1:], k-1)
    i += 1

amin = 0
i = 0
while i <= n-k:
    amin += alist[i] * comb(alist[i+1:], k-1)
    i += 1

print((amax - amin) % divide)


Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)

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

n, k = map(int, input().split())
alist = sorted(list(map(int, input().split())))
paterns = list(itertools.combinations(alist, k))
divide = 10**9 + 7

ans = 0
for patern in paterns:
    gmax = patern[-1]%divide
    gmin = patern[0]%divide
    ans += (gmax - gmin)%divide


print(ans%divide)

Fixed submission:
n, k = map(int, input().split())
alist = sorted(map(int, input().split()))
mod = 10**9 + 7

def comb(n, r, p=mod):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
N = n
for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

amax = 0
amin = 0
i = 0
while i <= n-k:
    cnt = comb(n-i-1, k-1)
    amax += (alist[n-i-1] * cnt)%mod
    amin += (alist[i] * cnt)%mod
    i += 1

print((amax - amin) % mod)