Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7

def nCr(n,r,mod):
  if n<r:
    return 0
  ret=1  
  for i in range(min(r,n-r)):
    ret = ret*(n-i)*pow(min(r,n-r)-i,mod-2,mod)%mod

  return ret%mod  

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  mi = (mi + A[i-1] * nCr(n-i,k-1,MOD)) % MOD
  ma = (ma + A[-i]  * nCr(n-i,k-1,MOD)) % MOD
  
print((ma - mi)%MOD)

Fixed submission:
mod = 10**9 + 7

N_MAX = 10**5+1
fact = [1] * N_MAX
for i in range(1,N_MAX):
  fact[i] = (fact[i-1]*i) % mod
  
def nCr(n,r):  
  if n < r:
    return 0
  return (fact[n] * pow(fact[r]*fact[n-r] ,mod-2,mod))% mod

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  m = nCr(n-i,k-1)
  mi += A[i-1] * m
  ma += A[-i]  * m
  
print((ma - mi)%mod)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7


def nCr(n,r,mod):
  if n<r:
    return 0
  ret=1  
  for i in range(min(r,n-r)):
    ret = ret*(n-i)*pow(min(r,n-r)-i,mod-2,mod)%mod

  return ret%mod  

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  mi += A[i-1] * nCr(n-i,k-1,MOD)
  ma += A[-i]  * nCr(n-i,k-1,MOD)  
  
print((ma - mi)%MOD)

Fixed submission:
mod = 10**9 + 7

N_MAX = 10**5+1
fact = [1] * N_MAX
for i in range(1,N_MAX):
  fact[i] = (fact[i-1]*i) % mod
  
def nCr(n,r):  
  if n < r:
    return 0
  return (fact[n] * pow(fact[r]*fact[n-r] ,mod-2,mod))% mod

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  m = nCr(n-i,k-1)
  mi += A[i-1] * m
  ma += A[-i]  * m
  
print((ma - mi)%mod)

Buggy python submission (verdict: Time Limit Exceeded):
MOD = 10**9 + 7
def nCr(n, r, mod):
  ret = 1
  for i in range(r):
    ret = (ret * (n-i)) % mod
    ret = (ret * pow((r-i), mod-2, mod)) % mod
  return ret

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  mi += A[i-1] * nCr(n-i,k-1,MOD)
  ma += A[-i]  * nCr(n-i,k-1,MOD)
  
print((ma - mi)%MOD)

Fixed submission:
mod = 10**9 + 7

N_MAX = 10**5+1
fact = [1] * N_MAX
for i in range(1,N_MAX):
  fact[i] = (fact[i-1]*i) % mod
  
def nCr(n,r):  
  if n < r:
    return 0
  return (fact[n] * pow(fact[r]*fact[n-r] ,mod-2,mod))% mod

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  m = nCr(n-i,k-1)
  mi += A[i-1] * m
  ma += A[-i]  * m
  
print((ma - mi)%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import mul
from functools import reduce

def nCr(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
from itertools import combinations
MOD = 10**9+7

n,k = map(int,input().split())
aa = list(map(int,input().split()))

aa = sorted(aa)
  
ans = 0
for i in range(n-1,k-2,-1):
  ans += aa[i] * nCr(i,k-1)
  ans %= MOD
  
r_aa = list(reversed(aa))
for i in range(n-1,k-2,-1):
  ans -= r_aa[i] * nCr(i,k-1)
  ans %= MOD
  
print(ans)  

Fixed submission:
mod = 10**9 + 7

N_MAX = 10**5+1
fact = [1] * N_MAX
for i in range(1,N_MAX):
  fact[i] = (fact[i-1]*i) % mod
  
def nCr(n,r):  
  if n < r:
    return 0
  return (fact[n] * pow(fact[r]*fact[n-r] ,mod-2,mod))% mod

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  m = nCr(n-i,k-1)
  mi += A[i-1] * m
  ma += A[-i]  * m
  
print((ma - mi)%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from operator import mul
from functools import reduce

def nCr(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under
  
from itertools import combinations
MOD = 10**9+7

n,k = map(int,input().split())
aa = list(map(int,input().split()))

aa = sorted(aa)
  
ans = 0
for i in range(n-1,k-2,-1):
  ans += aa[i] * nCr(i,k-1)
  ans %= MOD
  
r_aa = list(reversed(aa))
for i in range(n-1,k-2,-1):
  ans -= r_aa[i] * nCr(i,k-1)
  ans %= MOD
  
print(ans)  

Fixed submission:
mod = 10**9 + 7

N_MAX = 10**5+1
fact = [1] * N_MAX
for i in range(1,N_MAX):
  fact[i] = (fact[i-1]*i) % mod
  
def nCr(n,r):  
  if n < r:
    return 0
  return (fact[n] * pow(fact[r]*fact[n-r] ,mod-2,mod))% mod

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  m = nCr(n-i,k-1)
  mi += A[i-1] * m
  ma += A[-i]  * m
  
print((ma - mi)%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
MOD = 10**9+7

n,k = map(int,input().split())
aa = list(map(int,input().split()))

ans = 0
for tu in list(combinations(aa,k)):
  ans += max(tu) - min(tu)  
  ans %= MOD
  
print(ans)  

Fixed submission:
mod = 10**9 + 7

N_MAX = 10**5+1
fact = [1] * N_MAX
for i in range(1,N_MAX):
  fact[i] = (fact[i-1]*i) % mod
  
def nCr(n,r):  
  if n < r:
    return 0
  return (fact[n] * pow(fact[r]*fact[n-r] ,mod-2,mod))% mod

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  m = nCr(n-i,k-1)
  mi += A[i-1] * m
  ma += A[-i]  * m
  
print((ma - mi)%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import combinations
MOD = 10**9+7

n,k = map(int,input().split())
aa = list(map(int,input().split()))

ans = 0
for tu in list(combinations(aa,k)):
  ans += max(tu) - min(tu)  
  ans %= MOD
  
print(ans)  

Fixed submission:
mod = 10**9 + 7

N_MAX = 10**5+1
fact = [1] * N_MAX
for i in range(1,N_MAX):
  fact[i] = (fact[i-1]*i) % mod
  
def nCr(n,r):  
  if n < r:
    return 0
  return (fact[n] * pow(fact[r]*fact[n-r] ,mod-2,mod))% mod

n,k = map(int,input().split())
A = sorted(list(map(int,input().split())))

mi = 0
ma = 0

for i in range(1,n):
  m = nCr(n-i,k-1)
  mi += A[i-1] * m
  ma += A[-i]  * m
  
print((ma - mi)%mod)