CodeEff/ECCO huggingface dataset:
# Original solution
def factorial_mod(n, mod):
    a = 1
    for i in range(1, n + 1):
        a *= i
        a %= mod
    return a


def comb_mod(n, k, mod):
    if k > n or k < 0:
        return 0
    fact_n = factorial_mod(n, mod)
    fact_k = factorial_mod(k, mod)
    fact_n_k = factorial_mod(n - k, mod)
    return (fact_n * pow(fact_k, mod - 2, mod) * pow(fact_n_k, mod - 2, mod)) % mod


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

MOD = 10**9 + 7

ans_max = 0
for i in range(k - 1, n):
    ans_max += arr[i] * comb_mod(i, k - 1, MOD)
    ans_max %= MOD

arr.sort(reverse=True)

ans_min = 0
for i in range(k - 1, n):
    ans_min += arr[i] * comb_mod(i, k - 1, MOD)
    ans_min %= MOD

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

# Optimized solution
def prepare(n, MOD):
    f = 1
    factorials = [1]
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    inv = pow(f, MOD - 2, MOD)
    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


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


MOD = 10 ** 9 + 7
facts, invs = prepare(n, MOD)

ans_max = 0
for i in range(k - 1, n):
    ans_max += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

arr.sort(reverse=True)

ans_min = 0
for i in range(k - 1, n):
    ans_min += (arr[i] * facts[i] * invs[k - 1] * invs[i - k + 1]) % MOD

print(((ans_max - ans_min) % MOD))
# Original solution
MOD = 10**9 + 7
n,k,*a=list(map(int,open(0).read().split()))
a.sort()
comb = 1
ans = 0
for i in range(k,n+1):
    ans += (a[i-1] - a[-i]) * (comb % MOD)
    comb = comb * i // (i-k+1)
    ans = ans%MOD
print(ans)

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


def comb(n, r):
    if n < r or r < 0:
        return 0
    nume = 1
    deno = 1
    for i in range(1, r + 1):
        nume *= (n - i + 1)
        deno *= (i)
    return nume//deno


combs=[comb(i, K - 1)%P for i in range(N+1)]

for i, a in enumerate(A):
    ans = (ans + a * combs[i]) % P
    ans = (ans - a * combs[N - i - 1]) % P

print(ans)


# Optimized solution
N, K = list(map(int, input().split()))
A = sorted(list(map(int, input().split())))
P = int(1e9+7)
ans = 0

kaizyo = [0]
kaizyo_inv = [0]
tmp = 1
for i in range(1, N+1):
    tmp = (tmp*i) % P
    kaizyo.append(tmp)
    kaizyo_inv.append(pow(tmp, P - 2, P))


def comb(n, r):
    if n < r or n == 0:
        return 0
    elif n == r or r == 0:
        return 1
    else:
        return kaizyo[n] * kaizyo_inv[r] * kaizyo_inv[n - r]


combs=[comb(i, K - 1)%P for i in range(N+1)]

for i, a in enumerate(A):
    ans = (ans + a * combs[i]) % P
    ans = (ans - a * combs[N - i - 1]) % P

print(ans)
# Original solution
from math import factorial as jj
mod=10**9+7
n,k=list(map(int,input().split()))
l=sorted(map(int,input().split()))
ans=0

ml=jj(n-1)//(jj(k-1)*jj(n-k))
ans-=ml*(l[0]-l[n-1])%mod
for i in range(1,n-k+1):
  ml=ml*(n-i-k+1)//(n-i)
  ans-=ml*(l[i]-l[n-1-i])%mod
print((ans%mod))


# Optimized solution
n,k=list(map(int,input().split()))
l=sorted(list(map(int,input().split())))
mod=10**9+7
fact=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2,n+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
for i in range(n):
    if i>=k-1:
        ans+=l[i]*fact[i]*inv[k-1]*inv[i-k+1]%mod
    if n-i-1>=k-1:
        ans-=l[i]*fact[n-i-1]*inv[k-1]*inv[n-i-1-k+1]%mod
    ans%=mod
