import numpy as np

import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

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

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

mod = 10**9+7

A.sort()



fact = np.zeros(N+1, np.int64)

inv = np.zeros(N+1, np.int64)



fact[0], fact[1] = 1, 1

inv[0], inv[1] = 1, 1



for i in range(2,N+1):

	fact[i] = fact[i-1] * i

	fact[i] %= mod

	inv[i] = inv[i-1] * pow(i, mod-2, mod)

	inv[i] %= mod



ans = 0

lim = N - K + 1

for ind, a in enumerate(A,1):

	if ind < K:

		tmp_max = 0

	else:

		r_cnt = ind - 1

		r_comb = fact[r_cnt]

		r_comb *= inv[K - 1]

		r_comb %= mod

		r_comb *= inv[r_cnt - K + 1]

		r_comb %= mod



		tmp_max= r_comb * a % mod



	if ind > lim:

		tmp_min = 0

	else:

		l_cnt = N - ind

		l_comb = fact[l_cnt]

		l_comb *= inv[K - 1]

		l_comb %= mod

		l_comb *= inv[l_cnt - K + 1]

		l_comb %= mod



		tmp_min = l_comb * a % mod



	ans += (tmp_max - tmp_min) % mod

	ans %= mod



ans %= mod

print(ans)





	
import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

import numpy as np



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

A = np.array(list(map(int, input().split())), dtype = np.int64)



MOD = 10**9+7

def cumprod(x, MOD = MOD):

	L = len(x)

	Lsq = int(L**.5 + 1)

	x = np.resize(x, Lsq ** 2).reshape(Lsq, Lsq)

	for n in range(1, Lsq):

		x[:,n] *= x[:,n-1]

		x[:,n] %= MOD

	for n in range(1,Lsq):

		x[n] *= x[n-1, -1]

		x[n] %= MOD

	return x.flatten()[:L]



def make_fact(U, MOD = MOD):

	x = np.arange(U, dtype = np.int64)

	x[0] = 1

	fact = cumprod(x, MOD)

	x = np.arange(U, 0, -1, dtype = np.int64)

	x[0] = pow(int(fact[-1]), MOD - 2, MOD)

	fact_inv = cumprod(x, MOD)[::-1]

	return fact, fact_inv



fact, inv = make_fact(N+100, MOD)

A.sort()

ans = 0

lim = N - K + 1

for ind, a in enumerate(A, 1):

	if ind < K:

		tmp_max = 0

	else:

		max_comb = (fact[ind-1] * inv[K-1]) % MOD * inv[ind-K] % MOD

		tmp_max = max_comb * a % MOD

	if ind > lim:

		tmp_min = 0

	else:

		min_comb = (fact[N - ind] * inv[K-1]) % MOD * inv[N - ind - K + 1] % MOD

		tmp_min = min_comb * a % MOD



	ans += (tmp_max - tmp_min) % MOD

	ans %= MOD



print(ans)
import numpy as np
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
N, K = list(map(int, input().split()))
A = np.array(list(map(int, input().split())))
mod = 10**9 + 7
A.sort()
fact = np.zeros(N + 1, np.int64)
inv = np.zeros(N + 1, np.int64)
fact[0], fact[1] = 1, 1
inv[0], inv[1] = 1, 1
for i in range(2, N + 1):
    fact[i] = fact[i - 1] * i
    fact[i] %= mod
    inv[i] = inv[i - 1] * pow(i, mod - 2, mod)
    inv[i] %= mod
ans = 0
lim = N - K + 1
for ind, a in enumerate(A, 1):
    if ind < K:
        tmp_max = 0
    else:
        r_cnt = ind - 1
        r_comb = fact[r_cnt]
        r_comb *= inv[K - 1]
        r_comb %= mod
        r_comb *= inv[r_cnt - K + 1]
        r_comb %= mod
        tmp_max = r_comb * a % mod
    if ind > lim:
        tmp_min = 0
    else:
        l_cnt = N - ind
        l_comb = fact[l_cnt]
        l_comb *= inv[K - 1]
        l_comb %= mod
        l_comb *= inv[l_cnt - K + 1]
        l_comb %= mod
        tmp_min = l_comb * a % mod
    ans += (tmp_max - tmp_min) % mod
    ans %= mod
