CodeEff/ECCO huggingface dataset:
# Original solution
from math import factorial
MOD = 10**9 + 7

# 階乗を用意
fact = [1] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    fact[i] = fact[i-1] * i
    fact[i] %= MOD

def comb(n, k, p):
    """power_funcを用いて(nCk) mod p を求める"""
    global fact
    if n < 0 or k < 0 or n < k:
        return 0
    if n == 0 or k == 0:
        return 1
    a = fact[n]
    b = fact[k]
    c = fact[n-k]
    return (a*power_func(b, p-2, p)*power_func(c, p-2, p)) % p

def power_func(a, b, p):
    """a^b mod p を求める"""
    if b == 0:
        return 1
    if b % 2 == 0:
        d = power_func(a, b//2, p)
        return d*d % p
    if b % 2 == 1:
        return (a*power_func(a, b-1, p)) % p

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


a.sort()

ans = 0

# pos
for i in range(1, n):
    ans += a[i] * comb(i, k-1, MOD)
    ans %= MOD

for i in range(n-1):
    ans -= a[i] * comb(n-1-i, k-1, MOD)
    ans %= MOD
print(ans)



# Optimized solution
from math import factorial
MOD = 10**9 + 7

# 階乗を用意
fact = [1] * (10**5 + 1)
for i in range(1, 10**5 + 1):
    fact[i] = fact[i-1] * i
    fact[i] %= MOD

def comb(n, k, p):
    """power_funcを用いて(nCk) mod p を求める"""
    global fact
    if n < 0 or k < 0 or n < k:
        return 0
    if n == 0 or k == 0:
        return 1
    a = fact[n]
    b = fact[k]
    c = fact[n-k]
    return (a*power_func(b, p-2, p)*power_func(c, p-2, p)) % p

def power_func(a, b, p):
    """a^b mod p を求める"""
    if b == 0:
        return 1
    if b % 2 == 0:
        d = power_func(a, b//2, p)
        return d*d % p
    if b % 2 == 1:
        return (a*power_func(a, b-1, p)) % p

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

ans = 0

# pos
for i in range(n-1):
    c = comb(n-1-i, k-1, MOD)
    ans += a[n-1-i] * c
    ans %= MOD
    ans -= a[i] * c
    ans %= MOD
print(ans)
# Original solution
mod = int(1e9+7)

fac = [1]

def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c

def subtract(a, b):
    return (a - b) % mod

def mul(a, b):
    return (a * b) % mod

def my_pow(a, b):
    if b == 0:
        return 1
    if b % 2 == 1:
        return mul(a, my_pow(a, b-1))
    else:
        temp = my_pow(a, b/2)
        return temp * temp
    
def my_inv(a):
    return my_pow(a, mod-2)

def nCk(n, k, fac):
    if k > n:
        return 0
    #print(fac[n])
    #print(my_inv(mul(fac[n-k], fac[k])))
    return mul(fac[n], my_inv(mul(fac[n-k], fac[k])))

def main():
    n, k = [int(x) for x in input().split()]
    
    a = [int(x) for x in input().split()]
    
    fac = [0 for _ in range(2*n+1)]
    
    fac[0] = 1
    
    for i in range(1, 2*n+1):
        fac[i] = fac[i-1] * i
    
    a.sort()
    
    ans = 0
    
    for i in range(n-1):
        #print(a[i])
        diff = subtract(a[i+1], a[i])
        ways = nCk(n, k, fac)
        #print(ways)
        ways = subtract(ways, nCk(i+1, k, fac))
        ways = subtract(ways, nCk(n-i-1, k, fac))
        ans = add(ans, mul(diff, ways))
    
    print(ans)
    

main()

# Optimized solution
mod = int(1e9+7)

fac = [1]

def add(a, b):
    c = a + b
    if c >= mod:
        c -= mod
    return c

def subtract(a, b):
    return (a - b) % mod

def mul(a, b):
    return (a * b) % mod

def my_pow(a, b):
    if b == 0:
        return 1
    if b % 2 == 1:
        return mul(a, my_pow(a, b-1))
    else:
        temp = my_pow(a, b/2)
        return temp * temp
    
def my_inv(a):
    return my_pow(a, mod-2)

def nCk(n, k, fac):
    if k > n:
        return 0
    #print(fac[n])
    #print(my_inv(mul(fac[n-k], fac[k])))
    return mul(fac[n], my_inv(mul(fac[n-k], fac[k])))

def main():
    n, k = [int(x) for x in input().split()]
    
    a = [int(x) for x in input().split()]
    
    fac = [0] * (n+5)
    
    #print(fac)
    
    fac[0] = 1
    
    for i in range(1, n+5):
        fac[i] = mul(fac[i-1], i)
    
    a.sort()
    
    ans = 0
    
    for i in range(n-1):
        #print(a[i])
        diff = subtract(a[i+1], a[i])
        ways = nCk(n, k, fac)
        #print(ways)
        ways = subtract(ways, nCk(i+1, k, fac))
        ways = subtract(ways, nCk(n-i-1, k, fac))
        ans = add(ans, mul(diff, ways))
    
    print(ans)
    

main()
# Original solution
from operator import mul
from functools import reduce

def cmb(n,r):
    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
n,k = list(map(int,input().split()))
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1)
    ans %= 10 ** 9+7
print(ans)

# Optimized solution
import sys
sys.setrecursionlimit(1000000)
def cmb(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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = list(map(int,input().split()))
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)
# Original solution
from itertools import combinations
n,k = list(map(int,input().split()))
a = [int(i) for i in input().split()]
a.sort()
mod = 10**9+7
ans = 0
for i in combinations(a,k):
    ans += i[k-1] - i[0]
    ans %= mod
print(ans)

# Optimized solution
n,k = list(map(int,input().split()))
a = [int(i) for i in input().split()]
a.sort()
mod = 10**9+7
fac = [0]*(n+1)
finv = [0]*(n+1)
def comb(n,k):
    if n < k: return 0
    if n < 0 or k < 0: return 0
    return fac[n]*finv[k]%mod*finv[n-k]%mod
fac[0] = finv[0] = 1
for i in range(1,n+1):
    fac[i] = fac[i-1]*i%mod
    finv[i] = pow(fac[i],mod-2,mod)
smin = 0
smax = 0
for i in range(n):
    smin += (a[i]*comb(n-i-1,k-1))%mod
    smin %= mod
    smax += (a[n-1-i]*comb(n-i-1,k-1))%mod
    smax %= mod
ans = (smax-smin)%mod
print(ans)
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from bisect import bisect_left


def comb(n, k):
    if n < 0 or n < k or k < 0:
        return 0
    return fac[n] * ifac[k] * ifac[n - k] % mod


n, k, *a = list(map(int, read().split()))
a.sort()
mod = 10 ** 9 + 7
max_n = 2 * 10 ** 5 + 1
ans = 0
fac = [1] * max_n
inv = [1] * max_n
ifac = [1] * max_n
for i in range(2, max_n):
    fac[i] = fac[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    ifac[i] = ifac[i - 1] * inv[i] % mod
for i, aa in enumerate(a):
    ans += aa * (comb(i, k - 1) - comb(n - 1 - i, k - 1))
    ans %= mod
print(ans)


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)


def comb(n, k):
    if n < 0 or n < k or k < 0:
        return 0
    return fac[n] * ifac[k] * ifac[n - k] % mod


n, k, *a = list(map(int, read().split()))
a.sort()
mod = 10 ** 9 + 7
ans = 0
fac = [1] * n
inv = [1] * n
ifac = [1] * n
for i in range(2, n):
    fac[i] = fac[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    ifac[i] = ifac[i - 1] * inv[i] % mod
for i, aa in enumerate(a):
    ans += aa * (comb(i, k - 1) - comb(n - 1 - i, k - 1))
    ans %= mod
print(ans)
# Original solution
def combi(x, y):
    if x < y:
        return 0
    if (x,y) in memo:
        return memo[(x,y)]
    else:
        memo[(x,y)]  = math.factorial(x) // (math.factorial(x - y) * math.factorial(y))
        return memo[(x,y)]
import math
memo = dict()
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
mod = 10**9+7
ans = 0
for i in range(n):
    #今回の数字が、最小値として数えられる数の合計
    ans -= a[i] * combi(n - i - 1, k-1)
    #今回の数字が、最大値として数えられる数の合計
    ans += a[i] * combi(i,k-1)
    ans %= mod
print((int(ans)))

# Optimized solution
mod = 10**9+7
memo = dict()
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
a.sort()
factorial = [1 for i in range(n+1+k)]
for i in range(1,n+1+k):
    factorial[i] = (factorial[i-1] * i) % mod
#互いに素なa,bについて、a*x+b*y=1の一つの解
def extgcd(a,b):
    r = [1,0,a]
    w = [0,1,b]
    while w[2]!=1:
        q = r[2]//w[2]
        r2 = w
        w2 = [r[0]-q*w[0],r[1]-q*w[1],r[2]-q*w[2]]
        r = r2
        w = w2
    #[x,y]
    return [w[0],w[1]]

# aの逆元(mod m)を求める。(aとmは互いに素であることが前提)
#a/b = a*(b`)となるb`を求める
def mod_inv(a,mod):
    x = extgcd(a,mod)[0]
    return (mod+x%mod)%mod
def mod_combination(n,k,mod):
    if n < k:
        return 0
    return factorial[n] * mod_inv(factorial[n-k], mod) * mod_inv(factorial[k], mod)
ans = 0
for i in range(n):
    #今回の数字が、最小値として数えられる数の合計
    ans -= a[i] * mod_combination(n - i - 1, k-1,mod)
    #今回の数字が、最大値として数えられる数の合計
    ans += a[i] * mod_combination(i,k-1,mod)
    ans %= mod
print((int(ans)))
# Original solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
Mod = 10**9 + 7
a.sort()
c = [0] * (n+1)
c[k - 1] = 1
for i in range(n+1):
    if i >= k:
        c[i] = (c[i - 1]*i) // (i - k + 1)
ans = 0
for i in range(1, n + 1):
    ans += (a[i - 1] * (c[i - 1] - c[n - i])) % Mod
print((ans % Mod))

# Optimized solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
Mod = 10**9 + 7
a.sort()
c = [0] * (n+1)
c[k - 1] = 1
for i in range(n+1):
    if i >= k:
        c[i] = ((c[i - 1]*i * pow(i - k + 1, Mod - 2, Mod))) % Mod
ans = 0
for i in range(1, n + 1):
    ans += (a[i - 1] * (c[i - 1] - c[n - i])) % Mod
print((ans % Mod))
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))


