CodeEff/ECCO huggingface dataset:
# Original 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)

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

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

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)
# Original solution

class BinomialCoefficient(object):

    def __init__(self, N=510000, MOD=10**9+7):
        self.fac = [1, 1]
        self.finv = [1, 1]
        self.inv = [0, 1]
        self.MOD = MOD
        for i in range(2, N + 1):
            self.fac.append((self.fac[-1] * i) % MOD)
            self.inv.append((-self.inv[MOD % i] * (MOD // i)) % MOD)
            self.finv.append((self.finv[-1] * self.inv[-1]) % MOD)

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


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

A.sort()
bc = BinomialCoefficient()
nCk = bc.comb(N, K)
ans = 0
for i in range(N - 1):
    c = nCk - bc.comb(i + 1, K) - bc.comb((N - i - 1), K)
    ans += c * (A[i + 1] - A[i]) % MOD
    ans %= MOD
print(ans)


# Optimized solution

class BinomialCoefficient(object):

    def __init__(self, N=510000, MOD=10**9+7):
        self.fac = [1, 1]
        self.finv = [1, 1]
        self.inv = [0, 1]
        self.MOD = MOD
        for i in range(2, N + 1):
            self.fac.append((self.fac[-1] * i) % MOD)
            self.inv.append((-self.inv[MOD % i] * (MOD // i)) % MOD)
            self.finv.append((self.finv[-1] * self.inv[-1]) % MOD)

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


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

A.sort()
bc = BinomialCoefficient(N)
nCk = bc.comb(N, K)
ans = 0
for i in range(N - 1):
    c = nCk - bc.comb(i + 1, K) - bc.comb((N - i - 1), K)
    ans += c * (A[i + 1] - A[i]) % MOD
    ans %= MOD
print(ans)
# Original solution
from bisect import bisect_right, bisect_left

class Combination:
    def __init__(self, size, mod=10**9 + 7):
        self.size = size + 2
        self.mod = mod
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % self.mod
            self.inv[i] = -self.inv[self.mod % i] * (self.mod // i) % self.mod
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % self.mod

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % self.mod

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % self.mod) % self.mod

    def nhr(self, n, r):  # 重複組合せ
        return self.ncr(n + r - 1, n - 1)

    def factN(self, n):
        if n < 0:
            return 0
        return self.fact[n]

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

ans = 0
for i, a in enumerate(A):
    mi = min(i, bisect_right(A, a) - 1)
    mx = N - max(i + 1, bisect_left(A, a))

    ans = (ans + (comb.ncr(mi, K - 1) - comb.ncr(mx, K - 1)) * a) % MOD

print(ans)

# Optimized solution
MOD = 10**9 + 7

class Combination:
    def __init__(self, size):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

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

A.sort()
ans = 0
for i, a in enumerate(A):
    mi = comb.ncr(N - i - 1, K - 1)
    mx = comb.ncr(i, K - 1)
    ans += (mx - mi) * a
    ans %= MOD
print(ans)
# Original solution
MOD = 10**9 + 7

class Combination:
    def __init__(self, size):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

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

A.sort()
ans = 0
for i, a in enumerate(A):
    mi = comb.ncr(N - i - 1, K - 1)
    mx = comb.ncr(i, K - 1)
    ans += (mx - mi) * a
    ans %= MOD
print(ans)


# Optimized solution
MOD = 10**9 + 7

class Combination:
    def __init__(self, size):
        self.size = size + 2
        self.fact = [1, 1] + [0] * size
        self.factInv = [1, 1] + [0] * size
        self.inv = [0, 1] + [0] * size

        for i in range(2, self.size):
            self.fact[i] = self.fact[i - 1] * i % MOD
            self.inv[i] = -self.inv[MOD % i] * (MOD // i) % MOD
            self.factInv[i] = self.factInv[i - 1] * self.inv[i] % MOD

    def npr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * self.factInv[n - r] % MOD

    def ncr(self, n, r):
        if n < r or n < 0 or r < 0:
            return 0
        return self.fact[n] * (self.factInv[r] * self.factInv[n - r] % MOD) % MOD

    def nhr(self, n, r):  # 重複組合せ: x_1 + ... + x_n = r
        return self.ncr(n + r - 1, n - 1)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
comb = Combination(N + 100)
A.sort()

ans = 0
for i, a in enumerate(A):
    ans += a * comb.ncr(i, K - 1)
    ans -= a * comb.ncr(N - i - 1, K - 1)
    ans %= MOD
print(ans)
# Original solution
def comb(a,b,m):
    if b == 0 or b == a:
        ret = 1
    else:
        ret = 1
        for i in range(a-b+1,a+1):
            ret = (ret * i) % m
        for i in range(1,b+1):
            ret = (ret * pow(i,m-2,m) ) % m
    return ret

mod = 10**9+7
n, k = [ int(v) for v in input().split() ]
num = sorted([ int(v) for v in input().split() ])

max_list2 = [ (i, i-k+1) for i in range(n) ]
max_list2 = [ comb(a, b, mod) if b >= 0 else 0 for a, b in max_list2 ]
min_list_2 = list(reversed(max_list2))

max_min_list = [ (max_list2[i] - min_list_2[i]) * num[i] for i in range(n) ] 
print((sum(max_min_list) % mod))

# Optimized solution
mod = 10**9+7
n, k = [ int(v) for v in input().split() ]
num = sorted([ int(v) for v in input().split() ])


max_list2 = [ (i, i-k+1) for i in range(n) ]
max_list = []
for a, b in max_list2:
    if b < 0:
        max_list.append(0)
    elif b == 0:
        max_list.append(1)
    else:
        s = max_list[-1]
        s *= a * pow(b, mod-2, mod)
        max_list.append(s%mod)

min_list = list(reversed(max_list))

max_min_list = [ (max_list[i] - min_list[i]) * num[i] for i in range(n) ] 
print((sum(max_min_list) % mod))
# Original solution
from math import factorial

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

if K==1:print((0))
else:
  def comb(n,k):
    return factorial(n)*pow(factorial(n-k)*factorial(k), -1, mod)

  A1=sorted(A)
  A2=A1[::-1]
  
  ans=0
  for i in range(N-K+1):
    ans += (A2[i]-A1[i])*comb(N-i-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

if K==1:print((0))
else:
  factorial=[1 for i in range(N+1)]
  for i in range(1,N+1):
    if i==1:factorial[i]=1
    else:factorial[i] = factorial[i-1]*i % mod
  
  def comb(n,k):
    return factorial[n]*pow(factorial[n-k]*factorial[k], -1, mod)

  A1=sorted(A)
  A2=A1[::-1]
  
  ans=0
  for i in range(N-K+1):
    ans += (A2[i]-A1[i])*comb(N-i-1,K-1)
  ans %= mod

  print(ans)
# Original solution
N, K = list(map(int,input().split()))

A = list(map(int,input().split()))
 
MAX = 10 ** 5 + 1
MOD = 10 ** 9 + 7
 
fac = [0] * (MAX + 1)
fac[0] = 1
fac[1] = 1
for i in range(2, MAX + 1):
    fac[i] = fac[i - 1] * i % MOD
    
finv = [0] * (MAX + 1)
finv[MAX] = pow(fac[MAX], MOD - 2, MOD)
  
for i in range(MAX, 0, -1):
    finv[i - 1] = finv[i] * i % MOD
 
def comb(n, k):
    if n < k or k < 0:
        return 0
    return (fac[n] * finv[k] * finv[n - k]) % MOD

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

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

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

A.sort()


MAX = 10 ** 5 + 1
MOD = 10 ** 9 + 7
fac = [0]*(MAX+1)
fac[0] = 1
fac[1] = 1
for i in range(2,MAX+1):
    fac[i] = fac[i-1]*i%MOD

finv = [0]*(MAX+1)
finv[MAX] = pow(fac[MAX], MOD-2, MOD)

for i in range(MAX, 0, -1):
    finv[i-1] = finv[i] * i % MOD

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

ans = 0
for i in range(N):
    if K - 1 <= i:
        ans += comb(i,K-1) * A[i]
    if K-1+i < N:
        ans -= comb(N-i-1,K-1)*A[i]
    ans %= MOD
print(ans)
# Original solution
def power(a,n):
    x=1
    L=list(format(n,'b'))
    l=len(L)
    for i in range(l):
        if int(L[l-i-1])==1:        
            x=(x*a)%(10**9+7)
        a=(a*a)%(10**9+7)
    return x
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
b=sorted(A)
c=sorted(A,reverse=True)
D=[]
M=0
for i in range(N-K+1):
    x=1
    y=1
    for l in range(K-1):
        x=(x*(N-i-1-l))%(10**9+7)
        y=(y*(l+1))%(10**9+7)
    y=power(y,10**9+5)
    m=(x*y*(c[i]-b[i]))
    if m>0:
        m=m%(10**9+7)
    M+=m
    M%(10**9+7)

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







# Optimized solution
def power(a,n):
    x=1
    L=list(format(n,'b'))
    l=len(L)
    for i in range(l):
        if int(L[l-i-1])==1:        
            x=(x*a)%(10**9+7)
        a=(a*a)%(10**9+7)
    return x
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))
b=sorted(A)
c=sorted(A,reverse=True)
D=[]
M=0
x=1
y=1
for l in range(K-1):
    x=(x*(N-l-1))%(10**9+7)
    y=(y*(l+1))%(10**9+7)
y=power(y,10**9+5)
M+=(x*y*(c[0]-b[0]))%(10**9+7)
for i in range(1,N-K+1):
    y1=power(N-i,10**9+5)
    x=(x*y1)%(10**9+7)
    x=(x*(N-K-i+1))%(10**9+7)  
    m=(x*y*(c[i]-b[i]))
    if m>0:
        m=m%(10**9+7)
    M+=m
    M%(10**9+7)

print((M%(10**9+7)))
# Original solution
def cmb(n,r,mod):
  a=1
  b=1
  r = min(r,n-r)
  for i in range(r):
    a = a*(n-i)%mod
    b = b*(i+1)%mod
  return a*pow(b,mod-2,mod)%mod



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

# Optimized solution
# kurimu_usamaro様のメソッドを拝借しています。
class Combi():
    def __init__(self, N, mod):
        self.power = [1 for _ in range(N+1)]
        self.rev = [1 for _ in range(N+1)]
        self.mod = mod
        for i in range(2, N+1):
            self.power[i] = (self.power[i-1]*i) % self.mod
        self.rev[N] = pow(self.power[N], self.mod-2, self.mod)
        for j in range(N, 0, -1):
            self.rev[j-1] = (self.rev[j]*j) % self.mod
 
    def com(self, K, R):
        if K < R:
            return 0
        else:
            return ((self.power[K])*(self.rev[K-R])*(self.rev[R])) % self.mod
 
    def pom(self, K, R):
        if K < R:
            return 0
        else:
            return (self.power[K])*(self.rev[K-R]) % self.mod
 
def main():
  mod = 10**9+7
  N,K = list(map(int,input().split()))
  A = list(map(int,input().split()))
  A.sort()
  c = Combi(N,mod)

  ans = 0
  if K == 1:
    print((0))
  else:
    for i, a in enumerate(A):
      ans += c.com(i, K-1)*a
      ans -= c.com(N-1-i, K-1)*a
      ans %= mod
    print((ans%mod))

if __name__ == "__main__":
    main()
# Original solution
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  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
def com(n,k):
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD

MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N-1,-1,-1):
  for i in range(j-K+2):
    mi = A[i]
    ma = A[j]
    #print(ma,mi,i,j,L[j-i-1])
 #     print(j-i-1,com(j-i-1,K-2))
    ans+=(ma-mi)*com(j-i-1,K-2)
print((ans%(10**9+7)))

# Optimized solution
def COMint():
  fac[0]=1
  fac[1]=1
  finv[0]=1
  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
def com(n,k):
  if n<k:
    return 0
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD
MOD=10**9+7
N,K=[int(x) for x in input().split()]
A= [int(x) for x in input().split()]
A= sorted(A)
fac=[0]*(N+1)
inv=[0]*(N+1)
finv=[0]*(N+1)
COMint()
ans=0
#print(fac)
for j in range(N):
  ans+=(A[j])*com(j,K-1)
  ans-=(A[j])*com(N-1-j,K-1)
print((ans%(10**9+7)))
# Original solution
MOD = 10**9+7
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A.sort(reverse=True)

f = [1]
for i in range(10**5+7):
    f.append(f[-1]*(i+1)%MOD)
def nCr(n, r, mod=MOD):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

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


# Optimized solution
MOD = 10**9+7

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

f = [1]
for i in range(10**5):
    f.append(f[-1] * (i+1) % MOD)

def nCr(n, r, mod=MOD):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

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

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

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

f = [1]
for i in range(10**5):
    f.append(f[-1] * (i+1) % MOD)

def nCr(n, r, mod=MOD):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

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

print(ans)


# Optimized solution
import sys
readline = sys.stdin.buffer.readline
MOD = 10**9+7
N, K = list(map(int, readline().split()))
A = list(map(int, readline().split()))
A.sort()

f = [1]
for i in range(10**5):
    f.append(f[-1] * (i+1) % MOD)
def nCr(n, r, mod=MOD):
    return f[n] * pow(f[r], mod-2, mod) * pow(f[n-r], mod-2, mod) % mod

ans = 0
for i in range(N-K+1):
    ans += (A[-1-i] - A[i]) * nCr(N-1-i, K-1)
    ans %= MOD
print(ans)
# Original solution
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np

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



def main():
	n,k=list(map(int,input().split()))
	A=list(map(int,input().split()))
	A.sort()
	ans=0
	for i in range(n-k+1):
		ans+=((comb(k-1+i,k-1,mod)*A[k-1+i])-(comb(k-1+i,k-1,mod)*A[n-k-i]))%mod
		ans=ans%mod
	print(ans)


if __name__ == "__main__":
	main()


# Optimized solution
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
#import numpy as np


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 main():
	n,k=list(map(int,input().split()))
	A=list(map(int,input().split()))
	A.sort()
	
	comb=[1]
	for i in range(n-k):
		comb.append((comb[-1]*(k+i)*pow(i+1,mod-2,mod))%mod)
	ans=0
	
	for i in range(n-k+1):
		ans+=((comb[i]*A[k-1+i])-(comb[i]*A[n-k-i]))%mod
		ans=ans%mod
	print(ans)


if __name__ == "__main__":
	main()
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()

mod=10**9+7
def modfc(x,y,p):
  f=1
  for i in range(x,y+1):
    f=f%p*i%p
  return f%p
def comb(x,y,p):
  return modfc(x-y+1,x,p)*pow(modfc(1,y,p),p-2,p)

maxs=0
for i in range(k,n+1):
  maxs+=a[i-1]*comb(i-1,k-1,mod)
  maxs%=mod
mins=0
for i in range(n-k+1):
  mins+=a[i]*comb(n-i-1,k-1,mod)
  mins%=mod
ans=(maxs-mins)%mod
print((ans+mod if ans<0 else ans))

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

mod=10**9+7
fcl=[1]*(n+1)
for i in range(1,n+1):
  fcl[i]=(fcl[i-1]*i)%mod
def comb(x,y,p):
  return ((fcl[x]*pow(fcl[y],p-2,p))%mod*pow(fcl[x-y],p-2,p))%mod

maxs=0
for i in range(k,n+1):
  maxs+=a[i-1]*comb(i-1,k-1,mod)
  maxs%=mod
mins=0
for i in range(n-k+1):
  mins+=a[i]*comb(n-i-1,k-1,mod)
  mins%=mod
ans=(maxs-mins)%mod
print((ans+mod if ans<0 else ans))
# Original solution
N_MAX = 10**6
MOD = 10**9 + 7
fac, finv, inv = [0]*N_MAX ,[0]*N_MAX, [0]*N_MAX
def com_init():
    fac[0], fac[1] = 1, 1
    finv[0], finv[1] = 1, 1
    inv[1] = 1
    for i in range(2, N_MAX):
        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
    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()))
al = list(map(int, input().split()))
com_init()
al.sort()

ans_max = 0
ans_min = 0
for i,a in enumerate(al):
    ans_max += a*com(i,k-1)
    ans_max%=MOD
    ans_min += a*com(n-1-i,k-1)
    ans_min%=MOD


print(((ans_max-ans_min)%MOD))

# Optimized solution
class Combination:
    def __init__(self, n_max=10**6, mod=10**9+7):
        # self._n_max = n_max
        self._fac, self._finv, self._inv = [0]*n_max, [0]*n_max, [0]*n_max
        self._fac[0], self._fac[1] = 1, 1
        self._finv[0], self._finv[1] = 1, 1
        self._inv[1] = 1
        for i in range(2, n_max):
            self._fac[i] = self._fac[i - 1] * i % MOD
            self._inv[i] = MOD - self._inv[MOD%i] * (MOD // i) % MOD
            self._finv[i] = self._finv[i - 1] * self._inv[i] % MOD
    def com(self, n, r):
        if n < r: return 0
        if n < 0 or r < 0: return 0
        return self._fac[n] * (self._finv[r] * self._finv[n - r] % MOD) % MOD


MOD = 10**9+7
# comb.com(10,3)
comb = Combination(10**5+1, MOD)
n,k = list(map(int, input().split()))
al=list(map(int, input().split()))  
al.sort(reverse=True)

fmax = 0
for i,a in enumerate(al):
    cnt = comb.com(n-i-1,k-1)
    fmax += cnt*a
    fmax%=MOD

al.sort()
fmin = 0
for i,a in enumerate(al):
    cnt = comb.com(n-i-1,k-1)
    fmin += cnt*a
    fmin%=MOD

ans = fmax-fmin
ans%=MOD
print(ans)
# Original solution
N, K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
MOD = 10**9+7
fac = [0]*(N+1) # 階乗テーブル
fac[0] = fac[1] = 1
inv = [0]*(N+1) # 逆元テーブル
inv[1] = 1
facinv = [0]*(N+1) # 階乗の逆元テーブル
facinv[0] = facinv[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
    facinv[i] = facinv[i-1] * inv[i] % MOD

def nCr(n,r):
    if n < r or n < 0 or r < 0:return 0
    else:return fac[n] * (facinv[r] * facinv[n-r] % MOD) % MOD

# 各要素が最大値、最小値として何回使われるか考える
maxsum = 0
minsum = 0
for i,a in enumerate(A):
    maxsum += nCr(i,K-1)*a
    maxsum %= MOD
    minsum += nCr(N-i-1,K-1)*a
    minsum %= MOD
ans = (maxsum-minsum)%MOD

print(ans)

# Optimized solution
N,K = list(map(int,input().split()))
A = list(map(int,input().split()))
A.sort()
MOD = 10**9+7
# 各aが何回最大最小として選ばれるか考える
ans = 0

class Combination:

    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(N+1)

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


for i in range(N):
    if i <= N-K:
        ans -= (A[i] * comb(N-1-i,K-1))%MOD
        ans %= MOD
print(ans)