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

def mymodBinomial(n, k, p):
	if k < 0 or k > n:
		return 0
	else:
		numerator = 1
		for i in range(k):
			numerator = (numerator * (n - i)) % p
		denominator = 1
		for i in range(1, k + 1):
			denominator = (denominator * i) % p
		return (numerator * pow(denominator, p - 2, p)) % p


if __name__ == "__main__":
	N, K = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	# print(A)
	l = [0]*(N + 1)
	for i in range(1, N+1):
		l[i] = mymodBinomial(i-1, K-1, MOD)
	ans = 0
	# print(l)
	for i in range(N):
		ans += (A[i] - A[N-1-i])* l[i+1] % MOD
	print(ans % MOD)

Fixed submission:
MOD = 10 ** 9 + 7

# def mymodBinomial(n, k, p):
# 	if k < 0 or k > n:
# 		return 0
# 	else:
# 		numerator = 1
# 		for i in range(k):
# 			numerator = (numerator * (n - i)) % p
# 		denominator = 1
# 		for i in range(1, k + 1):
# 			denominator = (denominator * i) % p
# 		return (numerator * pow(denominator, p - 2, p)) % p

def modBinomiallist(n, k, p):
	l = [0 for i in range(n)]
	for i in range(n):
		if i < k:
			l[i] = 0
		elif i == k:
			l[i] = 1
		else:
			l[i] = l[i - 1] * i * pow(i-k, p-2, p) % p
	return l


if __name__ == "__main__":
	N, K = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	# print(A)
	# l = [0]*(N + 1)
	# for i in range(1, N+1):
	# 	l[i] = mymodBinomial(i-1, K-1, MOD)
	# print(l)
	l = modBinomiallist(N, K-1, MOD)
	# print(l)
	ans = 0
	# print(l)
	for i in range(N):
		ans += (A[i] - A[N-1-i])* l[i] % MOD
	print(ans % MOD)

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

def mymodBinomial(n, k, p):
	if k < 0 or k > n:
		return 0
	else:
		numerator = 1
		for i in range(k):
			numerator = (numerator * (n - i)) % p
		denominator = 1
		for i in range(1, k + 1):
			denominator = (denominator * i) % p
		return (numerator * pow(denominator, p - 2, p)) % p


if __name__ == "__main__":
	N, K = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	# print(A)
	l = [0]*(N + 1)
	for i in range(1, N+1):
		l[i] = mymodBinomial(i-1, K-1, MOD) - mymodBinomial(N-i, K-1, MOD)
	ans = 0
	# print(l)
	for i in range(N):
		ans += A[i] * l[i+1] % MOD
	print(ans % MOD)

Fixed submission:
MOD = 10 ** 9 + 7

# def mymodBinomial(n, k, p):
# 	if k < 0 or k > n:
# 		return 0
# 	else:
# 		numerator = 1
# 		for i in range(k):
# 			numerator = (numerator * (n - i)) % p
# 		denominator = 1
# 		for i in range(1, k + 1):
# 			denominator = (denominator * i) % p
# 		return (numerator * pow(denominator, p - 2, p)) % p

def modBinomiallist(n, k, p):
	l = [0 for i in range(n)]
	for i in range(n):
		if i < k:
			l[i] = 0
		elif i == k:
			l[i] = 1
		else:
			l[i] = l[i - 1] * i * pow(i-k, p-2, p) % p
	return l


if __name__ == "__main__":
	N, K = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	# print(A)
	# l = [0]*(N + 1)
	# for i in range(1, N+1):
	# 	l[i] = mymodBinomial(i-1, K-1, MOD)
	# print(l)
	l = modBinomiallist(N, K-1, MOD)
	# print(l)
	ans = 0
	# print(l)
	for i in range(N):
		ans += (A[i] - A[N-1-i])* l[i] % MOD
	print(ans % MOD)

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

def mymodBinomial(n, k, p):
	if k < 0 or k > n:
		return 0
	else:
		numerator = 1
		for i in range(k):
			numerator = (numerator * (n - i)) % p
		denominator = 1
		for i in range(1, k + 1):
			denominator = (denominator * i) % p
		return (numerator * pow(denominator, p - 2, p)) % p


if __name__ == "__main__":
	N, K = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	# print(A)
	l = [0]*(N + 1)
	for i in range(1, N+1):
		l[i] = mymodBinomial(i-1, K-1, MOD) - mymodBinomial(N-i, K-1, MOD)
	ans = 0
	# print(l)
	for i in range(N):
		ans += A[i] * l[i+1] % MOD
	print(ans % MOD)

Fixed submission:
MOD = 10 ** 9 + 7

# def mymodBinomial(n, k, p):
# 	if k < 0 or k > n:
# 		return 0
# 	else:
# 		numerator = 1
# 		for i in range(k):
# 			numerator = (numerator * (n - i)) % p
# 		denominator = 1
# 		for i in range(1, k + 1):
# 			denominator = (denominator * i) % p
# 		return (numerator * pow(denominator, p - 2, p)) % p

def modBinomiallist(n, k, p):
	l = [0 for i in range(n)]
	for i in range(n):
		if i < k:
			l[i] = 0
		elif i == k:
			l[i] = 1
		else:
			l[i] = l[i - 1] * i * pow(i-k, p-2, p) % p
	return l


if __name__ == "__main__":
	N, K = map(int, input().split())
	A = list(map(int, input().split()))
	A.sort()
	# print(A)
	# l = [0]*(N + 1)
	# for i in range(1, N+1):
	# 	l[i] = mymodBinomial(i-1, K-1, MOD)
	# print(l)
	l = modBinomiallist(N, K-1, MOD)
	# print(l)
	ans = 0
	# print(l)
	for i in range(N):
		ans += (A[i] - A[N-1-i])* l[i] % MOD
	print(ans % MOD)