mod=pow(10,9)+7

def cmb(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
N = n
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 )

from collections import Counter
cnt_a=Counter(a)
list_a=list(cnt_a.keys())
list_a.sort()
num_a=len(list_a)
sa_=0
sa=[0]
for ai in list_a:
    sa_+=cnt_a[ai]
    sa.append(sa_)

ans=0
for i in range(num_a): 
    for j in range(i,num_a):# list_a[i]をmin,list_a[j]をmaxとしてk個選べるか
        k_=sa[j+1]-sa[i]
        if k_>=k:
            mj=0 if k_-cnt_a[list_a[j]]<k else cmb(k_-cnt_a[list_a[j]],k,mod)
            mi=0 if k_-cnt_a[list_a[i]]<k else cmb(k_-cnt_a[list_a[i]],k,mod)
            mij=0 if k_-cnt_a[list_a[j]]-cnt_a[list_a[i]]<k else cmb(k_-cnt_a[list_a[j]]-cnt_a[list_a[i]],k,mod)
            ans+=(list_a[j]-list_a[i])*(cmb(k_,k,mod)-mj-mi+mij)
            #print(list_a[j],list_a[i],cmb(k_,k,mod),mj,mi)
            ans%=mod
            #f(x)の値×list_a[i]をmin,list_a[j]をmaxとしてk個選ぶ選び方
