CodeEff/ECCO huggingface dataset:
# Original solution
N, K = list(map(int, input().split()))

A = list(map(int, input().split()))

MOD = 1000000007

def combinations(n, r):
    r = min(r, n - r)

    num = 1
    den = 1

    for i in range(1, r + 1):
        num = num * (n + 1 - i) % MOD
        den = den * i % MOD

    return num * pow(den, MOD - 2, MOD) % MOD


A.sort()

max_sum = 0
min_sum = 0
for i, a in enumerate(A):
    # max
    if i >= K - 1:
        max_sum += a * combinations(i, K - 1)

    # min
    if i <= N - K:
        min_sum += a * combinations(N - i - 1, K - 1)


print((int((max_sum - min_sum) % MOD)))


# Optimized solution
N, K = list(map(int, input().split()))

A = list(map(int, input().split()))

MOD = 1000000007


def combinations(n, r, MOD):
    if (r < 0) or (n < r):
        return 0

    r = min(r, n - r)
    return fact[n] * fact_inv[r] * fact_inv[n - r] % MOD


A.sort()
fact = [1, 1]
fact_inv = [1, 1]
inv = [0, 1]

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % MOD)
    inv.append((-inv[MOD % i] * (MOD // i)) % MOD)
    fact_inv.append((fact_inv[-1] * inv[-1]) % MOD)

max_sum = 0
min_sum = 0

for i, a in enumerate(A):
    # max
    if i >= K - 1:
        max_sum += a * combinations(i, K - 1, MOD)

    # min
    if i <= N - K:
        min_sum += a * combinations(N - i - 1, K - 1, MOD)

print((int((max_sum - min_sum) % MOD)))
# Original solution
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
res = 0
a.sort()
cs = [0] * (N + 1)
for i in range(N): cs[i + 1] = cs[i] + a[i]
#print(cs)

class Factorial:
  def __init__(self, n, mod):
    self.f = [1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i = [pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % mod * self.i[k] % mod

f = Factorial(N, mod)
t = [0] * (N + 1)
for i in range(1, N + 1):
  if i - 1 < K - 2: continue
  x = f.combi(i - 1, max(K - 2, 0))
  y = -cs[i] + cs[-1] - cs[-1 - i]
  t[i] = x * y % mod
  res += t[i]
  res %= mod
#print(t)
print(res)

# Optimized solution
import sys
input = sys.stdin.readline
N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort()

class Factorial:
  def __init__(self, n, mod):
    self.mod = mod
    self.f = [1]
    for i in range(1, n + 1):
      self.f.append(self.f[-1] * i % mod)
    self.i = [pow(self.f[-1], mod - 2, mod)]
    for i in range(1, n + 1)[: : -1]:
      self.i.append(self.i[-1] * i % mod)
    self.i.reverse()
  def factorial(self, i):
    return self.f[i]
  def ifactorial(self, i):
    return self.i[i]
  def combi(self, n, k):
    return self.f[n] * self.i[n - k] % self.mod * self.i[k] % self.mod
  def permi(self, n, k):
    return self.f[n] * self.i[n - k] % self.mod

f = Factorial(N, mod)

res = 0
for l in range(N - K + 1):
  res -= f.combi(N - l - 1, K - 1) * a[l]
  res %= mod
for r in range(K - 1, N):
  res += f.combi(r, K - 1) * a[r]
  res %= mod
print(res)
# Original solution
from heapq import heappush, heappop
from collections import deque
import re
import math
import functools
import itertools
import fractions

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(eval(input()))


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29


def mk1d(n, val=INF):
    return [val for i in range(n)]

def mk2d(h, w, val=INF):
    return [[val for i in range(w)]for i in range(h)]

DIV = 998244353

def gcd(a, b):
    if(b == 0):
        return a
    return gcd(b, a % b)

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)


def lcm(*numbers):
    return functools.reduce(lcm_base, numbers, 1)


def floyd_warshall(costs):
    N = len(costs)
    for k in range(N):
        for i in range(N):
            for j in range(N):
                costs[i][j] = min(costs[i][j], costs[i][k]+costs[k][j])
    return costs

def lcm_list(numbers):
    return functools.reduce(lcm_base, numbers, 1)

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)

def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b

def ncr(n, r, mod=DIV):
	r = min(r, n-r)
	ret = 1
	for i in range(1, r+1):
		ret = ret * (n-i+1) % mod
		ret = ret * mod_inv(i, mod) % mod
	return ret

def main():
    N,K = isRaw()
    As = isRaw()
    As.sort()
    dp = [0]*N
    dp[K-1] = As[K-1]-As[0]
    for n in range(K,N):
        dp[n] = dp[n-1]
        for idx in range(n-K+2):
            nCk = ncr(n-idx-1,K-2)
            dp[n] = (dp[n]+nCk*(As[n]-As[idx])+DIV)%DIV
    return dp[N-1]

if __name__ == "__main__":
    print((main()))


# Optimized solution

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(eval(input()))


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)

def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max,mod=DIV):
    fac,finv,inv = [0]*max, [0]*max, [0]*max
    fac[0]=fac[1]=1
    finv[0]=finv[1]=1
    inv[1]=1
    for i in range(2,max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod -inv[mod%i] * (mod//i)%mod
        finv[i] = finv[i-1]*inv[i]%mod
    class CONV:
        def __init__(self):
            pass
        def ncr(self,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
    return CONV()



def main():
    n,k = isRaw()
    As = list(isRaw())
    As.sort()
    ct = CONV_TBL(n)
    ans = 0

    for i in range(n):
        ans += As[i]*ct.ncr(i, k-1) - As[i]*ct.ncr(n-(i+1), k-1)
        ans = ans%DIV
    return ans

if __name__ == "__main__":
    print((main()))
# Original solution
import itertools

N, K = (int(x) for x in input().split())
As = (int(x) for x in input().split())


def f(S):
    m = 1000000001
    M = -1000000001

    for a in S:
        m = min(m, a)
        M = max(M, a)

    return M - m

Ss = list(itertools.combinations(As, K))


answer = 0
for S in Ss:
    answer = answer + f(S) 

print((answer % 1000000007))


# Optimized solution
import itertools

N, K = (int(x) for x in input().split())
As = (int(x) for x in input().split())

def f(S):
    m = 1000000001
    M = -1000000001

    for a in S:
        m = min(m, a)
        M = max(M, a)

    return M - m

answer = 0
for S in itertools.combinations(As, K):
    answer = answer + f(S) 

print((answer % 1000000007))
# Original solution
import itertools

mod = 10**9 +7
n,k = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]

f = list(itertools.combinations(l, k))

ans = 0
for i in f:
    ans += (max(i) - min(i))
    
print((ans%mod))
    

# Optimized solution
import itertools

mod = 10**9 +7
n,k = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]

f = itertools.combinations(l, k)

ans = 0
for i in f:
    ans += abs(i[0] - i[k-1])
    
print((ans%mod))
# Original solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
p = 10 ** 9 + 7
#二項定理(nCr)を高速に計算する
def cmb(n,r,p):
    N = 10 ** 5 + 5 #必要なサイズを指定
    fact = [1,1]
    factinv = [1,1]
    inv = [0,1]
    for i in range(2,N+1):
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1] % p))

    if (r < 0) or (n < r):
        return 0
    r = min(r,n-r)
    return fact[n] * factinv[r] * factinv[n-r] % p

