CodeEff/ECCO huggingface dataset:
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
MOD = 10 ** 9 + 7
kai = [1]
for i in range(1, N + 1):
  kai.append(kai[-1] * i)

def comb(n, r):
  return kai[n] // (kai[n - r] * kai[r])

if K >= 2:
  A.sort(reverse = True)
  ans = 0
  for i in range(N - K + 1):
    for j in range(i + K - 1, N):
      ans += (A[i] - A[j]) * comb(j - i - 1, max(0, K - 2))
      ans %= MOD
  print(ans)
else:
  print((0))

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

kai1 = [1, 1]
kai2 = [1, 1]
inverse = [0, 1]
for i in range(2, N + 1):
  kai1.append((kai1[-1] * i) % MOD)
  inverse.append((-inverse[MOD % i] * (MOD // i)) % MOD)
  kai2.append((kai2[-1] * inverse[-1]) % MOD)


def comb(n, r):
  if r < 0 or r > n:
    return 0
  r = min(r, n - r)
  return (kai1[n] * kai2[r] * kai2[n - r]) % MOD

if K >= 2:
  A.sort(reverse = True)
  ans = 0
  for i in range(N - K + 1):
    for j in range(i + K - 1, N):
      ans += (A[i] - A[j]) * comb(j - i - 1, max(0, K - 2))
      ans %= MOD
  print(ans)
else:
  print((0))
# Original solution
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))

a.sort()

a2 = []
for i, x in enumerate(a):
  a2.append((x, i))

from math import factorial


def c(n, r):
  if n - r < 0:
    return 0
  return factorial(n) // factorial(r) // factorial(n - r)


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

MOD = 10**9 + 7
print(((ma - mi) % MOD))


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

a.sort()

a2 = []
for i, x in enumerate(a):
  a2.append((x, i))

from math import factorial


def c(n, r):
  if n - r < 0:
    return 0
  return factorial(n) // factorial(r) // factorial(n - r)


MOD = 10**9 + 7  # 出力の制限

# https://qiita.com/derodero24/items/91b6468e66923a87f39f


def cmb(n, r):
  if (r < 0 or r > n):
    return 0
  r = min(r, n - r)
  return g1[n] * g2[r] * g2[n - r] % MOD


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)


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

print(((ma - mi) % MOD))
# Original solution
from functools import lru_cache


@lru_cache(None)
def factrial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i
    return ans