print(ans)


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


mod=pow(10,9)+7

def cmb(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
N = n
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 )

from collections import Counter
cnt_a=Counter(a)
list_a=list(cnt_a.keys())
list_a.sort()
num_a=len(list_a)
sa_=0
sa=[0]
for ai in list_a:
    sa_+=cnt_a[ai]
    sa.append(sa_)
ansmin=0
ansmax=0
for i in range(num_a):# list_a[i]がminになる場合の数
    k_=sa[num_a]-sa[i]
    if k_>=k:
        mi=0 if k_-cnt_a[list_a[i]]<k else cmb(k_-cnt_a[list_a[i]],k,mod)
        ansmin+=list_a[i]*(cmb(k_,k,mod)-mi)
        ansmin%=mod
for i in range(num_a):# list_a[i]がmaxになる場合の数
    k_=sa[i+1]
    if k_>=k:
        mi=0 if k_-cnt_a[list_a[i]]<k else cmb(k_-cnt_a[list_a[i]],k,mod)
        ansmax+=list_a[i]*(cmb(k_,k,mod)-mi)
        ansmax%=mod
print(((ansmax-ansmin)%mod))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort(reverse = True)

N = n+2
P = mod
inv_t = [0]+[1]
for i in range(2,N):
  inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

base = 1
for item in range(n-k+1, n):
    base = (base * item) % mod
