CodeEff/ECCO huggingface dataset:
# Original solution
import sys,bisect
input = sys.stdin.readline

n,k = list(map(int,input().split()))
a = list(map(int,input().split()))
mod = 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.sort()

mi = 0
for i in range(n-k+1):
    mi = (mi + a[i]*mod_comb_k(n-i-1,k-1,mod)%mod)%mod

a = a[::-1]
ma = 0
for i in range(n-k+1):
    ma = (ma + a[i]*mod_comb_k(n-i-1,k-1,mod)%mod)%mod


print(((ma-mi)%mod))

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline
    
    n,k = list(map(int,input().split()))
    a = list(map(int,input().split()))
    mod = 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.sort()
    
    res = 0
    for i in range(n-k+1):
        res = (res + (a[n-1-i]- a[i])*mod_comb_k(n-i-1,k-1,mod)%mod)%mod

    print(res)
if __name__ == '__main__':
    main()
# Original solution
def main():
    import sys
    input = sys.stdin.readline
    
    n,k = list(map(int,input().split()))
    a = list(map(int,input().split()))
    mod = 10**9+7
    
    fact = [1]
    fact_inv = [1]
    for i in range(10**5+5):
        fact.append(fact[-1]*(i+1)%mod)
        fact_inv.append(pow(fact[-1],mod-2,mod))
        
    def mod_comb_k(n,k,mod):
        return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod
    
    a.sort()
    
    res = 0
    for i in range(n-k+1):
        res = (res + (a[n-1-i]- a[i])*mod_comb_k(n-i-1,k-1,mod)%mod)%mod

    print(res)
if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import sys
    input = sys.stdin.readline
    
    n,k = list(map(int,input().split()))
    a = list(map(int,input().split()))
    mod = 10**9+7
    
    fact = [1]
    fact_inv = [1]
    for i in range(10**5+5):
        fact.append(fact[-1]*(i+1)%mod)
        fact_inv.append(pow(fact[-1],mod-2,mod))
        
    def mod_comb_k(n,k,mod):
        return fact[n] * fact_inv[k] % mod * fact_inv[n-k] %mod
    
    a.sort()
    
    res = 0
    for i in range(n-k+1):
        res = (res + (a[n-1-i]- a[i])*mod_comb_k(n-i-1,k-1,mod))%mod

    print(res)
if __name__ == '__main__':
    main()
# Original solution
import math

def nCr(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

a.sort()
a_reverse = a[::-1]

plus = 0
minus = 0
cnt = 0
ind = 0
# ceil = nCr(n, k)
kaisu = nCr(n, k-1)
# while cnt < ceil:
for _ in range(n-k+1):
    kaisu = kaisu * ((n-ind)-(k-1)) // (n-ind)
    cnt += kaisu
    plus = (plus + (kaisu * a_reverse[ind]) % mod) % mod
    minus = (minus + (kaisu * a[ind]) % mod) % mod
    ind += 1

print(((plus-minus) % mod))

# Optimized solution
n,k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
a_reverse = a[::-1]
mod = 10**9+7
 
factorial = [1]
inverse = [1]
for i in range(1, n+2):
    factorial.append(factorial[-1] * i % mod)
    inverse.append(pow(factorial[-1], mod - 2, mod))
 
def comb(n, r, mod):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % mod

plus = 0
minus = 0
cnt = 0
ind = 0
for i in range(n-k+1):
    kaisu = comb(n-i-1, k-1, mod)
    plus = (plus + (kaisu * a_reverse[ind]) % mod) % mod
    minus = (minus + (kaisu * a[ind]) % mod) % mod
    ind += 1

print(((plus-minus) % mod))
# Original solution
n,k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
a_reverse = a[::-1]
mod = 10**9+7
 
factorial = [1]
inverse = [1]
for i in range(1, n+1):
    factorial.append(factorial[-1] * i % mod)
    inverse.append(pow(factorial[-1], mod - 2, mod))
 
def comb(n, r, mod):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % mod

plus = 0
minus = 0
cnt = 0
ind = 0
for i in range(n-k+1):
    kaisu = comb(n-i-1, k-1, mod)
    plus = (plus + (kaisu * a_reverse[ind]) % mod) % mod
    minus = (minus + (kaisu * a[ind]) % mod) % mod
    ind += 1

print(((plus-minus) % mod))

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

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

def comb(n, r, mod):
    if n < r or r < 0: return 0
    elif r == 0: return 1
    return factorial[n] * inverse[r] * inverse[n - r] % mod

a.sort()
mini = 0
for i in range(n-1):
    mini += a[i] * comb(n-i-1, k-1, mod)
    mini %= mod

a.sort(reverse=True)
maxi = 0
for i in range(n-1):
    maxi += a[i] * comb(n-i-1, k-1, mod)
    maxi %= mod

print(((maxi-mini)%mod))
# Original solution
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

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)

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


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

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

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)