@lru_cache(None)
def combinations(n, r,mod):
    return (factrial(n) // (factrial(n - r) * factrial(r))) % mod


def main():
    n, k = list(map(int, input().split()))
    mod = 10 ** 9 + 7
    A = tuple([x % mod for x in sorted(map(int, input().split()))])
    ans = 0
    for i, a in enumerate(A[k - 1:], k - 1):
        ans += (a * combinations(i, k - 1,mod)) % mod
        ans %= mod
    for i, a in enumerate(A[: n - k + 1], 1):
        ans -= (a * combinations(n - i, k - 1,mod)) % mod
        ans %= mod
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
from functools import lru_cache
n, k = list(map(int, input().split()))
A = sorted(map(int, input().split()))
mod = 10 ** 9 + 7
# 階乗とその逆元の計算
factrial = [0 for _ in range(n + 1)]
factrial[0] = 1
for i in range(1, n + 1):
    factrial[i] = (factrial[i - 1] * i) % mod
inverse_factrial = list()
for f in factrial:
    inverse_factrial.append(pow(f, -1, mod))


@lru_cache(maxsize=None)
def nCr(n, r):
    return (factrial[n] * inverse_factrial[r] * inverse_factrial[n - r]) % mod


ans = 0
# 最大値の和
for i, a in enumerate(A[k - 1:], k - 1):
    ans += ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
# 最小値の和
A.reverse()
for i, a in enumerate(A[k - 1:], k - 1):
    ans -= ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
print(ans)
# Original solution
import math

N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
Ar = A[::-1]
Ai = [Ar[i] - A[i] for i in range(N)]
ans = 0

if K > N//2:
    n, r = N-1, K-1 
    C = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

    for i in range(N-K+1):
        ans += Ai[i] * C
        C *= N-i-K
        if N-i-1 != 0:
            C //= N-i-1
else:
    C = 1
    for i in range(N-K, -1, -1):
        ans += Ai[i] * C
        C *= N - i
        if N-K+1 - i != 0:
            C //= N-K+1 - i

print((ans % 1000000007))

# Optimized solution
N, K = list(map(int, input().split()))
MOD = 10**9+7
A = [a % MOD for a in sorted(list(map(int, input().split())))]
MIN, MAX = 0, 0

def div_MOD(a):     # (a^{-1} mod MOD) をフェルマーの小定理より求める
    return pow(a, MOD - 2, MOD)     # a^{-1} = a^{p-2} (mod p)

C = [0]*(K-1) + [1]
for n in range(K, N):
    C.append((C[n-1] * n * div_MOD(n-(K-1))) % MOD)

for i in range(N-K+1):
    MIN = (MIN + (A[i] * C[N-i-1]) % MOD) %MOD
for i in range(N-K+1):
    MAX = (MAX + (A[N-1-i] * C[N-i-1]) % MOD) %MOD

print(((MAX - MIN) % MOD))
# Original solution
def prepare(N, MOD):
    fac = [None] * (N+2)
    finv = [None] * (N+2)
    inv = [None] * (N+2)
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    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
    return fac, finv

def combination(N, K, fac, finv, MOD):
    if N < K:
        return 0
    if N < 0 or K < 0:
        return 0
    else:
        return fac[N] * (finv[K] * finv[N - K] % MOD) % 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):
    fac, finv = prepare(i, MOD)
    comb = combination(i, K-1, fac, finv, MOD)
    ans += A[i] * comb

A.sort(reverse=True)
for i in range(N):
    fac, finv = prepare(i, MOD)
    comb = combination(i, K-1, fac, finv, MOD)
    ans -= A[i] * comb

ans %= MOD
print(ans)

# Optimized solution
def prepare(N, MOD):
    fac = [None] * (N+2)
    finv = [None] * (N+2)
    inv = [None] * (N+2)
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    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
    return fac, finv

def combination(N, K, fac, finv, MOD):
    if N < K:
        return 0
    if N < 0 or K < 0:
        return 0
    else:
        return fac[N] * (finv[K] * finv[N - K] % MOD) % MOD

MOD = 10 ** 9 + 7

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

A.sort()

fac, finv = prepare(N, MOD)
ans = 0
for i in range(N):
    comb = combination(i, K-1, fac, finv, MOD)
    ans += A[i] * comb

A.sort(reverse=True)
for i in range(N):
    comb = combination(i, K-1, fac, finv, MOD)
    ans -= A[i] * comb

ans %= MOD
print(ans)
# Original solution
"""
ジャガー「わからん」
先にmax,minそれぞれの和を求める
"""

import sys
input = sys.stdin.readline

mod = 10**9+7

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    # n!/(n-k)!
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    # k!
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    # n!/((n-k)!*k!)
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

def main():
    N,K = list(map(int,input().split()))
    A = sorted([int(i) for i in input().split()])

    cmb = [0] * (N-K+1)
    for i in range(N-K+1):
        cmb[i] = getComb(i+(K-1),K-1,mod)

    sum_min = 0
    sum_max = 0
    for i in range(N):
        if i >= K-1:
            sum_max = (sum_max + (cmb[i-(K-1)] * A[i]) % mod) % mod
        if i <= N-K: 
            sum_min = (sum_min + (cmb[(N-K)-i] * A[i]) % mod) % mod
        
    answer = (sum_max - sum_min) % mod
    print(answer)

