CodeEff/ECCO huggingface dataset:
# Original solution
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, K = list(map(int, readline().split()))
    A = list(map(int, readline().split()))

    A.sort()
    lenA = len(A)
    ans = 0

    if K == 1:
        print((0))
        sys.exit()

    def com(n, r, mod):
        r = min(r, n - r)
        if r == 0:
            return 1
        res = ilist[n] * iinvlist[n - r] * iinvlist[r] % mod
        return res

    ilist = [0]
    iinvlist = [1]

    def modinv(a, mod=10 ** 9 + 7):
        return pow(a, mod - 2, mod)

    tmp = 1
    for i in range(1, N + 1):
        tmp *= i
        tmp %= mod
        ilist.append(tmp)
        iinvlist.append(modinv(tmp, mod))

    for front in range(lenA):
        for last in range(lenA-front-K+1):
            last = last + front + K - 1
            f_x = A[last] - A[front]
            count = last - front - 1
            ans += com(count, K-2, mod) * f_x
    ans %= mod
    print(ans)

if __name__ == '__main__':
    solve()

# Optimized solution
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, K = list(map(int, readline().split()))
    A = list(map(int, readline().split()))

    A.sort()
    lenA = len(A)
    ans = 0

    if K == 1:
        print((0))
        sys.exit()

    def com(n, r, mod):
        r = min(r, n - r)
        if r == 0:
            return 1
        res = ilist[n] * iinvlist[n - r] * iinvlist[r] % mod
        return res

    def modinv(a, mod=10 ** 9 + 7):
        return pow(a, mod - 2, mod)

    ilist = [0]
    iinvlist = [1]
    tmp = 1
    for i in range(1, N + 1):
        tmp *= i
        tmp %= mod
        ilist.append(tmp)
        iinvlist.append(modinv(tmp, mod))

    sum_of_com = []
    now = 0

    for i in range(K-2, N+1):
        now += com(i, K-2, mod)
        sum_of_com.append(now)

    ans = 0
    for first in range(lenA-K+1):
        ans -= A[first] * sum_of_com[lenA-first-K]
    for last in range(K-1, lenA):
        ans += A[last] * sum_of_com[last-K+1]
    ans %= mod
    print(ans)

if __name__ == '__main__':
    solve()
# Original solution
def prepare(n, MOD):
 
    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
     
    return factorials, invs


MOD = 10**9 + 7
f, inv = prepare(10**5+100, MOD)

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

a.sort(reverse=True)

p = 0
m = 0
for i in range(n):
    if n - i >= k:
        cn = n - i - 1
        ck = k - 1
        if cn >= ck:
            combi = f[cn] * inv[ck] % MOD * inv[cn - ck] % MOD
            if a[i] >= 0:        
                p += (a[i] * combi) % MOD
            else:
                p += (a[i] * combi) % -MOD
            if p >= 0:
                p %= MOD
            else:
                p %= -MOD
    if i >= k - 1:
        cn = i
        ck = k - 1
        if cn >= ck:
            combi = f[cn] * inv[ck] % MOD * inv[cn - ck] % MOD
            if a[i] >= 0:        
                m += (a[i] * combi) % MOD
            else:
                m += (a[i] * combi) % -MOD
            if m >= 0:
                m %= MOD
            else:
                m %= -MOD
        
print(((p - m)%MOD))

# Optimized solution
def prepare(n, MOD):
 
    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv
     
    return factorials, invs


MOD = 10**9 + 7
f, inv = prepare(10**5+1, MOD)

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

a.sort(reverse=True)

p = 0
m = 0
for i in range(n):
    if n - i >= k:
        cn = n - i - 1
        ck = k - 1
        combi = f[cn] * inv[ck] % MOD * inv[cn - ck] % MOD
        if a[i] >= 0:        
            p += (a[i] * combi) % MOD
        else:
            p += (a[i] * combi) % -MOD
        if p >= 0:
            p %= MOD
        else:
            p %= -MOD
    if i >= k - 1:
        cn = i
        ck = k - 1
        combi = f[cn] * inv[ck] % MOD * inv[cn - ck] % MOD
        if a[i] >= 0:        
            m += (a[i] * combi) % MOD
        else:
            m += (a[i] * combi) % -MOD
        if m >= 0:
            m %= MOD
        else:
            m %= -MOD
        