n,k = list(map(int,input().split()))
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        maxs = a[i]
        ncr = cmb(i, k-1)
        #print(maxs, ncr, maxs * ncr)
        ans += maxs * ncr
        ans %= mod
    for i in range(n-k+1):
        mins = a[i]
        ncr = cmb(n-i-1, k-1)
        #print(mins, ncr, mins * ncr)
        ans -= mins * ncr
        ans %= mod
    print(ans)
else:
    print((0))
# Original solution
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 #出力の制限
N = 10 ** 5 * 2 + 1
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()))

A.sort()

answer = 0
for i in range(N):
  if i > N - K:
    break
  answer = (answer + (A[N - i - 1] - A[i]) * cmb(N - 1 - i, K - 1, MOD)) % MOD

print(answer)

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

### Combination Table ###
g1 = [1, 1]      # 元テーブル
g2 = [1, 1]      # 逆元テーブル
inverse = [0, 1] # 逆元テーブル計算用テーブル

def comb(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

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)
#########################
  
A.sort()

answer = 0
for i in range(N):
  if i > N - K:
    break
  answer = (answer + (A[N - i - 1] - A[i]) * comb(N - 1 - i, K - 1, MOD)) % MOD

print(answer)
# Original solution
MOD = 10**9 + 7

def getInvs(n, MOD):
    invs = [1] * (n+1)
    for x in range(2, n+1):
        invs[x] = (-(MOD//x) * invs[MOD%x]) % MOD
    return invs

def getCombKs(n, k, invs, MOD):
    combKs = [0] * (n+1)
    combKs[k] = 1
    for x in range(k+1, n+1):
        combKs[x] = (combKs[x-1] * x * invs[x-k]) % MOD
    return combKs

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

As.sort()

invs = getInvs(N, MOD)
combKs = getCombKs(N, K-1, invs, MOD)

ans = 0
for i, A in enumerate(As):
    numMax = combKs[i]
    numMin = combKs[N-1-i]
    ans += A * (numMax - numMin) % MOD
    ans %= MOD

print(ans)


# Optimized solution
MOD = 10**9 + 7

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

def getInvs(n, MOD):
    invs = [1] * (n+1)
    for x in range(2, n+1):
        invs[x] = (-(MOD//x) * invs[MOD%x]) % MOD
    return invs
invs = getInvs(N, MOD)
def getCombKs(n, k, invs, MOD):
    combKs = [0] * (n+1)
    combKs[k] = 1
    for x in range(k+1, n+1):
        combKs[x] = (combKs[x-1] * x * invs[x-k]) % MOD
    return combKs
combKs = getCombKs(N, K-1, invs, MOD)

As.sort()

ans = 0
for i, A in enumerate(As):
    if i >= K-1:
        ans += combKs[i] * A
    if N-1-i >= K-1:
        ans -= combKs[N-1-i] * A
    ans %= MOD

print(ans)
# Original solution
MAX_NUM = 10**6 + 1
MOD = 10**9+7

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(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
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A=sorted(A)
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):
    for j in range(i-K+2):
        ANSWER += (A[i]-A[j])*combinations(i-j+1-2, K-2)
ANSWER %= MOD
print(ANSWER)


# Optimized solution
MAX_NUM = 10**6 + 1
MOD = 10**9+7

fac  = [0 for _ in range(MAX_NUM)]
finv = [0 for _ in range(MAX_NUM)]
inv  = [0 for _ in range(MAX_NUM)]

fac[0]  = fac[1] = 1
finv[0] = finv[1] = 1
inv[1] = 1

for i in range(2,MAX_NUM):
    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 combinations(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
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A=sorted(A)
ANSWER = 0
# Sの最大値をイテレート
for i in range(K-1, N):
    ANSWER += A[i]*combinations(i, K-1)
    # print(ANSWER)
for i in range(N-K+1):
    ANSWER -= A[i]*combinations(N-i-1, K-1)
    # print(ANSWER)

ANSWER %= MOD
print(ANSWER)
# Original solution
from operator import mul
from functools import reduce
def comb(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()))
a = list(map(int, input().split()))
a.sort()
mi = [0] * n
ma = [0] * n
s_mi = 0
s_ma = 0
for i in range(n):
    nn = n - i - 1
    if nn < k - 1:
        break
    else:
        mi[i] = comb(nn, k - 1)
        ma[-(i + 1)] = mi[i] 
for i, j, k in zip(a, mi, ma):
    s_mi += i * j
    s_ma += i * k
    
print(((s_ma - s_mi) % (10 ** 9 + 7)))

# Optimized solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
fac = {0: 1, 1: 1}
finv = {0: 1, 1: 1}
inv = {1: 1}
a.sort()
mi = [0] * n
ma = [0] * n
s_mi = 0
s_ma = 0

def comb_init():
    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

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

comb_init()

for i in range(n):
    nn = n - i - 1
    if nn < k - 1:
        break
    else:
        mi[i] = comb(nn, k - 1)
        ma[-(i + 1)] = mi[i] 
    s_mi = (s_mi + mi[i] * a[i]) % mod
    s_ma = (s_ma + ma[-(i + 1)] * a[-(i + 1)]) % mod
print(((s_ma - s_mi) % mod))
# Original solution
def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.sort()
    r = 0
    if False:
        for i in range(K+1):
            r += (A[-i-1] - A[i]) * cmb(N-i-1, K-1, mod)
    else:
        for i in range(N-K+1):
            r += (A[-i-1] - A[i]) * cmb(N-i-1, K-1, mod)
 
    print((r % mod))
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]

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


main()



# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    A.sort()
    r = 0
    if False:
        for i in range(K+1):
            r += (A[-i-1] - A[i]) * cmb(N-i-1, K-1, mod)
    else:
        for i in range(N-K+1):
            r += (A[N-i-1] - A[i]) * cmb(N-i-1, K-1, mod)
 
    print((r % mod))
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]

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


main()
# Original solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
a.sort()
total = 0
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
N = 10**5
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)

mem = {}

def cmb(n, r, mod):
    if (n,r) not in mem:
        mem[(n,r)] = _cmb(n, r, mod)
    return mem[(n,r)]

for i in range(n-k+1):
    for j in range(i+k-1,n):
        total += cmb(j-i-1,k-2,mod)*(a[j]-a[i])
        total %= mod
print(total)

# Optimized solution
n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
a.sort()
total = 0
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
N = 10**5
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)