if __name__ == "__main__":
    main()

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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


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

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print((answer % mod))
# Original solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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


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

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print((answer % mod))

# Optimized solution
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

class Combination():
    # コンストラクタ
    def __init__(self, N:int, P:int):
        self.N = N
        self.P = P

        # fact[i] = (i! mod P)
        self.fact = [1, 1]   
        # factinv[i] = ((i!)^(-1) mod P)
        self.factinv = [1, 1]
        # factinv 計算用
        self.inv = [0, 1]    

        for i in range(2, N+1):
            self.fact.append((self.fact[-1] * i) % P)
            self.inv.append((-self.inv[P % i] * (P // i)) % P)
            self.factinv.append((self.factinv[-1] * self.inv[-1]) % P)
    
    # nCk (mod P)   (ただし、n<=N)
    def getComb(self, n:int, k:int):
        if (k < 0) or (n < k):
            return 0
        k = min(k, n - k)
        return self.fact[n] * self.factinv[k] * self.factinv[n-k] % self.P

def main():
    N,K = list(map(int,input().split()))
    A = sorted([int(i) for i in input().split()])

    mod = 10**9+7

    COMB = Combination(N,mod)

    cmb = [0] * (N-K+1)
    for i in range(N-K+1):
        cmb[i] = COMB.getComb(i+(K-1),K-1)

    sum_max = 0
    for i in range(K-1,N):
        sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

    sum_min = 0
    for i in range(N-K,-1,-1):
        sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

    answer = sum_max - sum_min
    print((answer % mod))

if __name__ == "__main__":
    main()
# Original solution
from math import factorial
p=10**9+7
n,k=list(map(int,input().split()))
A=[int(i) for i in input().split()]
A.sort()

def nck(n,k):
  return factorial(n)//factorial(n-k)//factorial(k)

ans=0
if k==1:
  ans=0
else:
  for i in range(n):
    for j in range(k-1,n-i):
      ans+=(A[i+j]-A[i])*nck(j-1,k-2)
      ans=ans%p
print(ans)
      

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

mod=10**9+7

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

inv_factorial = [-1] * (n+1)
inv_factorial[-1] = pow(factorial[-1], mod-2, mod)
for i in reversed(list(range(n))):
    inv_factorial[i] = inv_factorial[i+1] * (i+1) % mod

def fact(n):
    return factorial[n]

def nck(n, k):
    if k>n or k<0:
        return 0
    else:
        return factorial[n] * inv_factorial[n-k] * inv_factorial[k] %mod

m=0
for i in range(n):
  m%=mod
  m+=arr[i]*nck(n-i-1, k-1)


M=0
for i in range(n-1, -1, -1):
  M%=mod
  M+=arr[i]*nck(i, k-1)

print(((M-m)%mod))
# Original solution
n, k = list(map(int, input().split()))
a_list = list(map(int, input().split()))
a_list.sort()

import math


def cmb(n, r):
    o = math.factorial(n)
    u = math.factorial(r) * math.factorial(n - r)
    return o // u


max_val = 0
min_val = 0
val = int(cmb(n - 1, k - 1))
for i in range(n - k + 1):
    if i != 0:
        val = (val * (n - i - k + 1) // (n - i))
    max_val = (max_val + (((a_list[-(i + 1)]) * val))) % (10 ** 9 + 7)
    min_val = (min_val + ((a_list[i]) * val)) % (10 ** 9 + 7)

print((int((max_val - min_val) % (10 ** 9 + 7))))

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


mod = 10 ** 9 + 7
f_list = [1]
for i in range(1,n+1):
    f_list.append((f_list[-1] * i) % mod)
def comb(n,r):
    return f_list[n] * (pow(f_list[r], mod-2, mod) * pow(f_list[n-r], mod-2, mod) % mod) % mod


max_val = 0
min_val = 0
for i in range(n - k + 1):
    val = comb(n - i - 1, k - 1)
    max_val = (max_val + (((a_list[-(i + 1)]) * val))) % (10 ** 9 + 7)
    min_val = (min_val + ((a_list[i]) * val)) % (10 ** 9 + 7)

print((int((max_val - min_val) % (10 ** 9 + 7))))
# Original solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

def comb(n,k,M):
    nume,deno = 1,1
    for i in range(1,k+1):
        nume = (nume * (n - i + 1)) % M
        deno = (deno * i) % M
    return (nume * inv(deno,M)) % M

ans = 0
for i in range(N-K+1):
    ans = (ans + comb(N-1-i,K-1,M)*((A[N-1-i]-A[i]) % M)) % M
print(ans)


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

M = 10**9+7

def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

def inv(a,p):
    return pow(a,p-2,p)

ans,comb = 0,1
#i番目ではcomb = (K-1-i)_C_(K-1)
#max,min以外での選ぶ個数は常にK-1であることに注目すればcomb関数はいらなかった
for i in range(N-K+1):
    if i > 0:
        comb = (comb * (K-1+i) * inv(i,M)) % M
    ans = (ans + comb*((A[K-1+i]-A[N-K-i]) % M)) % M
print(ans)
# Original solution
def pow(a,b,M):
    if b == 0:
        return 1
    elif b % 2 == 0:
        return pow((a**2)%M,b//2,M) % M
    else:
        return ((a % M) * pow((a**2)%M,b//2,M)) % M

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()))
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) * pow(n-i-1,mod-2,mod)
if ans < 0:
    ans += mod
print(ans)


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

M = 10**9+7

def inv(a,p):
    return pow(a,p-2,p)

ans,comb = 0,1
#i番目ではcomb = (K-1-i)_C_(K-1)
#max,min以外での選ぶ個数は常にK-1であることに注目すればcomb関数はいらなかった
for i in range(N-K+1):
    if i > 0:
        comb = (comb * (K-1+i) * inv(i,M)) % M
    ans = (ans + comb*((A[K-1+i]-A[N-K-i]) % M)) % M
print(ans)
# Original solution
n, k = [int(i) for i in input().split()]
al = [int(i) for i in input().split()]
al.sort()
mod = 10 ** 9 + 7


kai_dp={}
def kai(x, p=mod):
    if x in kai_dp:
        return kai_dp[x]
    a = 1
    for i in range(1, x + 1):
        a *= i
        a %= p
    kai_dp[x]=a
    return a

def comb(a, b, p=mod):
    if a < 0 or b < 0:
        return 0
    elif a < b:
        return 0
    c = 1
    c *= kai(a, p)
    c *= pow(kai(b, p), p - 2, p)
    c *= pow(kai(a - b, p), p - 2, p)
    return c % p


ans = 0
for i in range(k - 1, n):
    ans += al[i] * comb(i,k-1)
    ans %= mod
al = list(reversed(al))
for i in range(k - 1, n):
    ans -= al[i] * comb(i, k-1)
    ans %= mod
print(ans)


# Optimized solution
n, k = [int(i) for i in input().split()]
al = [int(i) for i in input().split()]
al.sort()
mod = 10 ** 9 + 7


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




def comb(a, b, p=mod):
    if a < 0 or b < 0:
        return 0
    elif a < b:
        return 0
    c = 1
    c *= kai[a]
    c *= pow(kai[b], p - 2, p)
    c *= pow(kai[a - b], p - 2, p)
    return c % p


ans = 0
for i in range(k - 1, n):
    ans += al[i] * comb(i,k-1)
    ans %= mod
al = list(reversed(al))
for i in range(k - 1, n):
    ans -= al[i] * comb(i, k-1)
    ans %= mod
print(ans)
# Original solution
N,K=list(map(int, input().split()))
AA=list(map(int, input().split()))
A=sorted(AA)
B=sorted(AA)[::-1]
mod=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
mod = 10**9+7 #出力の制限
n = 10**6+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 )

ans=0
for i in range(N-K+1):
  d=(A[K-1+i]%mod)*cmb(K-1+i,K-1,mod)
  e=(B[K-1+i]%mod)*cmb(K-1+i,K-1,mod)
  ans+=d%mod-e%mod
  ans%=mod
print(ans)

# Optimized solution
N,K=list(map(int, input().split()))
AA=list(map(int, input().split()))
A=sorted(AA)
B=sorted(AA)[::-1]
mod=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
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 )

ans=0
for i in range(N-K+1):
  d=(A[K-1+i]%mod)*cmb(K-1+i,K-1,mod)
  e=(B[K-1+i]%mod)*cmb(K-1+i,K-1,mod)
  ans+=d%mod-e%mod
  ans%=mod
print(ans)
# Original solution
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def comb(n,k):
        if n < k:
            return 0
        return f[n]*inv[k]*inv[n-k]%mod
    n,k = LI()
    a = LI()
    a.sort()
    f = [1]
    N = 10**6
    for i in range(1,N+1):
        f.append(f[-1]*i%mod)
    inv = [None]*(N+1)
    inv[N] = pow(f[N],mod-2,mod)
    for i in range(1,N+1)[::-1]:
        inv[i-1] = inv[i]*i%mod
    ans = 0
    d = defaultdict(lambda : 1)
    for i in a:
        l = bisect.bisect_right(a,i)-d[i]
        r = n-l-1
        d[i] += 1
        ans += i*(comb(l,k-1)-comb(r,k-1))%mod
        ans %= mod
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    a = LI()
    f = list(set(a))
    f.sort()
    s = [0]*len(f)
    ind = [0]*n
    for i in range(n):
        j = bisect.bisect_left(f,a[i])
        s[j] += 1
    ls = list(accumulate(s))
    rs = list(accumulate(s[::-1]))[::-1]
    ans = 0
    fact = [1]
    N = 10**5
    for i in range(1,N+1):
        fact.append(fact[-1]*i%mod)
    inv = [0]*(N+1)
    inv[N] = pow(fact[N],mod-2,mod)
    for i in range(N)[::-1]:
        inv[i] = inv[i+1]*(i+1)%mod
    comb = lambda x,y:fact[x]*inv[y]*inv[x-y]%mod
    for j in a:
        i = bisect.bisect_left(f,j)
        l = ls[i]-1
        r = rs[i]-1
        cl = comb(l,k-1) if l >= k-1 else 0
        cr = comb(r,k-1) if r >= k-1 else 0
        ans += f[i]*(cl-cr)
        ans %= mod
        ls[i] -= 1
        rs[i] -= 1
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()
# Original solution
n,k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()

def mod_nCr(n,k,mod):
    a = 1
    for i in range(n-k+1,n+1):
        a = a*i%mod
    b = 1
    for i in range(1,k+1):
        b = b*pow(i,mod-2,mod)%mod
    return a*b%mod

mod = 10**9+7
ans = 0
for i in range(k-1,n)[::-1]:
    ans += a[i]*mod_nCr(i,k-1,mod)
    ans %= mod
a.reverse()
for i in range(k-1,n)[::-1]:
    ans -= a[i]*mod_nCr(i,k-1,mod)
    ans %= mod
print((ans%mod))

# Optimized solution
n,k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
a.sort()

def mod_nCr(n,k,mod):
    r = min(k,n-k)
    return fact[n]*factinv[k]*factinv[n-k]%mod

mod = 10**9+7
fact = [1,1]
inv = [0,1]
factinv = [1,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)
    
ans = 0
for i in range(k-1,n)[::-1]:
    ans += a[i]*mod_nCr(i,k-1,mod)
    ans %= mod
a.reverse()
for i in range(k-1,n)[::-1]:
    ans -= a[i]*mod_nCr(i,k-1,mod)
    ans %= mod
print((ans%mod))