CodeEff/ECCO huggingface dataset:
# Original solution
n,k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))

def framod(n, mod, a=1):
    for i in range(1,n+1):
        a=a * i % mod
    return a

def power(n, r, mod):
    if r == 0: return 1
    if r%2 == 0:
        return power(n*n % mod, r//2, mod) % mod
    if r%2 == 1:
        return n * power(n, r-1, mod) % mod
    
def comb(n, k, mod):
    a=framod(n, mod)
    b=framod(k, mod)
    c=framod(n-k, mod)
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod

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


print((ans%mod))

# Optimized solution
n,k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))
ans=0
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

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


print((ans%mod))
# Original solution
from collections import deque
N, K = list(map(int, input().split()))
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
mod = (10**9)+7
for h in range(N):
    B.append(A[h])
    C.append(A[h])


def cmb(n, r):  # 【Python】組み合わせ(nCr) 計算の高速化より引用
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod


N_cmb = 10**5
g1 = [1, 1]  # 元テーブル
g2 = [1, 1]  # 逆元テーブル
inverse = [0, 1]  # 逆元テーブル計算用テーブル

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


ans = 0
for i in range(N):
    if N - i >= K:
        kari = cmb(N - i - 1, K-1) % mod
        # print(kari)
        ans += (kari * B.pop()) % mod
        ans -= (kari * C.popleft()) % mod
        ans = ans % mod
print(ans)


# Optimized solution
from collections import deque
N, K = list(map(int, input().split()))
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
mod = (10**9)+7
for h in range(N):
    B.append(A[h])
    C.append(A[h])


def cmb(n, r):  # 【Python】組み合わせ(nCr) 計算の高速化より引用
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return fac[n] * finv[r] * finv[n-r] % mod


N_cmb = 10**5  # 入力の制約によって変えるところ、nCrの計算で欲しいnの最大値
fac = [1, 1]  # 階乗テーブル
finv = [1, 1]  # 逆元テーブル
inverse = [0, 1]  # 逆元テーブル計算用テーブル