for i in range(n-k+1):
    total += cmb(n-i-1,k-1,mod)*(a[n-i-1]-a[i])
    total %= mod
print(total)
# Original solution
from itertools import permutations, combinations
n,k=list(map(int, input().split()))
c=combinations(list(map(int, input().split())),k)
t=sum(  [max(v)-min(v) for v in list(c)]  )
print((t%(10**9+7)))

# Optimized solution
from itertools import combinations
n,k=list(map(int, input().split()))
c=combinations(list(map(int, input().split())),k)
print((sum(  [max(v)-min(v) for v in c]  )%(10**9+7)))
# Original solution
H, K = list(map(int,input().split()))
A = list(map(int, input().split()))
mod = int(10**9) + 7
A.sort()

nCkminu1 = [1] * H
for n in range(K, H):
    nCkminu1[n] = nCkminu1[n-1] * n // (n - (K-1))

ans = 0
for nCk, amin, amax in zip(nCkminu1[K-1:], A[:-(K-1)][::-1], A[K-1:]):
    ans +=  nCk % mod * (amax - amin)

print((ans % mod))


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

nCkminu1 = [1] * H
for n in range(K, H):
    nCkminu1[n] = nCkminu1[n-1] * n // (n - (K-1))
    nCkminu1[n-1] %= mod