print(((p - m) % MOD))
# Original solution
def main():
  n,k = list(map(int, input().split()))
  a = list(map(int, input().split()))
  a.sort()
  ans = 0

  c = 1
  nCk = [1]
  for r in range(k,n+1):
      c = c*r//(r-k+1)
      nCk.append(c)

  if k == 1:
    print((0))
  else:
    for i in range(1,n):
      if n-i < k-1:
        break
      ans += (a[n-i]-a[i-1])*nCk[n-i-k+1]
    print((ans%1000000007))
if __name__ == "__main__":
  main()


# Optimized solution
def main():
  n,k = list(map(int, input().split()))
  a = list(map(int, input().split()))
  a.sort()
  ans = 0
  if k == 1:
    print((0))
  else:
    for i in range(k-1,n):
      if n-i+1<0:
        break
      if i == k-1:
        c = 1
        ans += (a[i]-a[n-i-1])*c
      else:
        c = c*i//(i-k+1)
        ans += (a[i]-a[n-i-1])*c
    print((ans%1000000007))
if __name__ == "__main__":
  main()
# Original solution
n,K=list(map(int,input().split()))

L=list(map(int,input().split()))

L.sort()

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 )

#print(L)
mod=10**9+7
ans=0
for i in range(K-1,len(L)): #ピッチ
    for j in range(len(L)): #スタート
        if i+j<len(L):
            ans+=(L[j+i]-L[j])*cmb(i-1,K-2,mod)
            #print(j,i,cmb(i,K-2,mod))
    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



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 = 10**5+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 )
L=[]
for i in range(n):
    L.append(cmb(n-1-i,K-1,mod))
#print(L)
Small=0
for i in range(n):
    Small+=A[i]*L[i]
    Small%mod
    
Big=0
RA=A[::-1]
for i in range(n):
    Big+=RA[i]*L[i]
    Big%=mod
print(((Big-Small)%mod))
# Original solution
import sys
from math import factorial

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

mod = 10**9 + 7

a.sort()

li = []
for i in range(n-1):
    li.append(a[i+1]-a[i])

ans = 0

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

def comb(n,k):
    return dp[n] // dp[k] // dp[n-k]

comb1 = comb(n,k)

for i in range(n-1):
    if i+1 < k and n-i-1 < k:
        ans += li[i] * comb1
    elif i+1 < k:
        ans += li[i] * (comb1 - comb(n-i-1,k))
    elif n-i-1 < k:
        ans += li[i] * (comb1 - comb(i+1,k))
    else:
        ans += li[i] * (comb1 - comb(n-i-1,k) - comb(i+1,k))

print((ans%mod))


# Optimized solution
import sys
from math import factorial

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

mod = 10**9 + 7

a.sort()

li = []
for i in range(n-1):
    li.append(a[i+1]-a[i])

ans = 0

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

def comb(n,k):
    return factorial(n) // factorial(k) // factorial(n-k) % mod
    #return dp[n] // dp[k] // dp[n-k]

comb1 = comb(n,k)

for i in range(n-1):
    if i+1 < k and n-i-1 < k:
        ans += li[i] * comb1
    elif i+1 < k:
        ans += li[i] * (comb1 - comb(n-i-1,k))
    elif n-i-1 < k:
        ans += li[i] * (comb1 - comb(i+1,k))
    else:
        ans += li[i] * (comb1 - comb(n-i-1,k) - comb(i+1,k))

print((ans%mod))
# Original solution
MOD = 10**9 + 7