A_max = 0
A_min = 0
for i in range(N-K+1):
    A_max = (A_max + (A[(K-1)+i] * cmb((K+i-1),i,p))) % p   
    A_min = (A_min + (A[-1*(K+i)] * cmb((K+i-1),i,p))) % p
    #print(A_max,A_min,cmb((K+i),i,p))
ans = (A_max - A_min)
if ans < 0:
    ans = p + ans
ans %= p
print(ans)

# Optimized solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
mod = p = 10 ** 9 + 7

fact = [1]
fact_inv = [1]
for i in range(10**5+5):
    new_fact = fact[-1]*(i+1)%mod
    fact.append(new_fact)
    fact_inv.append(pow(new_fact,mod-2,mod))
 
 
def mod_comb_k(n,k,mod):
    return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod    

A_max = 0
A_min = 0
for i in range(N-K+1):
    A_max = (A_max + (A[(K-1)+i] * mod_comb_k((K+i-1),i,p))) % p   
    A_min = (A_min + (A[-1*(K+i)] * mod_comb_k((K+i-1),i,p))) % p
    #print(A_max,A_min,cmb((K+i),i,p))
ans = (A_max - A_min)
if ans < 0:
    ans = p + ans
ans %= p
print(ans)
# Original solution
def cmb1(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**6
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )

n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
mod = 10**9+7

A.sort(reverse=True)

max_ = 0
for i in range(n):
    max_ += cmb1(n-1-i, k-1, mod)*A[i]
max_ %= mod

min_ = 0
for i in range(n):
    min_ += cmb1(n-1-i, k-1, mod)*A[n-1-i]
min_ %= mod
ans = max_-min_
ans %= mod
print(ans)    


# Optimized solution
n, k =list(map(int, input().split()))
A = list(map(int, input().split()))

mod = 10**9+7
N = 10**5+50
fac = [1]*(N+1)
finv = [1]*(N+1)
for i in range(N):
    fac[i+1] = fac[i] * (i+1) % mod
finv[-1] = pow(fac[-1], mod-2, mod)
for i in reversed(list(range(N))):
    finv[i] = finv[i+1] * (i+1) % mod

def cmb1(n, r, mod):
    if r <0 or r > n:
        return 0
    r = min(r, n-r)
    return fac[n] * finv[r] * finv[n-r] % mod


A.sort()
ans = 0
for i, a in enumerate(A):
    M = n-1-i
    m = i
    if m >= 0:
        ans += a*cmb1(m, k-1, mod)
    if M >= 0:
        ans -= a*cmb1(M, k-1, mod)
ans %= mod
print(ans)
# Original solution
#import sys
#input = sys.stdin.readline
Q = 10**9+7
def getInv(N):#Qはmod
    inv = [0] * (N + 1)
    inv[0] = 1
    inv[1] = 1
    for i in range(2, N + 1):
        inv[i] = (-(Q // i) * inv[Q%i]) % Q
    return inv

def getFactorialInv(N):
    inv = [0] * (N + 1)
    inv[0] = 1
    inv[1] = 1
    ret = [1]*(N+1)
    for i in range(2, N + 1):
        inv[i] = (-(Q // i) * inv[Q%i]) % Q
        ret[i] = ret[i-1]*inv[i]
    return ret

def getFactorial(N):
    ret = [1]*(N+1)
    for i in range(2,N+1):
        ret[i] = ret[i-1]*i%Q
    return ret

def main():

    N, K = list(map( int, input().split()))
    if K == 1:
        print((0))
        return
    A = list( map( int, input().split()))
    A.sort()
    ans = 0
#    cnt = 1
    fact = getFactorial(N)
    invfact = getFactorialInv(N)
    for i in range(K-1,N):
        # if i < N-1:
        #     if A[i+1] == A[i]:
        #         cnt += 1
        #         continue
        ans += fact[i]*invfact[K-1]%Q*invfact[i-(K-1)]%Q*A[i]%Q
#        print(A[i], fact[i]*invfact[K-1]%Q*invfact[i-(K-1)]%Q*cnt%Q)
        ans %= Q


    A = A[::-1]
    for i in range(K-1,N):
        # if i < N-1:
        #     if A[i+1] == A[i]:
        #         cnt += 1
        #         continue
        ans -= fact[i]*invfact[K-1]%Q*invfact[i-(K-1)]%Q*A[i]%Q
#        print(A[i], fact[i]*invfact[K-1]%Q*invfact[i-(K-1)]%Q*cnt%Q)
        ans %= Q
    print(ans)
    
if __name__ == '__main__':
    main()


# Optimized solution
#import sys
#input = sys.stdin.readline
Q = 10**9+7
# def getInv(N):#Qはmod
#     inv = [0] * (N + 1)
#     inv[0] = 1
#     inv[1] = 1
#     for i in range(2, N + 1):
#         inv[i] = (-(Q // i) * inv[Q%i]) % Q
#     return inv

def main():

    N, K = list(map( int, input().split()))
    if K == 1:
        print((0))
        return
    A = list( map( int, input().split()))
    A.sort()
    ans = 0

    cnt = 1
    for i in range(K-1,N):
        if i >= K:
            cnt *= i*pow(i-(K-1), Q-2, Q)%Q
            cnt %= Q
        ans += cnt*A[i]

        ans -= cnt*A[N-1-i]
        ans %= Q
    print(ans)
    
if __name__ == '__main__':
    main()
# Original solution
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 = list(map(int,input().split()))
inv = [0,1]
for i in range(2,n):
  inv += [inv[mod % i]*(mod - mod//i) % mod]
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)*inv[n-i-1]
if ans < 0:
    ans += mod
print(ans)

# Optimized solution
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 = list(map(int,input().split()))
inv = [0,1]
for i in range(2,n):
  inv += [inv[mod % i]*(mod - mod//i) % mod]
a = list(map(int,input().split()))
a.sort()
ans = 0
c = 1
x = 1
if k == 1:
    print((0))
    exit()
for i in range(n-k+1):
    ans -= a[n-k-i] * c
    ans += a[k+i-n-1] * c
    ans %= mod
    c = c*(i+k)*inv[i+1] % mod
if ans < 0:
    ans += mod
print(ans)
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = int(1e+9 + 7)
a = sorted(a)

kai = [1, 1]
gyaku = [1, 1]

for i in range(2, n+1):
    kai.append(kai[-1]*i % mod)
    gyaku.append(gyaku[-1]*pow(i, mod-2, mod))

answer = 0
for i in range(n-k+1):
    for j in range(i+k-1,n):
        answer = (answer + (a[j] - a[i]) * kai[j-i-1] *gyaku[j-i-1-(k-2)] * gyaku[k-2])%mod

if k == 1:
    print((0))
else:
    print(answer)

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = int(1e+9 + 7)
a = sorted(a)

kai = [1]*(n+1)
gyaku = [1]*(n+1)

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

answer = 0
for i in range(n-k+1):
    for j in range(i+k-1,n):
        answer = (answer + (a[j] - a[i]) * kai[j-i-1] *gyaku[j-i-1-(k-2)] * gyaku[k-2])%mod

if k == 1:
    print((0))
else:
    print(answer)
# Original solution
from functools import lru_cache


@lru_cache(maxsize=2**17, typed=int)
def comb(n, r, p):
    r = min(n - r, r)
    if r == 0:
        return 1
    over = 1
    for i in range(n, n - r, -1):
        over = over * i % p
    under = 1
    for i in range(1, r + 1):
        under = under * i % p
    inv = pow(under, p - 2, p)
    return over * inv % p


def main():
    n, k = list(map(int, input().split()))
    a = sorted(int(i) for i in input().split())
    mod = 10**9 + 7
    f = 0
    for i in range(n - k + 1):
        f = (f + a[k + i - 1] * comb(k - 1 + i, k - 1, mod) % mod) % mod
        f = (f - a[i] * comb(n - 1 - i, k - 1, mod) % mod) % mod
    print(f)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    n, k = list(map(int, input().split()))
    a = sorted(int(i) for i in input().split())
    mod = 10**9 + 7
    comb = [1] * (n - k + 1)
    for i in range(n - k):
        comb[i + 1] = (comb[i] * (k + i) * pow(i + 1, mod - 2, mod)) % mod
    f = 0
    for i in range(n - k + 1):
        f = (f + a[k + i - 1] * comb[i] % mod) % mod
        f = (f - a[i] * comb[-1 - i] % mod) % mod
    print(f)


if __name__ == '__main__':
    main()
# Original solution
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb)
    plus += (A[-i-1] * comb)
print(((plus - minus) % mod))

# Optimized solution
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print(((plus - minus) % mod))
# Original solution
import itertools as it

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()

ans=0
for i in it.combinations([i for i in range(n)], k):
    max_x = a[max(i)]
    min_x = a[min(i)]
    ans += (max_x - min_x)

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


# Optimized solution
class Combination():  # nCr(mod p) #n<=10**6
    def __init__(self, N, MOD):  # cmbの前処理
        self.mod = MOD
        self.FACT = [1, 1]  # 階乗
        self.INV = [0, 1]  # 各iの逆元
        self.FACTINV = [1, 1]  # 階乗の逆元
        for i in range(2, N + 1):
            self.FACT.append((self.FACT[-1] * i) % self.mod)
            self.INV.append(pow(i, self.mod - 2, self.mod))
            self.FACTINV.append((self.FACTINV[-1] * self.INV[-1]) % self.mod)

    def count(self, N, R):  # nCr(mod p) #前処理必要
        if (R < 0) or (N < R):
            return 0
        R = min(R, N - R)
        return self.FACT[N] * self.FACTINV[R] * self.FACTINV[N-R] % self.mod


n, k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
mod = 10 ** 9 + 7

ans = 0
cmb = Combination(n, mod)
for i in range(n):
    cnt = cmb.count(i, k - 1) - cmb.count(n - i - 1, k - 1)
    ans += a[i] * cnt % mod
print((ans % mod))
# Original solution
n,k=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
mod = 10**9+7 # 変える
fac = [1]*n
finv = [1]*n
inv = [0]*n
inv[1] = 1
for i in range(2,n):
    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 comb(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
ans=0
for i in range(n-k+1):
    ans+=comb(n-i-1,k-1)*(a[n-i-1]-a[i])
print((ans%mod))

# Optimized solution
n,k=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
mod = 10**9+7 # 変える
fac = [1]*n
finv = [1]*n
inv = [0]*n
inv[1] = 1
for i in range(2,n):
    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 comb(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
ans=0
for i in range(n-k+1):
    ans=(ans+comb(n-i-1,k-1)*(a[n-i-1]-a[i]))%mod
print(ans)
# Original solution
# -*- coding: utf-8 -*-
# E

import sys
from functools import reduce
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

mod = 10**9 + 7

A.sort()
# print(A)


def cmb(n, r):
    from operator import mul
    from functools import reduce

    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, list(range(n, n - r, -1)))
    under = reduce(mul, list(range(1, r + 1)))
    return over // under


ans = 0
num1=None
num2=None

for i in range(N):
    if N - i - 1 >= K-1:
        if num1 is None:
            num1 = cmb(N-i-1, K-1) % mod
        else:
            num1 = int(num1 * ((N-i-K+1) % mod) * pow(N-i, mod-2, mod))
        # print(N-i-1, K-1, num1)

        if A[i] * num1 < 0:
            ans += (-A[i] * num1) % mod
        else:
            ans -= (A[i] * num1) % mod
        # print(f'-: {i}, {num}, {A[i]}')
    if i >= K-1:
        if num2 is None:
            num2 = cmb(i, K-1) % mod
        else:
            num2 = num2 * (i % mod) * pow(i-K+1, mod-2, mod)
        # print(i, K-1, num2)

        if A[i] * num2 < 0:
            ans -= (-A[i] * num2) % mod
        else:
            ans += (A[i] * num2) % mod
        # print(f'+: {i}, {num}, {A[i]}')

print((ans % mod))


# Optimized solution
# -*- coding: utf-8 -*-
# E

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
mod = 10**9 + 7


class Cmb(object):
    def cmb_precalc(self, n, r, mod: int = 10 ** 9 + 7):
        if (r < 0) or (r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % mod

    def cmb_prep(self, N: int, mod: int = 10 ** 9 + 7):
        self.g1 = [1, 1]  # 元テーブル
        self.g2 = [1, 1]  # 逆元テーブル
        inverse = [0, 1]  # 逆元テーブル計算用テーブル

        for i in range(2, N + 1):
            self.g1.append((self.g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            self.g2.append((self.g2[-1] * inverse[-1]) % mod)


ans = 0
cmb = Cmb()
cmb.cmb_prep(N + 5)

for i in range(N):
    if i >= K-1:
        ans += A[i] * cmb.cmb_precalc(i, i-K+1)
    if N - i - 1 >= K-1:
        ans -= A[i] * cmb.cmb_precalc(N-i-1, K-1)

print((ans % mod))
# Original solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
mod = 10**9+7
import itertools
ans = 0
for a in itertools.combinations(A,K):
  ans += (max(a)-min(a))%mod
  ans %= mod
  
print(ans)

# Optimized solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
fact_l=[1]*(N+10)
mod = 10**9+7
for i in range(2,N+1):
  fact_l[i]=(fact_l[i-1]*i)%mod
  
fact_inv_l = [1]*(N+10)
for i in range(2,N+1):
  fact_inv_l[i]=pow(fact_l[i],mod-2,mod)
ans = 0
for i in range(1,N-K+2):
  b = N-i
  c = K-1
  ans -= A[i-1]*fact_l[b]%mod*fact_inv_l[b-c]%mod*fact_inv_l[c]%mod
  ans += A[-i]*fact_l[b]%mod*fact_inv_l[b-c]%mod*fact_inv_l[c]%mod
    
print((ans%mod))
# Original solution
def factorial(n, p):
    ret = 1
    for i in range(n, 0, -1):
        ret *= i
        ret %= p
    return ret


def comb(n, r, p):
    r = min(r, n - r)
    a = factorial(r, p)
    b = factorial(n - r, p)
    ret = factorial(n, p) * pow(a, p - 2, p) * pow(b, p - 2, p) % p
    return ret


mod = 10 ** 9 + 7
n, k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
ans = 0
sum_min = 0
for i in range(n - k + 1):
    cnt = comb(n - 1 - i, k - 1, mod)
    sum_min += a[i] * cnt
    sum_min %= mod

a_rev = a[::-1]
sum_max = 0
for i in range(n - k + 1):
    cnt = comb(n - 1 - i, k - 1, mod)
    sum_max += a_rev[i] * cnt
    sum_max %= mod
ans = (sum_max - sum_min) % mod
print(ans)


# Optimized solution
def cmb(n, r, p):
    if r < 0 or n < r:
        return 0
    r = min(r, n - r)
    return fact[n] * fact_inv[r] * fact_inv[n - r] % p


n, k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())), reverse=True)
p = 10 ** 9 + 7
fact = [1, 1]  # fact[n] = (n! mod p)
fact_inv = [1, 1]  # fact_inv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # fact_invの計算用

for i in range(2, n + 1):
    fact.append(fact[-1] * i % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    fact_inv.append(fact_inv[-1] * inv[-1] % p)

i = 0
sum_max = 0
sum_min = 0
while n - i - 1 >= k - 1:
    cnt = cmb(n - i - 1, k - 1, p)
    sum_max += a[i] * cnt
    sum_min += a[n - i - 1] * cnt
    # print(i, a[i] * cnt, a[n - i - 1] * cnt)
    i += 1
ans = (sum_max - sum_min) % p
print(ans)