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

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
N, K = map(int, input().split())
A = list(map(int, input().split()))
A=sorted(A)
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):
    for j in range(i-K+2):
        ANSWER += (A[i]-A[j])*combinations(i-j+1-2, K-2)
ANSWER %= MOD
print(ANSWER)


Fixed submission:
MAX_NUM = 10**6 + 1
MOD = 10**9+7

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
N, K = map(int, input().split())
A = list(map(int, input().split()))
A=sorted(A)
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):
    ANSWER += A[i]*combinations(i, K-1)
    # print(ANSWER)
for i in range(N-K+1):
    ANSWER -= A[i]*combinations(N-i-1, K-1)
    # print(ANSWER)

ANSWER %= MOD
print(ANSWER)

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

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):

    for j in range(i-K+2):
        ANSWER += (A[i]-A[j])*combinations(i-j+1-2, K-2)
        ANSWER %= MOD
print(ANSWER)


Fixed submission:
MAX_NUM = 10**6 + 1
MOD = 10**9+7

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
N, K = map(int, input().split())
A = list(map(int, input().split()))
A=sorted(A)
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):
    ANSWER += A[i]*combinations(i, K-1)
    # print(ANSWER)
for i in range(N-K+1):
    ANSWER -= A[i]*combinations(N-i-1, K-1)
    # print(ANSWER)

ANSWER %= MOD
print(ANSWER)

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

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
N, K = map(int, input().split())
A = list(map(int, input().split()))
A.sort()
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):

    for j in range(i-K+2):
        ANSWER += (A[i]-A[j])*combinations(i-j+1-2, K-2)
        ANSWER %= MOD
print(ANSWER)


Fixed submission:
MAX_NUM = 10**6 + 1
MOD = 10**9+7

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(n,k):
    if n < k:
        return 0
    if n < 0 or k < 0:
        return 0
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD
N, K = map(int, input().split())
A = list(map(int, input().split()))
A=sorted(A)
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):
    ANSWER += A[i]*combinations(i, K-1)
    # print(ANSWER)
for i in range(N-K+1):
    ANSWER -= A[i]*combinations(N-i-1, K-1)
    # print(ANSWER)

ANSWER %= MOD
print(ANSWER)