ans %= mod
print(ans)

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np

N, K = list(map(int, input().split()))
A = np.array(list(map(int, input().split())), dtype=np.int64)
MOD = 10**9 + 7


def cumprod(x, MOD=MOD):
    L = len(x)
    Lsq = int(L**0.5 + 1)
    x = np.resize(x, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        x[:, n] *= x[:, n - 1]
        x[:, n] %= MOD
    for n in range(1, Lsq):
        x[n] *= x[n - 1, -1]
        x[n] %= MOD
    return x.flatten()[:L]


def make_fact(U, MOD=MOD):
    x = np.arange(U, dtype=np.int64)
    x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64)
    x[0] = pow(int(fact[-1]), MOD - 2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv


fact, inv = make_fact(N + 100, MOD)
A.sort()
ans = 0
lim = N - K + 1
for ind, a in enumerate(A, 1):
    if ind < K:
        tmp_max = 0
    else:
        max_comb = (fact[ind - 1] * inv[K - 1]) % MOD * inv[ind - K] % MOD
        tmp_max = max_comb * a % MOD
    if ind > lim:
        tmp_min = 0
    else:
        min_comb = (fact[N - ind] * inv[K - 1]) % MOD * inv[N - ind - K + 1] % MOD
        tmp_min = min_comb * a % MOD
    ans += (tmp_max - tmp_min) % MOD
    ans %= MOD
print(ans)

-import numpy as np +import numpy as np + -A = np.array(list(map(int, input().split()))) -mod = 10**9 + 7 +A = np.array(list(map(int, input().split())), dtype=np.int64) +MOD = 10**9 + 7 + + +def cumprod(x, MOD=MOD): +    L = len(x) +    Lsq = int(L**0.5 + 1) +    x = np.resize(x, Lsq**2).reshape(Lsq, Lsq) +    for n in range(1, Lsq): +        x[:, n] *= x[:, n - 1] +        x[:, n] %= MOD +    for n in range(1, Lsq): +        x[n] *= x[n - 1, -1] +        x[n] %= MOD +    return x.flatten()[:L] + + +def make_fact(U, MOD=MOD): +    x = np.arange(U, dtype=np.int64) +    x[0] = 1 +    fact = cumprod(x, MOD) +    x = np.arange(U, 0, -1, dtype=np.int64) +    x[0] = pow(int(fact[-1]), MOD - 2, MOD) +    fact_inv = cumprod(x, MOD)[::-1] +    return fact, fact_inv + + +fact, inv = make_fact(N + 100, MOD) -fact = np.zeros(N + 1, np.int64) -inv = np.zeros(N + 1, np.int64) -fact[0], fact[1] = 1, 1 -inv[0], inv[1] = 1, 1 -for i in range(2, N + 1): -    fact[i] = fact[i - 1] * i -    fact[i] %= mod -    inv[i] = inv[i - 1] * pow(i, mod - 2, mod) -    inv[i] %= mod -        r_cnt = ind - 1 -        r_comb = fact[r_cnt] -        r_comb *= inv[K - 1] -        r_comb %= mod -        r_comb *= inv[r_cnt - K + 1] -        r_comb %= mod -        tmp_max = r_comb * a % mod +        max_comb = (fact[ind - 1] * inv[K - 1]) % MOD * inv[ind - K] % MOD +        tmp_max = max_comb * a % MOD -        l_cnt = N - ind -        l_comb = fact[l_cnt] -        l_comb *= inv[K - 1] -        l_comb %= mod -        l_comb *= inv[l_cnt - K + 1] -        l_comb %= mod -        tmp_min = l_comb * a % mod -    ans += (tmp_max - tmp_min) % mod -    ans %= mod -ans %= mod +        min_comb = (fact[N - ind] * inv[K - 1]) % MOD * inv[N - ind - K + 1] % MOD +        tmp_min = min_comb * a % MOD +    ans += (tmp_max - tmp_min) % MOD +    ans %= MOD