for i in range(1, k):
    base = (base * inv_t[i]) % mod
base2 = base

ans = 0
ans2  = 0
top = n - 1
for ai in a:
    if top < k-1:
        break
    if ai > 0:
        ans += base * ai
        ans %= mod
    else:
        ans2 += abs(base * ai)
        ans2 %= mod

    base = base * inv_t[top] * (top - k + 1)  % mod
    top -= 1

top = n-1
for ai in reversed(a):
    if top < k-1:
        break
    if ai > 0:
        ans2 += base2 * ai
        ans2 %= mod
    else:
        ans += abs(base2 * ai)
        ans %= mod
    base2 = base2 * inv_t[top] * (top - k + 1)  % mod
    top -= 1
print(((ans - ans2) % mod))


# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
a.sort(reverse = True)

N = n+2
P = mod
inv_t = [0]+[1]
for i in range(2,N):
  inv_t += [inv_t[P % i] * (P - int(P / i)) % P]

base = 1
for item in range(n-k+1, n):
    base = (base * item) % mod
for i in range(1, k):
    base = (base * inv_t[i]) % mod
base2 = base

ans = 0
top = n - 1
for ai in a:
    if top < k-1:
        break
    ans += base * ai
    ans %= mod

    base = base * inv_t[top] * (top - k + 1)  % mod
    top -= 1

top = n-1
for ai in reversed(a):
    if top < k-1:
        break
    ans -= base2 * ai
    ans %= mod
    base2 = base2 * inv_t[top] * (top - k + 1)  % mod
    top -= 1
print((ans % mod))
# Original solution
def comb(n,r,mod):
	'''
	return nCr
	O(min{n-r, r})
	'''
	a=1
	b=1
	r = min(r,n-r)
	for i in range(r):
		a = a*(n-i)%mod
		b = b*(i+1)%mod
	return a*pow(b,mod-2,mod)%mod


N,K = list(map(int,input().split()))
A = [int(x) for x in input().split()]
A.sort()

mod = 10**9+7

l = [[A[0], comb(N-1, K-1, mod=mod)]]
r = [[A[-1], comb(N-1, K-1, mod=mod)]]


for i in range(1,N):
	if N-i-1>=K-1:
		l.append([A[i], comb(N-i-1, K-1,mod=mod)])
for i in range(N-1)[::-1]:
	if i>=K-1:
		r.append([A[i],comb(i, K-1,mod=mod)])


s = 0
t = 0

for i,j in l:
	s = (s+(i*j)%mod)%mod

for i,j in r:
	t = (t+(i*j)%mod)%mod

ans = (t-s)%mod
if ans < 0:
	ans += mod
print(ans)


# Optimized solution


N,K = list(map(int,input().split()))
A = [int(x) for x in input().split()]
A.sort()

mod = 10**9+7


l = 0
r = 0
t = 1
cnt = 1
for i in range(N-K+1)[::-1]:
	l = (l+(t*A[i])%mod)%mod
	t = (t*(cnt+K-1))%mod
	t = (t*pow(cnt,mod-2, mod))%mod
	cnt+=1
t = 1
cnt = 1
for i in range(K-1, N):
	r = (r+(t*A[i])%mod)%mod
	t = (t*(cnt+K-1))%mod
	t = (t*pow(cnt,mod-2, mod))%mod
	cnt+=1


ans = (r-l)%mod
if ans < 0:
	ans += mod
print(ans)
# Original solution
import math
n,k = list(map(int,input().split()))
als = list(map(int,input().split()))
als.sort()
# n = 10**5
# k = 5*(10**4) 
mod = 10**9+7
ans = 0
# def nCr(n,r):
#     mod = (10**9)+7
#     fac = [1,1]+[0]*n
#     finv = [1,1]+[0]*n
#     inv = [1,1]+[0]*n
#     for i in range(2,n+1):
#         fac[i] = fac[i-1] * i % mod
#         inv[i] = mod - inv[mod%i] * (mod//i) % mod
#         finv[i] = finv[i-1] * inv[i] % mod
#     ans = fac[n] * (finv[r] * finv[n-r] % mod) % mod
    
#     return ans