def comb(n,k,p):
    """power_funcを用いて(nCk) mod p を求める"""
    from math import factorial
    if n<0 or k<0 or n<k:
        return 0
    if n==0 or k==0:
        return 1
    a=factorial(n) %p
    b=factorial(k) %p
    c=factorial(n-k) %p
    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 = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort()
A_rev = list(reversed(A))


ans = 0
for i, j in zip(A, A_rev):
    if N < K:
        break
    C = comb(N - 1, K - 1, MOD)
    ans += C * j
    ans -= C * i
    ans %= MOD
    N -= 1
print((int(ans % MOD)))


# Optimized solution
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
A.sort()
MOD = 10 ** 9 + 7


def combination(mod=MOD):
    fact = [1] * (N + 1)
    inv_fact = [1] * (N + 1)
    r = 1
    for i in range(1, N + 1):
        fact[i] = r = r * i % mod
    inv_fact[N] = r = pow(fact[N], mod - 2, mod)
    for i in range(N, 0, -1):
        inv_fact[i - 1] = r = r * i % mod

    def _wrapper(n, k):
        nonlocal fact, inv_fact, mod
        if n == 0 or k == 0:
            return 1
        return fact[n] * inv_fact[k] * inv_fact[n - k] % mod

    return _wrapper


def main():
    if K == 1:
        print((0))
        exit()
    comb = combination()
    sum_max = 0
    sum_min = 0
    for i, A_i in enumerate(A):
        if i >= K - 1:
            sum_max += comb(i, K - 1) * A_i % MOD
        if N - i >= K:
            sum_min += comb(N - i - 1, K - 1) * A_i % MOD
    ans = (sum_max - sum_min) % MOD
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
mod = 10**9 + 7
A.sort(reverse=True)
kaijou = [1 for _ in range(N+1)]
for k in range(1, N):
  kaijou[k+1] = kaijou[k]*(k+1)%mod



b = mod-2
blis = []
c = 0
while b >0:
  if b & 1 == 1:
    blis.append(c)
  c += 1
  b >>= 1

def modinv(a):
  if a == 1:
    return 1
  else:
    res = 1
    li = []
    for _ in range(c):
      li.append(a%mod)
      a = a*a%mod
    for item in blis:
      res = res *li[item] %mod
    return res

def combination(n, k):
  foo = kaijou[n]*modinv(kaijou[k]*kaijou[n-k]%mod)%mod
  return foo

ans = 0

for k in range(N-K+1):
  ans += A[k]*combination(N-k-1, K-1)
  ans %= mod

for k in range(N-K+1):
  ans -= A[-k-1]*combination(N-k-1, K-1)
  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(reverse=True)
