Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
#a = factorial(n) / factorial(r) / factorial(n - r)
n,m = map(int,input().split())
l = list(map(int,input().split()))
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
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
l.sort()
ans = 0
mod = 10**9+7
mi = 0
for i in range(n-m+1):
    com = combination(n-i-1,m-1)
    ans += com*(l[-i-1] -l[i])
    
    ans %= mod
    
    
print(ans%mod)


Fixed submission:
n,k = map(int,input().split())
l = list(map(int,input().split()))

mod = 10**9+7

fact = [1]
for i in range(n):
    fact.append(fact[-1]*(i+1)%mod)

finv = [1]*(n+1)
finv[n] = pow(fact[-1],mod-2,mod)
for i in range(n-1,0,-1):
    finv[i] = ((i+1)*finv[i+1])%mod

def combi(n,k,mod):
    return (fact[n]*finv[k]*finv[n-k])%mod

l.sort()

ans = 0
for i in range(n-k+1):
    tmp = combi(n-i-1,k-1,mod)
    ans += tmp*(l[-i-1]-l[i])
    ans %= mod

print(ans%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
#a = factorial(n) / factorial(r) / factorial(n - r)
n,m = map(int,input().split())
l = list(map(int,input().split()))
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
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
l.sort()
ans = 0
mod = 10**9+7
mi = 0
for i in range(n-m+1):
    com = combination(n-i-1,m-1)
    ans += com*l[-i-1]
    mi += com*l[i]
    ans %= mod
    mi %= mod
    
print((ans-mi)%mod)


Fixed submission:
n,k = map(int,input().split())
l = list(map(int,input().split()))

mod = 10**9+7

fact = [1]
for i in range(n):
    fact.append(fact[-1]*(i+1)%mod)

finv = [1]*(n+1)
finv[n] = pow(fact[-1],mod-2,mod)
for i in range(n-1,0,-1):
    finv[i] = ((i+1)*finv[i+1])%mod

def combi(n,k,mod):
    return (fact[n]*finv[k]*finv[n-k])%mod

l.sort()

ans = 0
for i in range(n-k+1):
    tmp = combi(n-i-1,k-1,mod)
    ans += tmp*(l[-i-1]-l[i])
    ans %= mod

print(ans%mod)

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
#a = factorial(n) / factorial(r) / factorial(n - r)
n,m = map(int,input().split())
l = list(map(int,input().split()))
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
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
l.sort()
ans = 0
mod = 10**9+7
for i in range(n-m+1):
    for j in range(i+m-1,n):
        a = combination(j-i-1,m-2)
        
        ans += a*(l[j]-l[i])
      
        ans %= mod
print(ans%mod)


Fixed submission:
n,k = map(int,input().split())
l = list(map(int,input().split()))

mod = 10**9+7

fact = [1]
for i in range(n):
    fact.append(fact[-1]*(i+1)%mod)

finv = [1]*(n+1)
finv[n] = pow(fact[-1],mod-2,mod)
for i in range(n-1,0,-1):
    finv[i] = ((i+1)*finv[i+1])%mod

def combi(n,k,mod):
    return (fact[n]*finv[k]*finv[n-k])%mod

l.sort()

ans = 0
for i in range(n-k+1):
    tmp = combi(n-i-1,k-1,mod)
    ans += tmp*(l[-i-1]-l[i])
    ans %= mod

print(ans%mod)