fac = [1,1]+[0]*n
finv = [1,1]+[0]*n
inv = [1,1]+[0]*n
for i in range(2,n+1):
    fac[i] = fac[i-1] * i % mod
    inv[i] = mod - inv[mod%i] * (mod//i) % mod
    finv[i] = finv[i-1] * inv[i] % mod

for i in range(n):
    if i >= k-1:
        ans += als[i] * fac[i] * (finv[(k-1)] * finv[i-(k-1)] % mod) % mod
        ans = ans % mod
    if i <= n-k:
        ans -= als[i] * fac[(n-i-1)] * (finv[(k-1)] * finv[(n-i-1)-(k-1)] % mod) % mod
        ans = ans % mod

print(ans)
        

# Optimized solution
def nCr(n,r,fac,finv):
    mod = (10**9)+7
    ans = fac[n] * (finv[r] * finv[n-r] % mod) % mod
    return ans

n,k = list(map(int,input().split()))
als = list(map(int,input().split()))
als.sort()
ans = 0
mod = (10**9)+7
fac = [1,1]+[0]*n
finv = [1,1]+[0]*n
inv = [1,1]+[0]*n
for i in range(2,n+1):
    fac[i] = fac[i-1] * i % mod
    inv[i] = mod - inv[mod%i] * (mod//i) % mod
    finv[i] = finv[i-1] * inv[i] % mod

for i in range(n):
    if i >= k-1:
        ans += als[i] * nCr(i,k-1,fac,finv) % mod
        ans = ans % mod
    if i <= n-k:
        ans -= als[i] * nCr(n-i-1,k-1,fac,finv) % mod
        ans = ans % mod

print(ans)
# Original solution
def modinv2(a,m):
    return pow(a,m-2,m)

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

cominv = [0 for i in range(N-1)]
for i in range(N-1):
    if i+2 == K:
        cominv[i] = 1
    elif i+1 == K:
        cominv[i] = (i+1)%P
    elif i+1 > K:
        a = modinv2(i-K+2,P)
        cominv[i] = cominv[i-1]*(i+1)*a%P

#print(cominv)
ans = 0

for i in range(N):
    if i <= N-K:
        b = A[i]*cominv[N-i-2]%P
        ans = (ans-b+P)%P
    if i >= K-1:
        c = A[i]*cominv[i-1]%P
        ans = (ans+c)%P

print(ans)


# Optimized solution
def modinv2(a,m):
    return pow(a,m-2,m)

def modcomn(n,r,m):
    modcom = [0 for _ in range(n)]
    for i in range(n):
        if i < r-1:
            continue
        if i == r-1:
            modcom[i] = 1
        elif i == r:
            modcom[i] = (i+1)%m
        else:
            div = modinv2(i-r+1,m)
            modcom[i] = (modcom[i-1]*div%m)*(i+1)%m
    return modcom

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

com = modcomn(N-1,K-1,P)

#print(com)
ans = 0

for i in range(N):
    if i >= K-1:
        ans = (ans+A[i]*com[i-1]%P)%P
    if i <= N-K:
        ans = (ans-A[i]*com[N-i-2]%P+P)%P

print(ans)
# Original solution
def modinv2(a,m):
    return pow(a,m-2,m)

def modcomn(n,r,m):
    modcom = [0 for _ in range(n)]
    for i in range(n):
        if i < r-1:
            continue
        if i == r-1:
            modcom[i] = 1
        elif i == r:
            modcom[i] = (i+1)%m
        else:
            div = modinv2(i-r+1,m)
            modcom[i] = (modcom[i-1]*div%m)*(i+1)%m
    return modcom

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

com = modcomn(N-1,K-1,P)

#print(com)
ans = 0

for i in range(N):
    if i >= K-1:
        ans = (ans+A[i]*com[i-1]%P)%P
    if i <= N-K:
        ans = (ans-A[i]*com[N-i-2]%P+P)%P

print(ans)


# Optimized solution
def modinv2(a,m):
    return pow(a,m-2,m)

def modcomn(n,r,m):
    modcom = [0 for _ in range(n)]
    for i in range(n):
        if i < r-1:
            continue
        if i == r-1:
            modcom[i] = 1
        elif i == r:
            modcom[i] = (i+1)%m
        else:
            div = modinv2(i-r+1,m)
            modcom[i] = (modcom[i-1]*div%m)*(i+1)%m
    return modcom

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

com = modcomn(N-1,K-1,P)

#print(com)
ans = 0

for i in range(N):
    if i >= K-1:
        ans = (ans+A[i]*com[i-1]%P)%P

A.sort(reverse=True)
for i in range(N):
    if i >= K-1:
        ans = (ans-A[i]*com[i-1]%P+P)%P

print(ans)
# Original solution
n,k=list(map(int, input().split()))
an=[int(i) for i in input().split()]

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

p = 10 ** 9 + 7
N = 10 ** 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)

an=sorted(an)
r_an=list(reversed(an))

max1=0
min1=0
for i in range(1,n-k+2):
    c=cmb(n-i,k-1,p)
    max1=max1 + an[-i]*c%p
    min1=min1 + r_an[-i]*c%p
print(((max1-min1)%p))

# Optimized solution
n,k=list(map(int, input().split()))
an=[int(i) for i in input().split()]

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

mod = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]
factinv = [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)
    factinv.append((factinv[-1] * inv[-1]) % mod)

