Buggy python submission (verdict: Time Limit Exceeded):
def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def cmb(a,b,c):
    b = min(b,a-b)
    num = 1
    for i in range(b):
        num = num*(a-i) % c
    den = 1
    for i in range(b):
        den = den*(i+1) % c
    return num * pow(den,c-2,c) % c

mod = 10**9+7
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
ans = 0
c = cmb(n-1,k-1,mod)
if k == 1:
    print(0)
    exit()
for i in range(n-k+1):
    ans -= a[i] * c
    ans += a[-i-1] * c
    ans %= mod
    c = c * (n-k-i) * pow(n-i-1,mod-2,mod)
if ans < 0:
    ans += mod
print(ans)


Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

ans,comb = 0,1
#i番目ではcomb = (K-1-i)_C_(K-1)
#max,min以外での選ぶ個数は常にK-1であることに注目すればcomb関数はいらなかった
for i in range(N-K+1):
    if i > 0:
        comb = (comb * (K-1+i) * inv(i,M)) % M
    ans = (ans + comb*((A[K-1+i]-A[N-K-i]) % M)) % M
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(a,b,c):
    b = min(b,a-b)
    num = 1
    for i in range(b):
        num = num*(a-i) % c
    den = 1
    for i in range(b):
        den = den*(i+1) % c
    return num * pow(den,c-2,c) % c

mod = 10**9+7
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
ans = 0
c = cmb(n-1,k-1,mod)
if k == 1:
    print(0)
    exit()
for i in range(n-k+1):
    ans -= a[i] * c
    ans += a[-i-1] * c
    ans %= mod
    c = c * (n-k-i) * pow(n-i-1,mod-2,mod)
if ans < 0:
    ans += mod
print(ans)


Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

ans,comb = 0,1
#i番目ではcomb = (K-1-i)_C_(K-1)
#max,min以外での選ぶ個数は常にK-1であることに注目すればcomb関数はいらなかった
for i in range(N-K+1):
    if i > 0:
        comb = (comb * (K-1+i) * inv(i,M)) % M
    ans = (ans + comb*((A[K-1+i]-A[N-K-i]) % M)) % M
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

def comb(n,k,M):
    nume,deno = 1,1
    for i in range(1,k+1):
        nume = (nume * (n - i + 1)) % M
        deno = (deno * i) % M
    return (nume * inv(deno,M)) % M

ans = 0
for i in range(N-K+1):
    ans = (ans + comb(N-1-i,K-1,M)*((A[N-1-i]-A[i]) % M)) % M
print(ans)


Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

ans,comb = 0,1
#i番目ではcomb = (K-1-i)_C_(K-1)
#max,min以外での選ぶ個数は常にK-1であることに注目すればcomb関数はいらなかった
for i in range(N-K+1):
    if i > 0:
        comb = (comb * (K-1+i) * inv(i,M)) % M
    ans = (ans + comb*((A[K-1+i]-A[N-K-i]) % M)) % M
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

def comb(n,k,M):
    nume,deno = 1,1
    for i in range(1,k+1):
        nume = (nume * (n - i + 1)) % M
        deno = (deno * i) % M
    return (nume * inv(deno,M)) % M

ans = 0
for i in range(N-K+1):
    ans = (ans + comb(N-1-i,K-1,M)*((A[N-1-i]-A[i]) % M)) % M
print(ans)


Fixed submission:
N,K = map(int,input().split())
A = list(map(int,input().split()))
A.sort()

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

ans,comb = 0,1
#i番目ではcomb = (K-1-i)_C_(K-1)
#max,min以外での選ぶ個数は常にK-1であることに注目すればcomb関数はいらなかった
for i in range(N-K+1):
    if i > 0:
        comb = (comb * (K-1+i) * inv(i,M)) % M
    ans = (ans + comb*((A[K-1+i]-A[N-K-i]) % M)) % M
print(ans)