Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
MOD = 10**9+7
ans = 0
for i in range(n):
  if n-i < k:
    continue
  C =  factorial(n-i-1)//(factorial(k-1)*factorial(n-i-k))
  C %= MOD
  ans += ((a[i]-a[n-i-1]) * C) % MOD
  ans %= MOD

print(ans)
  

Fixed submission:
MAX = 100000 #階乗のmodを求めたい中で最大のもの
MOD = 10**9+7

fac = [0]*MAX   #階乗のmod
inv = [0]*MAX    #その数の逆元
finv = [0]*MAX   #階乗の逆元

def COMinit():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2,MAX):
        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
    if n < 0 or k < 0:
        return 0
    return (fac[n] * (finv[k]*finv[n-k]))%MOD

#pが素数でないと使えない
COMinit()
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
MOD = 10**9+7
ans = 0
for i in range(n):
  if n-i < k:
    continue
  C = COM(n-i-1,k-1)
  ans += ((a[i]-a[n-i-1]) * C) % MOD
  ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
MOD = 10**9+7
ans = 0
for i in range(n):
  if n-i < k:
    continue
  C = ( factorial(n-i-1)//factorial(k-1) ) // factorial(n-i-k)
  C %= MOD
  ans += ((a[i]-a[n-i-1]) * C) % MOD
  ans %= MOD

print(ans)
  

Fixed submission:
MAX = 100000 #階乗のmodを求めたい中で最大のもの
MOD = 10**9+7

fac = [0]*MAX   #階乗のmod
inv = [0]*MAX    #その数の逆元
finv = [0]*MAX   #階乗の逆元

def COMinit():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2,MAX):
        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
    if n < 0 or k < 0:
        return 0
    return (fac[n] * (finv[k]*finv[n-k]))%MOD

#pが素数でないと使えない
COMinit()
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
MOD = 10**9+7
ans = 0
for i in range(n):
  if n-i < k:
    continue
  C = COM(n-i-1,k-1)
  ans += ((a[i]-a[n-i-1]) * C) % MOD
  ans %= MOD

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
MOD = 10**9+7
ans = 0
for i in range(n):
  if n-i-1 < k-1:
    continue
  C = ( factorial(n-i-1)//factorial(k-1) ) // factorial(n-i-k)
  C %= MOD
  ans += ((a[i]-a[n-i-1]) * C) % MOD
  ans %= MOD

print(ans)
  

Fixed submission:
MAX = 100000 #階乗のmodを求めたい中で最大のもの
MOD = 10**9+7

fac = [0]*MAX   #階乗のmod
inv = [0]*MAX    #その数の逆元
finv = [0]*MAX   #階乗の逆元

def COMinit():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2,MAX):
        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
    if n < 0 or k < 0:
        return 0
    return (fac[n] * (finv[k]*finv[n-k]))%MOD

#pが素数でないと使えない
COMinit()
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort(reverse = True)
MOD = 10**9+7
ans = 0
for i in range(n):
  if n-i < k:
    continue
  C = COM(n-i-1,k-1)
  ans += ((a[i]-a[n-i-1]) * C) % MOD
  ans %= MOD

print(ans)