an=sorted(an)
r_an=list(reversed(an))

max1=0
min1=0
for i in range(1,n-k+2):
    c=cmb(n-i,k-1,mod)
    max1=max1 + an[-i]*c%mod
    min1=min1 + r_an[-i]*c%mod
print(((max1-min1)%mod))
# Original solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

MOD = 10**9+7

MAXN = N+5
fac = [1,1] + [0]*MAXN
finv = [1,1] + [0]*MAXN
inv = [0,1] + [0]*MAXN
for i in range(2,MAXN+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def ncr(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD

combs = []
l = N-K+1
for i in range(l):
    combs.append(ncr(N-l+i,i))

cmax = [0]*(N-l) + combs
cmin = combs[::-1]

from collections import Counter
mins = Counter()
maxs = Counter()

for a,c in zip(A,cmax):
    maxs[a] += c
for a,c in zip(A,cmin):
    mins[a] += c

mx = A[-1]
mn = A[0]
ans = ncr(N,K) * (mx-mn)
for k,v in list(maxs.items()):
    ans -= v * (mx-k)
    ans %= MOD
for k,v in list(mins.items()):
    ans -= v * (k-mn)
    ans %= MOD
print(ans)

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

MAXN = N+5
fac = [1,1] + [0]*MAXN
finv = [1,1] + [0]*MAXN
inv = [0,1] + [0]*MAXN
for i in range(2,MAXN+2):
    fac[i] = fac[i-1] * i % MOD
    inv[i] = -inv[MOD%i] * (MOD // i) % MOD
    finv[i] = finv[i-1] * inv[i] % MOD

def comb(n,r):
    if n < r: return 0
    if n < 0 or r < 0: return 0
    return fac[n] * (finv[r] * finv[n-r] % MOD) % MOD

ans = 0
for n in range(K-1,N):
    c = comb(n,K-1)
    mx = A[n]
    mn = A[-1-n]
    ans += (mx-mn)*c
    ans %= MOD
print(ans)
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k,*alst = list(map(int,read().split()))
alst.sort()
lst1=alst[k-1:]
lst2=alst[n-k::-1]
mod = 10 ** 9 + 7
res=0
prep=1
for i in range(n-k+1):
  res+=prep*(lst1[i]-lst2[i])
  res%=mod
  prep=prep*(i+k)//(i+1)

print(res)


# Optimized solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k,*alst = list(map(int,read().split()))
alst.sort()
lst1=alst[k-1:]
lst2=alst[n-k::-1]
mod = 10 ** 9 + 7
res=0
prep=1
for i in range(n-k+1):
  res+=prep*(lst1[i]-lst2[i])
  res%=mod
  prep=prep*(i+k)*pow(i+1,mod-2,mod)
  prep%=mod

print(res)
# Original solution
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k,*alst = list(map(int,read().split()))
alst.sort()
lst1=alst[k-1:]
lst2=alst[n-k::-1]
mod = 10 ** 9 + 7
res=0
prep=1
for i in range(n-k+1):
  res+=prep*(lst1[i]-lst2[i])
  res%=mod
  prep=prep*(i+k)*pow(i+1,mod-2,mod)
  prep%=mod

print(res)


# Optimized solution
import sys
read = sys.stdin.read

n, k, *alst = list(map(int, read().split()))
alst.sort()
lst1 = alst[k - 1:]
lst2 = alst[n - k::-1]
mod = 10 ** 9 + 7
res = 0
prep = 1
for i in range(n - k + 1):
    res += prep * (lst1[i] - lst2[i])
    res %= mod
    prep = prep * (i + k) * pow(i + 1, mod - 2, mod)
    prep %= mod

print(res)