print(ans)
# Original solution
def main():
    def pre(n,mod):
        a=1
        for i in range(1,n+1):
            a=(a*i)%mod
        an=a
        b=pow(a,mod-2,mod)
        bs=[1]*(n+1)
        bs[n]=b
        for i in range(n,1,-1):
            b=b*i%mod
            bs[i-1]=b
        return an,bs

    n,k=list(map(int,input().split()))
    a=sorted(list(map(int,input().split())))
    mod=10**9+7
    an,bs=pre(n,mod)
    ans=0
    for i in range(k,n+1):
        if i==k:
            c=1
        else:
            c=(c*(i-2)*pow(i-k,mod-2,mod))%mod
        for j in range(0,n-i+1):
            ans=(ans+(a[i+j-1]-a[j])*c)%mod
    print(ans)

if __name__ == '__main__':
    main()

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

if __name__ == '__main__':
    main()
# Original solution
def modpow(x,p,mod):
    ret=1
    for i in range(100):
        if (1<<i)&p:
            ret=ret*x%mod
        x=x*x%mod
    return ret

def inverse_table(n,mod):
    table=[]
    for i in range(0,n+1):
        table.append(modpow(i,mod-2,mod))
    return table

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


it=inverse_table(10**5,1000000007)
ft=factorial_table(10**5,1000000007)

memo={}
def c(n,k,it,ft,mod):
    return ft[n]*it[ft[k]]*it[ft[(n-k)]]%mod

n,k=list(map(int,input().split()))
a=sorted(map(int,input().split()))
hoge=[]
while True:
    hoge.append(c(n-1,k-1,it,ft,1000000007))
    if n-1==k-1:
        break
    n-=1
ans=0
for i in range(len(hoge)):
    ans-=hoge[i]*a[i]%1000000007
    ans+=hoge[i]*a[-i-1]%1000000007
    ans%=1000000007
print(ans)



# Optimized solution
def modpow(x,p,mod):
    ret=1
    for i in range(64):
        if (1<<i)&p:
            ret=ret*x%mod
        x=x*x%mod
    return ret

def inverse_table(n,mod):
    table=[]
    for i in range(0,n+1):
        table.append(modpow(i,mod-2,mod))
    return table

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

def inverse_factorial_table(n,it,mod):
    table=[1]
    for i in range(1,n+1):
        table.append(table[-1]*it[i]%mod)
    return table

it=inverse_table(10**5,1000000007)
ft=factorial_table(10**5,1000000007)
ift=inverse_factorial_table(10**5,it,1000000007)
def c(n,k,it,ft,mod):
    return ft[n]*ift[k]*ift[(n-k)]%mod
n,k=list(map(int,input().split()))
a=sorted(map(int,input().split()))
hoge=[]
while True:
    hoge.append(c(n-1,k-1,it,ft,1000000007))
    if n-1==k-1:
        break
    n-=1
ans=0
for i in range(len(hoge)):
    ans-=hoge[i]*a[i]%1000000007
    ans+=hoge[i]*a[-i-1]%1000000007
    ans%=1000000007
print(ans)
# Original solution
import math
mod = 10**9+7
n,k = list(map(int,input().split()))
b = math.factorial(k-1)%mod
def comb(p):
    if p >= k-1:
        a = math.factorial(p)%mod
        c = math.factorial(p-k+1)%mod
        return (a*pow(b*c, mod-2, mod))%mod
    else:
        return 0
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += comb(i)*(A[i]-A[n-i-1])
    ans %= mod
print(ans)


# Optimized solution
import math
mod = 10**9+7
n,k = list(map(int,input().split()))
b = math.factorial(k-1)%mod
fact = [0]*(n-k+1)
a = math.factorial(k-1)%mod
c = 1
for i in range(k-1, n):
    if i == k-1:
        fact[0] = (a*pow(b*c, mod-2, mod))%mod
    else:
        a = a*i%mod
        c = c*(i-k+1)%mod
        fact[i-k+1] = (a*pow(b*c, mod-2, mod))%mod
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += fact[i-k+1]*(A[i]-A[n-i-1])
    ans %= mod