for ZZZ in range(2, N_cmb + 1):
    fac.append((fac[-1] * ZZZ) % mod)
    inverse.append(mod-inverse[mod % ZZZ]*(mod//ZZZ) % mod)
    finv.append((finv[-1] * inverse[-1]) % mod)


ans = 0
for i in range(N):
    if N - i >= K:
        kari = cmb(N - i - 1, K-1) % mod
        # print(kari)
        ans += (kari * B.pop()) % mod
        ans -= (kari * C.popleft()) % mod
        ans = ans % mod
print(ans)
# Original solution
N, K=(int(i) for i in input().split())
A=[int(i) for i in input().split()]
A= sorted(A)

from math import factorial

inf=10**9+7
ans=0

for i in range(K, N+1):
    #(N-1)C(K-1)
    ans+=(A[i-1]*factorial(i-1) // factorial(K-1) // factorial(i-1-K+1))%inf
for i in range(1, N-K+2):
    #(N-i)C(K-1)
    ans-=(A[i-1]*factorial(N-i) // factorial(K-1) // factorial(N-i-K+1))%inf
print((ans%inf))



# Optimized solution

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

from math import factorial

inf=10**9+7
ans=0

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
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
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)


for i in range(K, N+1):
    #(N-1)C(K-1)
    ans+=(A[i-1]*cmb(i-1, K-1,p))%inf
for i in range(1, N-K+2):
    #(N-i)C(K-1)
    ans-=(A[i-1]*cmb(N-i, K-1,p))%inf
print((ans%inf))
# Original solution
def main(): 
  def modinv(a, mod=10**9+7):
      return pow(a, mod-2, mod)

  def comb(n, r, mod=10**9+7):
      r = min(r, n-r)
      res = 1
      for i in range(r):
          res = res * (n - i) * modinv(i+1, mod) % mod
      return res

  import sys
  input = sys.stdin.readline
  mod = 10**9+7  
  n,k = [int(i) for i in input().split()]
  a = [int(i) for i in input().split()]
  a = sorted(a)
  a = tuple(a)

  ans = 0
  for i in range(0,n-k+1):
    p = a[i]*comb(n-1-i,k-1,mod)
    ans -= (p%mod)
    ans %= mod
  for i in range(k-1,n):
    q = a[i]*comb(i,k-1,mod)
    ans += (q%mod)
    ans %= mod
  print(ans)
if __name__ == '__main__':
    main()

# Optimized solution
def make_array_for_comb(N, mod=10**9+7):
    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[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
fact, fact_inv = make_array_for_comb(n,mod)
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(0,n-k+1):
  p = a[i]*comb(n-1-i,k-1,mod)
  ans -= (p%mod)
  ans %= mod
for i in range(k-1,n):
  q = a[i]*comb(i,k-1,mod)
  ans += (q%mod)
  ans %= mod
print(ans)
# Original solution
n, k = list(map(int, input().split()))
a = sorted(list(map(int, input().split())))


MOD = 1000000007 

def pow(x, y):
    if y == 0:
        return 1
    ans = 1
    while y > 1:
        if y % 2 != 0:
            ans *= x
            ans %= MOD  
        x *= x
        x %= MOD 
        y //= 2
    return ans * x % MOD


def mod_inv(x):
  return pow(x, MOD - 2)

ans = 0
xCk_1 = {}
for i in range(k - 1, n):
  if i == k - 1:
    xCk_1[i] = 1
  else:
    xCk_1[i] = i * xCk_1[i - 1] * mod_inv(i - k + 1)

  ans += ((a[i] - a[n - i - 1]) % MOD) * xCk_1[i]
  ans %= MOD

print(ans)

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


MOD = 1000000007 

def pow(x, y):
    if y == 0:
        return 1
    ans = 1
    while y > 1:
        if y % 2 != 0:
            ans *= x
            ans %= MOD  
        x *= x
        x %= MOD 
        y //= 2
    return ans * x % MOD


def mod_inv(x):
  return pow(x, MOD - 2)

ans = 0
xCk_1 = {}
for i in range(k - 1, n):
  if i == k - 1:
    xCk_1[i] = 1
  else:
    xCk_1[i] = (((i * xCk_1[i - 1]) % MOD) * mod_inv(i - k + 1)) % MOD

  ans += ((a[i] - a[n - i - 1]) % MOD) * xCk_1[i]
  ans %= MOD

print(ans)
# Original solution
# https://atcoder.jp/contests/abc151/tasks/abc151_e

def calc(x,y):
    a = fac[x]
    b = fac[y]
    c = fac[x-y]
    ret = ( (a%p)*pow(b,p-2,p)*pow(c,p-2,p))%p
    return ret

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

fac=[1]
for i in range(1,N+1):
    fac.append( (fac[-1]*i)%p )
#print(fac)

ans=0
for L in range(N-K+1):
    for R in range(L+K-1,N):
        if L==R:
            tmp=0
            diff=0
            #print(L,R,diff)
        elif L+(K-1)==R:
            diff= A[R]-A[L]
            ans += diff
            #print(L,R,diff)
        else:
            tmp = calc(R-L-1,K-2)
            diff= tmp*(A[R]-A[L])
            ans += diff            
            #print(L,R,diff)
        ans %=p
            
print(ans)

# Optimized solution
# https://atcoder.jp/contests/abc151/tasks/abc151_e

def calc(x,y):
    a = fac[x]
    b = fac[y]
    c = fac[x-y]
    ret = ( (a%p)*pow(b,p-2,p)*pow(c,p-2,p))%p
    return ret

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

fac=[1]
for i in range(1,N+1):
    fac.append( (fac[-1]*i)%p )
#print(fac)

ans=0

for i in range(N):
    if i<=N-K:
        nokori = N-1-i
        kumi = calc(nokori,K-1)
        ans -= (A[i]*kumi)%p
    if i>=K-1:
        nokori = i
        kumi = calc(nokori,K-1)
        ans += (A[i]*kumi)%p
        
print((ans%p))

#for L in range(N-K+1):
#    for R in range(L+K-1,N):
#        if L==R:
#            tmp=0
#            diff=0
#            #print(L,R,diff)
#        elif L+(K-1)==R:
#            diff= A[R]-A[L]
#            ans += diff
#            #print(L,R,diff)
#        else:
#            tmp = calc(R-L-1,K-2)
#            diff= tmp*(A[R]-A[L])
#            ans += diff            
#            #print(L,R,diff)
#        ans %=p
# Original solution
import math

def combinations_count(n, r):
    a = 1
    b = 1
    if (n != 0) & (n >= r):
        for i in range(n, n - r, -1):
            a *= i
            b *= i - n + r
        return a // b
    else:
        return 0

n, k = list(map(int, input().split()))
a = [int(i) for i in input().split()]
a.sort()
mod = 10 ** 9 + 7
ans = 0
for index, value in enumerate(a):
    ans += (combinations_count(index, k - 1) - combinations_count(n - index - 1, k - 1)) * value % mod
print((ans % mod))

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

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

def combination(n, r):
    if (n != 0) & (n >= r):
        return f[n] * inv[r] * inv[n - r] % mod
    return 0    
    
ans = 0
for index, value in enumerate(a):
    ans += (combination(index, k - 1) - combination(n - index - 1, k - 1)) * value
print((ans % mod))
# Original solution
import math

def comb(n,r,m):
    if r == 0: return 1
    return math.factorial(n)*pow(f,m-2,m)*pow(math.factorial(n-r),m-2,m)

N,K,*a = list(map(int, open(0).read().split()))
m = 1000000007
f = math.factorial(K-1)
a.sort()
ans = 0
if K == 1:
    print((0))
else:
    for i in range(1,N-K+2):
        ans = (ans + a[-i] * comb(N-i,K-1,m)) % m
    for i in range(1,N-K+2):
        ans = (ans - a[i-1] * comb(N-i,K-1,m)) % m
    print(ans)

# Optimized solution
def memfact(a,m):
    temp = 1
    yield temp
    for i in range(1,a+1):
        temp = temp * i % m
        yield temp
def comb(n,r,m):
    if r == 0: return 1
    return memf[n]*pow(memf[r],m-2,m)*pow(memf[n-r],m-2,m)

N,K,*a = list(map(int, open(0).read().split()))
m = 1000000007
memf = []
mfappend = memf.append
for x in memfact(N-1,m):
    mfappend(x)
a.sort()
ans = 0
if K == 1:
    print((0))
else:
    for i in range(1,N-K+2):
        ans = (ans + a[-i] * comb(N-i,K-1,m)) % m
    for i in range(1,N-K+2):
        ans = (ans - a[i-1] * comb(N-i,K-1,m)) % m
    print(ans)
# Original solution
SIZE=10**5+1; MOD=10**9+7 #998244353 #ここを変更する
 
SIZE += 1
inv = [0]*SIZE  # inv[j] = j^{-1} mod MOD
fac = [0]*SIZE  # fac[j] = j! mod MOD
finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE):
    inv[i] = MOD - (MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD
 
def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0
 
def chofuku(ball,box): # nHk mod MOD の計算
    return choose(box+ball-1,box)


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

ans=0
for i in range(N):
    for j in range(i+K-1,N):
        ans+=(A[j]-A[i])*choose(j-i-1,K-2)
        ans%=MOD

print(ans)



# Optimized solution
SIZE=10**5+1; MOD=10**9+7 #998244353 #ここを変更する
 
SIZE += 1
inv = [0]*SIZE  # inv[j] = j^{-1} mod MOD
fac = [0]*SIZE  # fac[j] = j! mod MOD
finv = [0]*SIZE # finv[j] = (j!)^{-1} mod MOD
inv[1] = 1
fac[0] = fac[1] = 1
finv[0] = finv[1] = 1
for i in range(2,SIZE):
    inv[i] = MOD - (MOD//i)*inv[MOD%i]%MOD
    fac[i] = fac[i-1]*i%MOD
    finv[i]= finv[i-1]*inv[i]%MOD
 
def choose(n,r): # nCk mod MOD の計算
    if 0 <= r <= n:
        return (fac[n]*finv[r]%MOD)*finv[n-r]%MOD
    else:
        return 0
 
def chofuku(ball,box): # nHk mod MOD の計算
    return choose(box+ball-1,box)


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

smax=0
smin=0
for i in range(N):
    smax+=A[i]*choose(i,K-1)
    smax%=MOD
    smin+=A[i]*choose(N-i-1,K-1)
    smin%=MOD

print(((smax-smin)%MOD))
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()

mod=10**9+7

MAX_N=n+5
fac = [1,1] + [0]*MAX_N
finv = [1,1] + [0]*MAX_N
inv = [0,1] + [0]*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
  finv[i] = finv[i-1] * inv[i] % mod

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

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

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

print(((max_x-min_x)%mod))

# Optimized solution
from sys import stdin
nii=lambda:list(map(int,stdin.readline().split()))
lnii=lambda:list(map(int,stdin.readline().split()))

n,k=nii()
a=lnii()
a.sort()

mod=10**9+7

MAX_N = n+5
fac = [1,1] + [0]*MAX_N
finv = [1,1] + [0]*MAX_N
inv = [0,1] + [0]*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
  finv[i] = finv[i-1] * inv[i] % mod

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

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

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

ans=max_X-min_X
ans%=mod
print(ans)
# 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()))
A=list(map(int,input().split()))
MOD=10**9+7
A.sort()
ans=0

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

print(ans)

# Optimized solution

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

fac=[None for i in range(N)]
finv=[None for i in range(N)]
inv=[None for i in range(N)]

def COMinit(num):
    global fac
    global finv
    global inv
    fac[0]=1
    fac[1]=1
    finv[0]=1
    finv[1]=1
    inv[1]=1

    for i in range(2,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 COM(n,k):
    if n<k:
        return 0
    elif n<0 or k<0:
        return 0
    else:
        return int(fac[n] * (finv[k] * finv[n - k] % MOD) % MOD)
        
ans=0
COMinit(N)

for i in range(N-K+1):
    ans+=A[-(i+1)]*COM(N-(1+i),K-1)
    ans=ans%MOD
    ans-=A[i]*COM(N-(1+i),K-1)
    ans=ans%MOD

print(ans)
# Original solution
def main():
    n,k = list(map(int,input().split()))
    A = list(map(int,input().split()))
    A.sort()
    mod=10**9+7
    sum=0    
    def we_want(x):
        ans=1
        for i in range(k-1):
            ans=ans*(x-i)*pow(i+1,mod-2,mod)%mod 
        return ans 
        
    def check_min(i):
        if n-i>=k:
            minf=-we_want(n-1-i)*A[i]
            return minf
        else:return 0
    def check_max(i):
        if i>=k-1:
            maxf=we_want(i)*A[i]        
            return maxf
        else:return 0
    for i in range(len(A)):
        sum+=check_min(i)+check_max(i)
        sum%=mod
    print((sum%mod))

if __name__=='__main__':
    main()

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

MOD = 10 ** 9 + 7


def comb(n, r):
    if (r < 0) or n < r:
        return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


def solve(N, K, A):
    A.sort()
    ans = 0

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

    A.sort(reverse=True)
    for i in range(1, N):
        now = comb(i, K - 1)
        ans += A[i] * (-now)

    return ans % MOD


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

print((solve(N, K, A)))


# Optimized solution
MOD = 10 ** 9 + 7

def solve(N, K, A):
    A.sort()
    ans = 0
 
    for i in range(1, N):
        now = cmb(i, K - 1, MOD)
        ans += A[i] * now
 
    A.sort(reverse=True)
    for i in range(1, N):
        now = cmb(i, K - 1, MOD)
        ans += A[i] * (-now)
 
    return ans % MOD
 
 
# N = int(input())
# S = input()
N, K = list(map(int, input().split()))
A = list(map(int, 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
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)


print((solve(N, K, A)))
# Original solution
def combination(n, k, MOD):
  if(k > n or k < 0):
    return 0
  frac = [1]*(n+1)
  for i in range(1, n+1):
    frac[i] = (frac[i-1]*i)%MOD
  return (frac[n]*pow(frac[k], MOD-2, MOD)*pow(frac[n-k], MOD-2, MOD))%MOD

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 = (ans + A[i]*combination(i, K-1, MOD))%MOD
for i in range(N):
  ans = (ans - A[i]*combination(N-i-1, K-1, MOD))%MOD
  
print(ans)

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

#c[i] := (i)C(K-1)
c = [0] * (N+1)
#frac[i] = i!%MOD
frac = [1] * (N+1)
for i in range(1, N+1):
  frac[i] = (frac[i-1]*i)%MOD
for i in range(1, N+1):
  if(i >= K-1):
    c[i] = frac[i]*pow(frac[K-1], MOD-2, MOD)*pow(frac[i-K+1], MOD-2, MOD)%MOD

  
ans = 0
for i in range(N):
  ans = (ans + A[i]*c[i])%MOD
for i in range(N):
  ans = (ans - A[i]*c[N-i-1])%MOD
  
print(ans)
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
a=sorted(a)
mod=10**9+7
from operator import mul
from functools import reduce

def cmb(n,r):
    r=min(n-r,r)
    if r==0:
        return 1
    else:
        over=reduce(mul,list(range(n,n-r,-1)))
        under=reduce(mul,list(range(1,r+1)))
        return over//under
ans=0
for i in range(n-k+1):
    ans+=cmb(k+i-1,k-1)*(a[k-1+i]-a[n-k-i])
print((ans%mod))

                

# Optimized solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
a=sorted(a)
mod=10**9+7
ans=0
f=[1]
for i in range(1,n+1):
    f.append((f[-1]*i)%mod)
for i in range(n-k+1):
    ans+=(f[k+i-1]*pow(f[i],mod-2,mod)*pow(f[k-1],mod-2,mod))*(a[k-1+i]-a[n-k-i])
print((ans%mod))
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort()

mod = 10 ** 9 + 7

f = [1, 1]
for i in range(2, N+1):
    f.append(f[i-1] * i)

fk = pow(f[K-1], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1
    q = K - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)


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

mod = 10 ** 9 + 7

f = [i for i in range(N+1)]
for i in range(2, N+1):
    f[i] *= f[i-1]
    f[i] %= mod
f[0] = 1

q = K - 1
fk = pow(f[q], mod - 2, mod)

total = 0

for i in range(N - K + 1):
    p = N - i - 1

    c = (f[p] * fk * pow(f[p-q], mod-2, mod)) % mod
    total += c * (A[-1 - i] - A[i])
    total %= mod

print(total)
# Original solution
MOD=10**9+7
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))

a.sort(reverse=True)


fact = [1]
fact_inv = [1]
mod=10**9+7
for i in range(10**5+1):
    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

  
c=[]
for i in range(1,n-k+1+1):
  c.append(mod_comb_k(n-i,k-1,MOD))
lc=len(c)
#print(c,a)
sum=0
for i in range(lc):
  sum+=c[i]*(a[i]-a[-i-1])%MOD
  
print((sum%MOD))


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

a.sort(reverse=True)

#前計算
fact = [1]
fact_inv = [1]
mod=10**9+7
for i in range(n+1):
    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
#前計算終わり
  
c=[]
for i in range(1,n-k+1+1):
  c.append(mod_comb_k(n-i,k-1,MOD))
  
lc=len(c)
sum=0
for i in range(lc):
  sum+=c[i]*(a[i]-a[-i-1])%MOD
  
print((sum%MOD))
# Original solution
MOD=10**9+7
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))

a.sort(reverse=True)

#前計算
fact = [1]
fact_inv = [1]
mod=10**9+7
for i in range(n+1):
    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
#前計算終わり
  
c=[]
for i in range(1,n-k+1+1):
  c.append(mod_comb_k(n-i,k-1,MOD))
  
lc=len(c)
sum=0
for i in range(lc):
  sum+=c[i]*(a[i]-a[-i-1])%MOD
  
print((sum%MOD))


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


#----------------------------------------------------------
# nCk mod 10**9+7
# 前計算
fact=[1]
fact_inv=[1]
mod=10**9+7
for i in range(n+1):
  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
#----------------------------------------------------------

  
c=[]
for i in range(1,n-k+1+1):
  c.append(mod_comb_k(n-i,k-1,MOD))
  
lc=len(c)
sum=0
for i in range(lc):
  sum+=c[i]*(a[i]-a[-i-1])%MOD
  
print((sum%MOD))
# Original solution
P = 10**9+7
N, K = list(map(int, input().split()))
S = list(map(int, input().split()))
S.sort()
ans = 0
c = 1
for i in range(N-K+1):
    ans += (S[K-1+i]*c)%P
    ans -= (S[N-K-i]*c)%P
    ans %= P
    c = c*(K+i)//(i+1)
print(ans)

# Optimized solution
P = 10**9+7
N, K = list(map(int, input().split()))
S = list(map(int, input().split()))
S.sort()
ans = 0
c = 1
for i in range(N-K+1):
    ans += (S[K-1+i]*c)%P
    ans -= (S[N-K-i]*c)%P
    ans %= P
    c = c*(K+i)*pow(i+1, P-2, P)%P
print(ans)