Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
lis = list(map(int, input().split()))
p = 1000000007
fac_lis = [[0] for i in range(N)]
fac_lis[0] = 1
max_ans = 0
min_ans = 0

for i in range(1,N):
  fac_lis[i] = fac_lis[i-1]*(i+1)

def combi(n,k):
  if n == k:
    return 1
  else:
  	return fac_lis[n-1]*pow(fac_lis[k-1], p-2, p)*pow(fac_lis[n-k-1], p-2, p) % p

lis.sort()
for i in range(K-1,N):
  max_ans += lis[i]*combi(i, K-1)
  max_ans %= p
  
for i in range(N-K+1):
  min_ans += lis[i]*combi(N-(i+1), K-1)
  min_ans %= p

ans = (max_ans - min_ans) % p
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
lis = list(map(int, input().split()))
p = 10**9+7
fac_lis = [[0] for i in range(N)]
fac_lis[0] = 1
max_ans = 0
min_ans = 0

for i in range(1,N):
  fac_lis[i] = fac_lis[i-1]*(i+1)

def combi(n,k):
  if n == k:
    return 1
  else:
  	return fac_lis[n-1]*pow(fac_lis[k-1], p-2, p)*pow(fac_lis[n-k-1], p-2, p) % p

lis.sort()
for i in range(K-1,N):
  max_ans += lis[i]*combi(i, K-1)
  max_ans %= p
  
for i in range(N-K+1):
  min_ans += lis[i]*combi(N-(i+1), K-1)
  min_ans %= p

ans = (max_ans - min_ans) % p
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
lis = list(map(int, input().split()))
p = 10**9+7
fac_lis = [[0] for i in range(N)]
fac_lis[0] = 1
max_ans = 0
min_ans = 0

for i in range(1,N):
  fac_lis[i] = fac_lis[i-1]*(i+1)

def combi(n,k):
  if n == k:
    return 1
  else:
  	return fac_lis[n-1]*pow(fac_lis[k-1], p-2, p)*pow(fac_lis[n-k-1], p-2, p) % p

lis.sort()
for i in range(K-1,N):
  max_ans += lis[i]*combi(i, K-1) % p
  
for i in range(N-K+1):
  min_ans += lis[i]*combi(N-(i+1), K-1) % p

ans = (max_ans - min_ans) % p
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
lis = list(map(int, input().split()))
p = 10**9+7
fac_lis = [[0] for i in range(N)]
fac_lis[0] = 1
max_ans = 0
min_ans = 0

for i in range(1,N):
  fac_lis[i] = fac_lis[i-1]*(i+1)

def combi(n,k,big_num):
  if n == k:
    return 1
  else:
  	return fac_lis[n-1]*pow(fac_lis[k-1], big_num-2, big_num)*pow(fac_lis[n-k-1], big_num-2, big_num) % big_num

lis.sort()
for i in range(K-1,N):
  max_ans += lis[i]*combi(i, K-1, p) % p
  
for i in range(N-K+1):
  min_ans += lis[i]*combi(N-(i+1), K-1, p) % p

ans = (max_ans - min_ans) % p
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
lis = list(map(int, input().split()))
p = 10**9+7
fac_lis = [[0] for i in range(N)]
fac_lis[0] = 1
max_ans = 0
min_ans = 0

for i in range(1,N):
  fac_lis[i] = fac_lis[i-1]*(i+1)

def combi(n,k,num_lis,big_num):
  if n == k:
    return 1
  else:
  	return num_lis[n-1]*pow(num_lis[k-1], big_num-2, big_num)*pow(num_lis[n-k-1], big_num-2, big_num) % big_num

lis.sort()
for i in range(K-1,N):
  max_ans += lis[i]*combi(i, K-1, fac_lis, p) % p
  
for i in range(N-K+1):
  min_ans += lis[i]*combi(N-(i+1), K-1, fac_lis, p) % p

ans = (max_ans - min_ans) % p
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
lis = list(map(int, input().split()))
lis = sorted(lis)
p = 10**9+7
fac_lis = [[0] for i in range(N)]
fac_lis[0] = 1
max_ans = 0
min_ans = 0

for i in range(1,N):
  fac_lis[i] = fac_lis[i-1]*(i+1)