print(ans)
# Original solution
n, k = list(map(int, input().split(' ')))
aaa = sorted(list(map(int, input().split(' '))))
mmm = 1000000000 + 7
# 二項係数 mod [検索]
fac = []
inv = []
inv_fac = []
def init(n):
    fac.append(1)
    fac.append(1)
    inv.append(0)
    inv.append(1)
    inv_fac.append(1)
    inv_fac.append(1)
    for i in range(2, n):
        fac.append(fac[-1] * i % mmm)
        inv.append(mmm - inv[mmm%i] * (mmm // i) % mmm)
        inv_fac.append(inv_fac[-1] * inv[-1] % mmm)

def choice(a, b):
    if a < b:
        return 0
    return fac[a] * (inv_fac[b] * inv_fac[a-b] % mmm) % mmm

ans = 0
init(1000000)
for st in range(n):
    for ed in range(st+1, n):
        choice_pattern = choice(ed-1 - st, k-2)  # [st + 1: ed - 1] の範囲から k-2個取るパターン数
        ans = (ans + (aaa[ed] - aaa[st]) * choice_pattern) % mmm
        # print(st, ed, ':::', ed-1 - st, k-2, "stCed=", choice_pattern, 'hoge=', (aaa[ed]-aaa[st]), ans)
print(ans)

# Optimized solution
n, k = list(map(int, input().split(' ')))
aaa = sorted(list(map(int, input().split(' '))))
mmm = 1000000000 + 7
# 二項係数 mod [検索]
fac = []
inv = []
inv_fac = []
def init(n):
    fac.append(1)
    fac.append(1)
    inv.append(0)
    inv.append(1)
    inv_fac.append(1)
    inv_fac.append(1)
    for i in range(2, n):
        fac.append(fac[-1] * i % mmm)
        inv.append(mmm - inv[mmm%i] * (mmm // i) % mmm)
        inv_fac.append(inv_fac[-1] * inv[-1] % mmm)

def choice(a, b):
    if a < b:
        return 0
    return fac[a] * (inv_fac[b] * inv_fac[a-b] % mmm) % mmm
ans = 0
init(1000000)
for target in range(n):
    p_pattern = choice(target, k-1)
    #print(target, k-1, 'p_choice', choice(target, k-1))
    n_pattern = choice(n-target-1, k-1)
    #print(n-target-1, k-1, 'n_choice', choice(n-target-1, k-1))
    ans = (ans + aaa[target] * p_pattern - aaa[target] * n_pattern) % mmm
    #print(aaa[target], target-1, n-target, k-1, p_pattern, n_pattern)
print((ans % mmm))
# Original solution
import math
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
mod = 10 ** 9 + 7


def com_init():
    MAX = 10 ** 5 + 1
    fac = [0] * MAX
    finv = [0] * MAX
    inv = [0] * MAX
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, 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
    return fac, finv

fac, finv = com_init()

def nCr(n,r):
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod 

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


MAX_SUM = 0
MIN_SUM = 0

A.sort() # asc
for j in range(n - k + 1): # 小さい方からk個
    MIN_SUM += A[j] * nCr(n - j - 1, k - 1)

A = A[::-1] # desc
for i in range(n - k + 1): #大きい方からk個
    MAX_SUM += A[i] * nCr(n - i - 1, k - 1)


print(((MAX_SUM - MIN_SUM + mod) % mod))

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

mod = 10 ** 9 + 7 # (modの値をすごく大きくすれば（5乗とか）、普通のnCrとして使える)
MAX = 10 ** 5 + 1 # これは変動するので注意。せいぜい10 ** 6くらいまでがTLEの限界

def com_init(MAX, mod):
    fac = [0] * MAX
    finv = [0] * MAX
    inv = [0] * MAX
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, 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
    return fac, finv

fac, finv = com_init(MAX, mod)

def nCr(n, r):
    if n < r:
        return 0
    elif n < 0 or r < 0:
        return 0
    return fac[n] * (finv[r] * finv[n - r] % mod) % mod

nums.sort()
MIN = 0
for i in range(n - k + 1):
    MIN += (nums[i] * nCr(n - i - 1, k - 1)) % mod

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

ans = (MAX - MIN) % mod
print(ans)
# Original solution
#!/usr/bin/env python3
import sys
import functools
INF = float("inf")

MOD = 1000000007  # type: int


@functools.lru_cache(maxsize=None)
def cmb(n, r):
    r = min(r, n-r)
    if r == 0:
        return 1
    if r == 1:
        return n

    numer = [n - r + k + 1 for k in range(r)]
    denom = [k + 1 for k in range(r)]

    for p in range(2, r+1):
        pivot = denom[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1, r, p):
                numer[k - offset] /= pivot
                denom[k] /= pivot

    result = 1
    for k in range(r):
        if numer[k] > 1:
            result *= int(numer[k])

    return result


def solve(N: int, K: int, A: "List[int]"):

    A.sort()

    ans = 0
    for i in range(N):
        # A[i]が最大として選ばれるケース
        if i >= K-1:
            ans += cmb(i, K-1)*A[i]
            ans %= MOD

        # A[i]が最小として選ばれるケース
        if N-i-1 >= K-1:
            ans -= cmb(N-i-1, K-1)*A[i]
            ans %= MOD
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)


if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
import functools
INF = float("inf")

MOD = 1000000007  # type: int


class Combination(object):
    """
    組み合わせを求める。
    cmbで使いうる最大のNまでを前処理で準備。
    """

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

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


def solve(N: int, K: int, A: "List[int]"):

    cmb = Combination(N, mod=MOD)

    A.sort()

    ans = 0
    for i in range(N):
        # A[i]が最大として選ばれるケース
        if i >= K-1:
            ans += cmb(i, K-1)*A[i]
            ans %= MOD

        # A[i]が最小として選ばれるケース
        if N-i-1 >= K-1:
            ans -= cmb(N-i-1, K-1)*A[i]
            ans %= MOD
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)


if __name__ == '__main__':
    main()
# Original solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
mod = 10 ** 9 + 7

fac=[]
finv=[]
inv=[]
def combi_init():
    fac.append(1)
    fac.append(1)
    finv.append(1)
    finv.append(1)
    inv.append(1)
    inv.append(1)

    for i in range(2,N+1):
        fac.append(fac[i-1]*i%mod)
        inv.append(mod-mod//i*inv[mod%i]%mod)
        finv.append(finv[i-1]*inv[i]%mod)

def combi_mod(n,k):
    combi_init()
    ret=fac[n]*finv[n-k]*finv[k]%mod
    return ret

A.sort()

ans=0

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

fac=[]
finv=[]
inv=[]
def combi_init():
    fac.append(1)
    fac.append(1)
    finv.append(1)
    finv.append(1)
    inv.append(1)
    inv.append(1)

    for i in range(2,N+1):
        fac.append(fac[i-1]*i%mod)
        inv.append(mod-mod//i*inv[mod%i]%mod)
        finv.append(finv[i-1]*inv[i]%mod)

def combi_mod(n,k):

    ret=fac[n]*finv[n-k]*finv[k]%mod
    return ret

A.sort()

ans=0
combi_init()
for i in range(N-1,K-2,-1):
    ans+=(A[i]-A[(N-1)-i])*combi_mod(i,K-1)


ans%=mod

print(ans)
# Original solution
dic = {}

def comb(n,k,p):
  from math import factorial
  
  if n / 2 < k:
    k = n - k
  
  if n<0 or k<0 or n<k: 
    return 0
  if n==0 or k==0: 
    return 1
  
  a=fact_mod[n] %p
  b=fact_mod[k] %p
  c=fact_mod[n-k] %p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p

def power_func(a,b,p):
  if (a,b) in dic:
    return dic[(a,b)]
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    dic[(a,b)] = d*d %p
    return d*d %p
  if b%2==1:
    dic[(a,b)] = (a*power_func(a,b-1,p))%p
    return (a*power_func(a,b-1,p))%p

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

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

count = 0
i = n - 1
j = k - 1
k = 0
while True:
  tmp = comb(i, j, 10**9+7)
  count += tmp * a[-1-k]
  count -= tmp * a[k]
  count = count % (10 ** 9 + 7)
  if i == j:
    break
  i -= 1
  k += 1
    
print(count)

# Optimized solution
dic = {}

def comb(n,k,p):
  from math import factorial
  
  if n / 2 < k:
    k = n - k
  
  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):
  if (a,b) in dic:
    return dic[(a,b)]
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    dic[(a,b)] = d*d %p
    return d*d %p
  if b%2==1:
    dic[(a,b)] = (a*power_func(a,b-1,p))%p
    return (a*power_func(a,b-1,p))%p

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


count = 0
i = n - 1
j = k - 1
k = 0
while True:
  tmp = comb(i, j, 10**9+7)
  count += tmp * a[-1-k]
  count -= tmp * a[k]
  count = count % (10 ** 9 + 7)
  if i == j:
    break
  i -= 1
  k += 1
    
print(count)
# Original solution
#nCr % mod
def comb_mod(n, r, mod):
  x = 1
  y = 1
  for i in range(r):
    x = (x * (n - i)) % mod
    y = (y * (i + 1)) % mod
    
  ans = x * pow_k(y, mod-2, mod)
  
  return ans % mod
  
# x^n % mod
def pow_k(x, n, mod):
  if n == 0:
    return 1

  K = 1
  while n > 1:
    if n % 2 != 0:
      K = (x * K) % mod
        
    x = (x * x) % mod

    n //= 2

  return (x * K) % mod

def main():
  MOD = 10 ** 9 + 7
  n,k = list(map(int,input().split()))
  a = sorted(list(map(int,input().split())))
  
  dic = {}
  
  ans = 0
  for i in range(n):
    if i-k+1 >= 0:
      if (i,k-1) in dic:
        tmp = dic[(i,k-1)]
      else:
        tmp = comb_mod(i,k-1,MOD)
        dic[(i,k-1)] = tmp
        
      ans += comb_mod(i,k-1,MOD) * a[i]
      
    if n-i-k >= 0:
      if (n-i-1,k-1) in dic:
        tmp = dic[(n-i-1,k-1)]
        
      else:
        tmp = comb_mod(n-i-1,k-1,MOD)
        dic[(n-i-1,k-1)] = tmp
        
      ans -= comb_mod(n-i-1,k-1,MOD) * a[i]
      
    ans %= MOD
      
  print(ans)

main()

# Optimized solution
# x^n % mod
def pow_k(x, n, mod):
  if n == 0:
    return 1

  K = 1
  while n > 1:
    if n % 2 != 0:
      K = (x * K) % mod
        
    x = (x * x) % mod

    n //= 2

  return (x * K) % mod

def main():
  MOD = 10 ** 9 + 7
  n,k = list(map(int,input().split()))
  a = sorted(list(map(int,input().split())))
  
  g_k = 1
  for i in range(1,k):
    g_k *= i
    g_k %= MOD
    
  gyakugen = pow_k(g_k,MOD-2,MOD)
  array = [0] * (n+1)
  array[k-1] = g_k
  
  for i in range(k, n+1):
    array[i] = array[i-1] * i * pow_k(i-k+1,MOD-2,MOD)
    array[i] %= MOD
    
  ans = 0
  for i in range(n):
    if i-k+1 >= 0:        
      ans += array[i] * gyakugen * a[i]
      
    if n-i-k >= 0:
      ans -= array[n-i-1] * gyakugen * a[i]
      
    ans %= MOD
      
  print(ans)

main()
# Original solution
N, K = list(map(int, input().split()))
lis = list(map(int, input().split()))
p = 10**9+7
fac_lis = [[0] for i in range(N)]
fac_lis[0] = 1
max_ans = 0
min_ans = 0

for i in range(1,N):
  fac_lis[i] = fac_lis[i-1]*(i+1)

def combi(n,k):
  if n == k:
    return 1
  else:
  	return fac_lis[n-1]*pow(fac_lis[k-1], p-2, p)*pow(fac_lis[n-k-1], p-2, p) % p

lis.sort()
for i in range(K-1,N):
  max_ans += lis[i]*combi(i, K-1)
  max_ans %= p
  
for i in range(N-K+1):
  min_ans += lis[i]*combi(N-(i+1), K-1)
  min_ans %= p

ans = (max_ans - min_ans) % p
print(ans)

# Optimized solution
# フェルマーの小定理
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
 
m = 1000000007
 
fac = [0] * (N + 1)
fac[0] = 1
for i in range(N):
    fac[i + 1] = fac[i] * (i + 1) % m
 
 
def mcomb(n, k):
    if n == 0 and k == 0:
        return 1
    if n < k or k < 0:
        return 0
    return fac[n] * pow(fac[n - k], m - 2, m) * pow(fac[k], m - 2, m) % m
 
 
A.sort(reverse=True)
maxX = 0
for i in range(N - K + 1):
    maxX += A[i] * mcomb(N - (i + 1), K - 1)
    maxX %= m

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

print(((maxX - minX) % m))
# Original solution
class Cmb:
    def __init__(self, m=10**5, p=10**9+7):
        fct, inv = [1], [1]
        a, b = 1, 1
        for i in range(1, m+1):
            a = (a * i) % p
            b = (b * pow(i, p - 2, p)) % p
            fct.append(a)
            inv.append(b)
        self.fct = fct
        self.inv = inv
        self.p = p

    def calc(self, n, r):
        fct, inv = self.fct, self.inv
        if r < 0 or n < r:
            return 0
        return (fct[n] * inv[r] * inv[n - r]) % self.p


n, k = list(map(int, input().split()))
*A, = list(map(int, input().split()))
A.sort()
s = 0
p = 10**9+7
cmb = Cmb()
for i in range(n):
    s += (A[n-1-i]-A[i])*cmb.calc(n-1-i, k-1)
    s %= p
print(s)


# Optimized solution
n, k = list(map(int, input().split()))
*A, = list(map(int, input().split()))
A.sort()
p = 10**9+7
F, I = [1], [1]
for i in range(1, n+1):
    F.append((F[-1]*i) % p)
    I.append((I[-1]*pow(i, p-2, p)) % p)
s = 0
for i in range(k-1, n):
    s += (A[i]-A[n-(i+1)])*F[i]*I[k-1]*I[i-(k-1)]
    s %= p
print(s)
# Original solution
cookie = list(map(int, input().split()))
sc = sum(cookie)
cnt = -1
if cookie[0] == cookie[1] and cookie[1] == cookie[2]:
    if cookie[0] % 2 == 0:
        print(cnt)
        exit()
    else:
        print((0))
memo = [0]*3
while sum(cookie) == sc:
    memo[0] = (cookie[1] + cookie[2]) // 2
    memo[1] = (cookie[0] + cookie[2]) // 2
    memo[2] = (cookie[1] + cookie[0]) // 2
    for i in range(3):
        cookie[i] = memo[i]
    cnt += 1

print(cnt)

# Optimized solution
cookie = list(map(int, input().split()))
sc = sum(cookie)
cnt = 0
if cookie[0] == cookie[1] and cookie[1] == cookie[2] and cookie[0]%2 == 0:
    print((-1))
    exit()
memo = [0]*3
while cookie[0]%2 == 0 and cookie[1]%2 == 0 and cookie[2]%2 == 0:
    memo[0] = (cookie[1] + cookie[2]) // 2
    memo[1] = (cookie[0] + cookie[2]) // 2
    memo[2] = (cookie[1] + cookie[0]) // 2
    for i in range(3):
        cookie[i] = memo[i]
    cnt += 1

print(cnt)
# Original solution
a,b,c=list(map(int,open(0).read().split()))
e=(a-b)|(b-c)
print((bool(e|(a|b|c)%2)*(e^~-e).bit_length()-1))

# Optimized solution
a,b,c=list(map(int,input().split()))
e=(a-b)|(b-c)
print((bool(e|(a|b|c)%2)*(e^~-e).bit_length()-1))
# Original solution
a,b,c=list(map(int,input().split()))
e=(a-b)|(b-c)
print((bool(e|(a|b|c)%2)*(e^~-e).bit_length()-1))

# Optimized solution
a,b,c=list(map(int,input().split()))
e=(a-b)|(b-c)
print((bool(e|b%2)*(e^~-e).bit_length()-1))
# Original solution
a,b,c=list(map(int, input().split()))
d=0
while d<10**5:
    if a%2==1 or b%2==1 or c%2==1:break
    a,b,c=b/2+c/2,a/2+c/2,a/2+b/2
    d+=1
else:d=-1
print(d)

# Optimized solution
a,b,c=list(map(int, input().split()))
ans=0
while a%2==0 and b%2==0 and c%2==0 and ans<10000:
    a,b,c=b//2+c//2,a//2+c//2,a//2+b//2
    ans+=1
if ans==10000:ans=-1
print(ans)
# Original solution
*a,=list(map(int,input().split()))
b=[bin(abs(a[i+1]-a[i]))[::-1].find('1') for i in range(2)]
print(((max(b) if b[0]*b[1]<0 else min(b))*(1-sum(a)%2)))

# Optimized solution
*a,=list(map(int,input().split()))
b=[bin(a[i+1]-a[i])[::-1].find('1')for i in (0,1)]
print(((max(b) if b[0]*b[1]<0 else min(b))*(1-sum(a)%2)))