Buggy python submission (verdict: Time Limit Exceeded):
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N-1,-1,-1):
  for i in range(j-K+2):
    mi = A[i]
    ma = A[j]
    #print(ma,mi,i,j,L[j-i-1])
 #     print(j-i-1,com(j-i-1,K-2))
    ans+=(ma-mi)*com(j-i-1,K-2)
print(ans%(10**9+7))

Fixed submission:
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  if n<k:
    return 0
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N):
  ans+=(A[j])*com(j,K-1)
  ans-=(A[j])*com(N-1-j,K-1)
print(ans%(10**9+7))

Buggy python submission (verdict: Time Limit Exceeded):
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N-1,-1,-1):
  for i in range(j-K+2):
    mi = A[i]
    ma = A[j]
    #print(ma,mi,i,j,L[j-i-1])
    if j-i-1>=K-2:
 #     print(j-i-1,com(j-i-1,K-2))
      ans+=(ma-mi)*com(j-i-1,K-2)
print(ans%(10**9+7))

Fixed submission:
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  if n<k:
    return 0
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N):
  ans+=(A[j])*com(j,K-1)
  ans-=(A[j])*com(N-1-j,K-1)
print(ans%(10**9+7))

Buggy python submission (verdict: Time Limit Exceeded):
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N-1,-1,-1):
  for i in range(j-K+2):
    mi = A[i]
    ma = A[j]
    #print(ma,mi,i,j,L[j-i-1])
    if j-i-1>=K-2:
 #     print(j-i-1,com(j-i-1,K-2))
      ans+=(ma-mi)*com(j-i-1,K-2)
print(ans%(10**9+7))

Fixed submission:
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  if n<k:
    return 0
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N):
  ans+=(A[j])*com(j,K-1)
  ans-=(A[j])*com(N-1-j,K-1)
print(ans%(10**9+7))

Buggy python submission (verdict: Runtime Error):
# nCr mod m
# mを法とするaの乗法的逆元
# 拡張ユークリッド互除法
# gcd(a,b) と ax + by = gcd(a,b) の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m
# modinvが必要
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
ans=0
if K==1:
  print(0)
elif K==2:
  L = [1]*(N+1)
elif K>=3:
  L = [0]*(N-1)
  for i in range(N-2,-1,-1):
    if i>=K-2:
      L[i]=combination(i,K-2)
#print(L)
for j in range(N-1,-1,-1):
  for i in range(j-K+2):
    mi = A[i]
    ma = A[j]
    #print(ma,mi,i,j,L[j-i-1])
    if j-i-1>=0:
      ans+=(ma-mi)*L[j-i-1]
print(ans%(10**9+7))

Fixed submission:
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  if n<k:
    return 0
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N):
  ans+=(A[j])*com(j,K-1)
  ans-=(A[j])*com(N-1-j,K-1)
print(ans%(10**9+7))

Buggy python submission (verdict: Runtime Error):
# nCr mod m
# mを法とするaの乗法的逆元
# 拡張ユークリッド互除法
# gcd(a,b) と ax + by = gcd(a,b) の最小整数解を返す
def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

def modinv(a, m):
    g, x, y = egcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m
# modinvが必要
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
ans=0
if K==1:
  print(0)
elif K==2:
  L = [1]*(N+1)
elif K>=3:
  L = [0]*(N-1)
  for i in range(N-2,-1,-1):
    if i>=K-2:
      L[i]=combination(i,K-2)
#print(L)
for j in range(N-1,-1,-1):
  for i in range(j-K+2):
    mi = A[i]
    ma = A[j]
    #print(ma,mi,i,j,L[j-i-1])
    if j-i-1>=0:
      ans+=(ma-mi)*L[j-i-1]
print(ans%(10**9+7))

Fixed submission:
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  finv[1]=1
  inv[1]=1
  for i in range(2,N+1):
    fac[i] = fac[i - 1] * i % MOD
    inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i - 1] * inv[i] % MOD
def com(n,k):
  if n<k:
    return 0
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N):
  ans+=(A[j])*com(j,K-1)
  ans-=(A[j])*com(N-1-j,K-1)
print(ans%(10**9+7))