"""
kaijou = [1 for _ in range(N+1)]
for k in range(1, N):
  kaijou[k+1] = kaijou[k]*(k+1)%mod



b = mod-2
blis = []
c = 0
while b >0:
  if b & 1 == 1:
    blis.append(c)
  c += 1
  b >>= 1

def modinv(a):
  if a == 1:
    return 1
  else:
    res = 1
    li = []
    for _ in range(c):
      li.append(a%mod)
      a = a*a%mod
    for item in blis:
      res = res *li[item] %mod
    return res
"""
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) % mod)
  inv.append((-inv[mod % i] * (mod // i)) % mod)
  factinv.append((factinv[-1] * inv[-1]) % mod)




def combination(n, k):
  #foo = kaijou[n]*modinv(kaijou[k]*kaijou[n-k]%mod)%mod
  foo = fact[n]*factinv[k]*factinv[n-k]%mod
  return foo

ans = 0

for k in range(N-K+1):
  ans += A[k]*combination(N-k-1, K-1)
  ans %= mod

for k in range(N-K+1):
  ans -= A[-k-1]*combination(N-k-1, K-1)
  ans %= mod

print(ans)
# Original solution
from operator import mul
from functools import reduce

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

X_max = 0
X_min = 0

nCr = {}

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


for i in range(N-K+1):
    n = cmb(N-1-i, K-1)
    X_max += n*A[-(i+1)]
    X_min += n*A[i]

print(((X_max - X_min) % 1000000007))

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

p = 1000000007
X_max = 0
X_min = 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


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(N-K+1):
    n = cmb(N-1-i, K-1, p)
    X_max += n*A[-(i+1)]
    X_max %= p
    X_min += n*A[i]
    X_min %= p

print((int((X_max - X_min) % p)))
# Original solution
MOD = 10 ** 9 + 7
FACT_MAX = 10 ** 5
fact = [1] * FACT_MAX
for i in range(1, FACT_MAX):
    fact[i] = fact[i - 1] * i % MOD
def comb(n, r):
    return fact[n] * pow(fact[n - r], MOD - 2, MOD) * pow(fact[r], MOD - 2, MOD)

N, K = list(map(int, input().split()))
A = sorted(map(int, input().split()))
print((sum(comb(i, K - 1) * (A[i] - A[N - i - 1]) % MOD for i in range(K - 1, N)) % MOD))


# Optimized solution
MOD = 10 ** 9 + 7
def comb(n, r):
    return fact[n] * pow(fact[n - r], MOD - 2, MOD) * pow(fact[r], MOD - 2, MOD)

N, K = list(map(int, input().split()))
fact = [1] * N
for i in range(1, N):
    fact[i] = fact[i - 1] * i % MOD
A = sorted(map(int, input().split()))
print((sum(comb(i, K - 1) * (A[i] - A[N - i - 1]) % MOD for i in range(K - 1, N)) % MOD))
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
mod = 10**9+7
comb = [0] * (N+1)
comb[K-1] = 1
for i in range(K-1, N):
    comb[i+1] = comb[i] * (i+1) * pow(i-K+2, -1, mod) % mod
# print(comb)
A.sort()
# mi = sum(a*comb[i]%mod for a, i in zip(reversed(A[:N-K+1]), range(K-1, N)))
# ma = sum(a*comb[i]%mod for a, i in zip((A[K-1:]), range(K-1, N)))
ans = sum((a-b)*comb[i]%mod for b, a, i in zip(reversed(A[:N-K+1]), A[K-1:], list(range(K-1, N))))
# print(mi%mod, ma%mod)
print((ans%mod))

# Optimized solution
def main():
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    mod = 10**9+7
    comb = [0] * (N+1)
    comb[K-1] = 1
    for i in range(K-1, N):
        comb[i+1] = comb[i] * (i+1) * pow(i-K+2, -1, mod) % mod
    # print(comb)
    A.sort()
    # mi = sum(a*comb[i]%mod for a, i in zip(reversed(A[:N-K+1]), range(K-1, N)))
    # ma = sum(a*comb[i]%mod for a, i in zip((A[K-1:]), range(K-1, N)))
    ans = sum((a-b)*comb[i]%mod for b, a, i in zip(reversed(A[:N-K+1]), A[K-1:], list(range(K-1, N))))
    # print(mi%mod, ma%mod)
    print((ans%mod))


if __name__ == '__main__':
    main()
# Original solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

class Combination:
    """
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv
n,k = inpl()
a = sorted(inpl())
res = 0
comb = Combination(10**5+10)
for i in range(n):
    if n-i-1 < k-1: continue
    p = comb(n-i-1,k-1)
    res -= a[i] * p
    res += a[-1-i] * p
    res %= mod
print(res)

# Optimized solution
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

class Combination:
    """
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

comb = Combination(100010)
n,k = inpl()
a = inpl()
a.sort()
mx = 0
for i in range(n):
    if n-i-1 < k-1: continue
    tmp = comb(n-i-1,k-1)
    # print(tmp)
    mx += tmp*a[-i-1] - tmp*a[i]
    mx %= mod
print(mx)
# Original solution
import math
import operator as op
from functools import reduce


def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, list(range(n, n-r, -1)), 1)
    denom = reduce(op.mul, list(range(1, r+1)), 1)
    return numer // denom


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

total = 0

for i in range(n-m+1):
    p = n - i - 1
    q = m - 1

    c = ncr(p, q)

    total -= c * a[i]
    total += c * a[-1-i]

print((total % (10**9 + 7)))


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

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

total = 0
for i in range(n - k + 1):
    p = n - i - 1
    q = k - 1

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

print(total)
# Original solution
import itertools
import math

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

mod = 10**9+7

# print(list(itertools.combinations(range(6), 3)))
# A = range(6)
# N = 6
# K = 3

# 二項係数を素数で割った余り
def combMod(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    # fact = [-1 for _ in range(n+1)]
    # fact[0] = 1
    # fact[1] = 1
    # inv = [-1 for _ in range(n+1)]
    # inv[0] = 0
    # inv[1] = 1
    # factinv = [-1 for _ in range(n+1)]
    # factinv[0] = 1
    # factinv[1] = 1
    fact = [1, 1]
    factinv = [1, 1]
    inv = [0, 1]
    
    for i in range(2, n + 1):
        # fact[i] = ((fact[i-1] * i) % p)
        # inv[i] = ((-inv[p % i] * (p // i)) % p)
        # factinv[i] = ((factinv[i-1] * inv[i]) % p)
        fact.append((fact[-1] * i) % p)
        inv.append((-inv[p % i] * (p // i)) % p)
        factinv.append((factinv[-1] * inv[-1]) % p)


    return fact[n] * factinv[r] * factinv[n-r] % p


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

print(ans)

# Optimized solution
# import itertools
# import math

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

mod = 10**9+7

# print(list(itertools.combinations(range(6), 3)))
# A = range(6)
# N = 6
# K = 3

# 二項係数を素数で割った余り
def combMod(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
    # fact = [-1 for _ in range(n+1)]
    # fact[0] = 1
    # fact[1] = 1
    # inv = [-1 for _ in range(n+1)]
    # inv[0] = 0
    # inv[1] = 1
    # factinv = [-1 for _ in range(n+1)]
    # factinv[0] = 1
    # factinv[1] = 1
fact = [1, 1]
factinv = [1, 1]
inv = [0, 1]
p = mod
    
for i in range(2, N + 1):
    # fact[i] = ((fact[i-1] * i) % p)
    # inv[i] = ((-inv[p % i] * (p // i)) % p)
    # factinv[i] = ((factinv[i-1] * inv[i]) % p)
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)




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

print(ans)
# Original solution
# import itertools
# import math

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

mod = 10**9+7

# print(list(itertools.combinations(range(6), 3)))
# A = range(6)
# N = 6
# K = 3

# 二項係数を素数で割った余り
def combMod(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
fact = [-1 for _ in range(N+1)]
fact[0] = 1
fact[1] = 1
inv = [-1 for _ in range(N+1)]
inv[0] = 0
inv[1] = 1
factinv = [-1 for _ in range(N+1)]
factinv[0] = 1
factinv[1] = 1
# fact = [1, 1]
# factinv = [1, 1]
# inv = [0, 1]
p = mod
    
for i in range(2, N + 1):
    fact[i] = ((fact[i-1] * i) % p)
    inv[i] = ((-inv[p % i] * (p // i)) % p)
    factinv[i] = ((factinv[i-1] * inv[i]) % p)
    # fact.append((fact[-1] * i) % p)
    # inv.append((-inv[p % i] * (p // i)) % p)
    # factinv.append((factinv[-1] * inv[-1]) % p)




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

print(ans)

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

# conbModに使うfactとfactinvを求める
def factorialMod(n, p):
    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)
    return fact, factinv

# 二項係数を素数で割った余り
def combMod(n, r, fact, factinv, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

ans = 0
fact, factinv = factorialMod(N, mod)
for i in range(N):
    num = combMod(i, K-1, fact, factinv, mod) % mod
    ans += num * A[i] % mod
    ans -= num * A[N-i-1] % mod
    ans %= mod

print(ans)