ans = 0
for nCk, amin, amax in zip(nCkminu1[K-1:], A[:-(K-1)][::-1], A[K-1:]):
    ans +=  nCk * (amax - amin)

print((ans % mod))
# Original solution
p = 1000000007

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

fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % p


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], p - 2, p) * pow(fac[k], p - 2, p) % p


A.sort(reverse=True)
result = 0
for i in range(N - K + 1):
    result += A[i] * mcomb(N - (i + 1), K - 1)
    result %= p

A.reverse()
for i in range(N - K + 1):
    result -= A[i] * mcomb(N - (i + 1), K - 1)
    result %= p

# result += p
# result %= p
print(result)


# Optimized solution
# フェルマーの小定理
N, K = list(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)
result = 0
for i in range(N - K + 1):
    result += A[i] * mcomb(N - (i + 1), K - 1)
    result %= m

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

result -= t
result %= m
print(result)
# Original solution
# -*- coding: utf-8 -*-
class FactMod():
    '''
    modの値が素数の時のfactと組み合わせを求める
    フェルマーの小定理を用いているため、modが素数以外の時は使えない
    '''

    def __init__(self, n, mod):
        '''
        コンストラクタ
        f:nまでの i!の値を　配列に入れる
        inv: (i!)^-1　の値を配列に入れる
        '''
        self.mod = mod
        self.f = [1]*(n+1)
        for i in range(1, n+1):
            self.f[i] = self.f[i-1]*i % mod

        self.inv = [pow(self.f[-1], mod-2, mod)]
        for i in range(1, n+1)[::-1]:
            self.inv.append(self.inv[-1]*i % mod)
        self.inv.reverse()

    def fact(self, n):
        '''
        n!の値を返す
        '''
        return self.f[n]

    def comb(self, n, r):
        '''
        nCrの値を返す
        '''
        ret = self.f[n] * self.inv[n-r]*self.inv[r]
        ret %= self.mod
        return ret

    def perm(self, n, r):
        """
        nPrの値を返す
        """
        ret = self.f[n] * self.inv[n-r]
        ret %= self.mod
        return ret


N, K = list(map(int, input().split()))
L = list(map(int, input().split()))
L = sorted(L)
MOD = 10**9+7
F = FactMod(N+1, MOD)

sub = 0
add = 0
for i in reversed(list(range(K-1, N))):
    add += L[i]*F.comb(i, K-1)
    sub += L[N-i-1]*F.comb(i, K-1)

print(((-sub+add+MOD) % MOD))


# Optimized solution
# -*- coding: utf-8 -*-
class FactMod():
    '''
    modの値が素数の時のfactと組み合わせを求める
    フェルマーの小定理を用いているため、modが素数以外の時は使えない
    '''

    def __init__(self, n, mod):
        '''
        コンストラクタ
        f:nまでの i!の値を　配列に入れる
        inv: (i!)^-1　の値を配列に入れる
        '''
        self.mod = mod
        self.f = [1]*(n+1)
        for i in range(1, n+1):
            self.f[i] = self.f[i-1]*i % mod

        self.inv = [pow(self.f[-1], mod-2, mod)]
        for i in range(1, n+1)[::-1]:
            self.inv.append(self.inv[-1]*i % mod)
        self.inv.reverse()

    def fact(self, n):
        '''
        n!の値を返す
        '''
        return self.f[n]

    def comb(self, n, r):
        '''
        nCrの値を返す
        '''
        ret = self.f[n] * self.inv[n-r]*self.inv[r]
        ret %= self.mod
        return ret

    def perm(self, n, r):
        """
        nPrの値を返す
        """
        ret = self.f[n] * self.inv[n-r]
        ret %= self.mod
        return ret