def combi(n,k,num_lis,big_num):
  if n == k:
    return 1
  else:
  	return num_lis[n-1]*pow(num_lis[k-1], big_num-2, big_num)*pow(num_lis[n-k-1], big_num-2, big_num) % big_num

for i in range(K-1,N):
  max_ans += lis[i]*combi(i, K-1, fac_lis, p) % p
  
for i in range(N-K+1):
  min_ans += lis[i]*combi(N-(i+1), K-1, fac_lis, p) % p

ans = (max_ans - min_ans) % p
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Time Limit Exceeded):
import operator as op
from functools import reduce

N, K = map(int, input().split())
num_lis = list(map(int, input().split()))
num_lis = sorted(num_lis)

ans = 0
combi_lis = []
multi = op.mul
numer = reduce(multi, range(1, K+1), 1)
denom = reduce(multi, range(1, K+1), 1)

#max
for i in range(K-1, N):
  combi = numer//denom
  combi_lis.append(combi)
  ans += num_lis[i]*combi
  numer *= i+1
  numer = numer//(i-K+2)

#min
k = 0
for j in range(N-K,-1,-1):
  ans -= num_lis[j]*combi_lis[k]
  k += 1

ans %= 10**9+7
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Runtime Error):
import operator as op
from functools import reduce

N, K = map(int, input().split())
num_lis = list(map(int, input().split()))
num_lis = sorted(num_lis)

ans = 0
combi_lis = []
multi = op.mul
numer = reduce(multi, range(1, K+1), 1)
denom = reduce(multi, range(1, K+1), 1)

#max
for i in range(K-1, N):
  combi = int(numer/denom)
  combi_lis.append(combi)
  ans += num_lis[i]*combi
  numer *= i+1
  numer /= i-K+2

#min
k = 0
for j in range(N-K,-1,-1):
  ans -= num_lis[j]*combi_lis[k]
  k += 1

ans %= 10**9+7
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Runtime Error):
import operator as op
from functools import reduce

N, K = map(int, input().split())
num_lis = list(map(int, input().split()))
num_lis = sorted(num_lis)

#組み合わせ
def combi(N,K):
  multi = op.mul
  numer = reduce(multi, range(N, N-K, -1),1)
  denom = reduce(multi, range(1,K+1), 1)
  combi = int(numer/denom)
  return combi

ans = 0
#max
combi_lis = []
for i in range(K-1,N):
  num = combi(i, K-1)
  ans += num_lis[i]*num
  combi_lis.append(num)

#min
k = 0
for j in range(N-K,-1,-1):
  ans -= num_lis[j]*combi_lis[k]
  k += 1

print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Runtime Error):
import operator as op
from functools import reduce

N, K = map(int, input().split())
num_lis = list(map(int, input().split()))
num_lis = sorted(num_lis)

#組み合わせ
def combi(N,K):
  multi = op.mul
  numer = reduce(multi, range(N, N-K, -1),1)
  denom = reduce(multi, range(1,K+1), 1)
  combi = int(numer/denom)
  return combi

ans = 0
#max
for i in range(K-1,N):
  ans += num_lis[i]*combi(i, K-1)%(10**9+7)

#min
for j in range(N-K,-1,-1):
  ans -= num_lis[j]*combi(N-j-1,K-1)%(10**9+7)

print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)

Buggy python submission (verdict: Runtime Error):
import operator as op
from functools import reduce

N, K = map(int, input().split())
num_lis = list(map(int, input().split()))
num_lis = sorted(num_lis)

#組み合わせ
def combi(N,K):
  multi = op.mul
  numer = reduce(multi, range(N, N-K, -1),1)
  denom = reduce(multi, range(1,K+1), 1)
  combi = int(numer/denom)
  return combi

ans = 0
#max
for i in range(K-1,N):
  ans += num_lis[i]*combi(i, K-1)

#min
for j in range(N-K,-1,-1):
  ans -= num_lis[j]*combi(N-j-1,K-1)
  
ans %= 10**9+7
print(ans)

Fixed submission:
# フェルマーの小定理
N, K = map(int, input().split())
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

A.sort()
minX = 0
for i in range(N - K + 1):
    minX += A[i] * mcomb(N - (i + 1), K - 1)
    minX %= m

print((maxX - minX) % m)