N, K = list(map(int, input().split()))
L = list(map(int, input().split()))
L = sorted(L)
MOD = 10**9+7
F = FactMod(N+1, MOD)

print((sum([(L[i]-L[N-i-1]+MOD)*F.comb(i, K-1) % MOD
           for i in reversed(list(range(K-1, N)))]) % MOD))
# Original solution
# 組み合わせ(高速，フェルマーの小定理を利用)
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 #出力の制限
N = 10 ** 5
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 )

mod = 10 ** 9 + 7
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
ans = 0
for i in range(N-K+1):
    now_A = A[i]
    tmp = 1
    count = 0
    for k in range(i+K-1, N):
        ans += cmb(K-2+count, count, mod) * (A[k] - now_A)
        ans %= mod
        count += 1
        

print((ans%mod))

# Optimized solution
# 組み合わせ(高速，フェルマーの小定理を利用)
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 #出力の制限
N = 10 ** 5
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 )

mod = 10 ** 9 + 7
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()
ans_max = 0
ans_min = 0
# ans_minを求める
for i in range(N-K+1):
    ans_min += A[i] * cmb(N-1-i, K-1, mod)
    ans_min %= mod
for i in range(K-1, N):
    ans_max += A[i] * cmb(i, K-1, mod)
    ans_max %= mod
##print(ans_max, ans_min)
print(((ans_max - ans_min + mod)%mod))
# Original solution
P = 10**9 + 7
N = 1000000
inv_t = [0]+[1]
for i in range(2,N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]



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

A.sort()

mini_count = 0
maxim_count = 0
pattern = 1
for i in range(n-k, -1, -1):
    mini = A[i]
    mini_count += mini * pattern % mod
    mini_count %= mod
    pattern = pattern * (n-i) * inv_t[n-i-k+1] % mod


pattern = 1
for i in range(n-k+1, 0, -1):
    maxim = A[-i]
    maxim_count += maxim * pattern % mod
    maxim_count %= mod
    pattern = pattern * (n-i+1) * inv_t[n-i-k+2] % mod

ans = maxim_count - mini_count
ans %= mod

print(ans)

# Optimized solution
P = 10**9 + 7
N = 200000
inv_t = [0]+[1]  # 逆元
for i in range(2, N):
    inv_t += [inv_t[P % i] * (P - int(P / i)) % P]


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

A.sort()
mini_count = 0
maxim_count = 0
pattern = 1
"""
minX, maxXとして選ばれるのはそれぞれ
minX:sortAの右からk番目(大きい数値からk個取った時の最小値)とそれより左の要素
maxX:sortAの左からk番目(小さい数値からk個取った時の最大値)とそれより右の要素
選ばれるパターンはminXの場合自分の要素より右にある要素からk-1個を選ぶパターン数
つまり最初は(k-1)C(k-1) = 1
選べる要素が一つ増えるごとに
tC(k-1) ⇒ (t+1)C(k-1)のように増えるので
t * t-1 * t-2 ... * t-k ⇒ t+1 * t * t-1 ... * t-k+1 であり
つまりパターン数は (t+1) / (t-k) 倍になる
"""
for idx in range(k, n + 1):
    mini = A[-idx]  #
    mini_count += mini * pattern % mod
    mini_count %= mod

    maxim = A[idx - 1]
    maxim_count += maxim * pattern % mod
    maxim_count %= mod

    pattern = pattern * (idx) * inv_t[idx + 1 - k] % mod # 割り算のmodは逆元をかける

ans = maxim_count - mini_count
ans %= mod

print(ans)