CodeEff/ECCO huggingface dataset:
# Original solution
(r,c) = [int(i) for i in input().split()]
 
table = []
for rc in range(r):
    table.append([int(i) for i in input().split()])
 
table.append([0 for _ in range(c + 1)])
 
for rc in range(r):
    row_total = 0
    for cc in range(c):
        table[r][cc] += table[rc][cc]
        row_total += table[rc][cc]
 
    table[rc].append(row_total)
    table[r][c] += row_total
 
for row in table:
    print((' '.join([str(column) for column in row])))

# Optimized solution
(r, c) = [int(i) for i in input().split()]
last_row = [0 for _ in range(c+1)]
 
for _ in range(r):
    row = [int(i) for i in input().split()]
    for cc in range(c):
        last_row[cc] += int(row[cc])
        print(row[cc], end=' ')
 
    print(sum(row))
 
last_row[-1] = sum(last_row)
print(' '.join([str(a) for a in last_row]))
# Original solution
h, w = [int(i) for i in input().split()]
date = [[int(q) for q in input().split()] for e in range(h)]
date2=[]
date3=[]
for r in range(0, h):
    for t in range(0, w):
        print(date[r][t], end=' ')
    print(sum(date[r]))

for k in range(0,w):
   for j in range(0,h):
      date2.append(date[j][k])

for n in range(0,w*h,h):
   print(sum(date2[n:(n+h)]),end=' ')

for m in range(h):
   date3.append(sum(date[m]))
print(sum(date3))


# Optimized solution
r, c =[int(i) for i in input().split()]

date =[]
sum_row = [0] * (c + 1)

for ri in range(r):
    date.append([int(i) for i in input().split()])
    date[ri].append(sum(date[ri]))
    print((" ".join([str(d) for d in date[ri]])))
    for ci in range(c + 1):
        sum_row[ci] += date[ri][ci]

print((" ".join([str(s) for s in sum_row])))
# Original solution
r, c = map(int, input().split())
a = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    l = list(map(int, input().split()))
    for j in range(c):
        a[i][j] = l[j]
        a[i][c] += a[i][j]

for i in range(c+1):
    for j in range(r):
        a[r][i] += a[j][i]

for i in range(r+1):
    for j in range(c+1):
        if j == c:
            print(str(a[i][j]))
        else:
            print(str(a[i][j]) + " ", end="")


# Optimized solution
r, c = map(int, input().split())
a = [[int(i) for i in input().split()] for j in range(r)]
b = [[0 for i in range(c+1)] for j in range(r+1)]
for i in range(r):
    for j in range(c):
        b[i][j] = a[i][j]
        b[i][c] += a[i][j]
    
for i in range(c+1):
    for j in range(r):
        b[r][i] += b[j][i]

for i in range(r+1):
    for j in range(c+1):
        if j == c:
            print(b[i][j])
        else:
            print("{0} ".format(b[i][j]), end = "")
# Original solution
r,c=map(int,input().split())
a,b=[],[0]*c
for i in range(r):
    x=list(map(int,input().split()))
    print(*x, end=' ')
    print(sum(x))
    for j in range(c):
        b[j]+=x[j]
print(*b, end=' ')
print(sum(b))


# Optimized solution
r,c=map(int,input().split())
a=[0]*c
for i in range(r):
    x=list(map(int,input().split()))
    for j in range(c):
        a[j]+=x[j]
    print(*x, end=' ')
    print(sum(x))
print(*a, end=' ')
print(sum(a))
# Original solution
m,n=map(int,input().split())
a =[]
for _ in range(m):
    a.append(list(map(int,input().split())))
for index in range(m):
    a[index].append(sum(a[index]))
for a_column in a:
    for num in a_column[:-1]:
        print(num, end=' ')
    print(a_column[-1])
for row_index in range(n):
    sum = 0
    for a_column in a:
        sum += a_column[row_index]
    print(sum, end=' ')
sum = 0
for a_column in a:
    sum += a_column[-1]
print(sum)



# Optimized solution
m,n=list(map(int,input().split()))
a =[]
for _ in range(m):
    a.append(list(map(int,input().split())))
for index in range(m):
    a[index].append(sum(a[index]))
for a_column in a:
    print((' '.join(map(str, a_column))))
a.append([0]*(n+1))
for row_index in range(n+1):
    for a_column in a[:-1]:
        a[-1][row_index] += a_column[row_index]
print((' '.join(map(str, a[-1]))))
# Original solution
N,M = list(map(int,input().split()))

table = [1,1]
while len(table) <= N:
    temp = table[0]
    for i in range(len(table)-1):
        table[i+1] += temp
        temp = table[i+1]- temp
        table[i+1] %= M
    table.append(1)


S = [1]

rev2 = pow(2, M-2, M)
base = pow(2, N, M)
ans = 0
S = [1]
for K in range(N+1):
    res = table[K] % M
    res = (res * pow(2, pow(2, N - K, M-1), M)) % M
    b = 1
    v = 0
    T = [0]*(K+2)
    for L in range(K):
        T[L+1] = s = (S[L] + (L+1)*S[L+1]) % M
        v += s * b
        b = (b * base) % M
    v += b
    T[K+1] = 1
    S = T
    res = (res * v) % M
    if K % 2:
        ans -= res
    else:
        ans += res
    ans %= M

    base = (base * rev2) % M
print(ans)


# Optimized solution
N, M = list(map(int, input().split()))
fact = [1]*(N+1)
rfact = [1]*(N+1)
for i in range(1, N+1):
    fact[i] = r = (i * fact[i-1]) % M
    rfact[i] = pow(r, M-2, M)

S = [1]

rev2 = pow(2, M-2, M)
base = pow(2, N, M)
ans = 0
S = [1]
for K in range(N+1):
    res = (fact[N] * rfact[K] * rfact[N-K]) % M
    res = (res * pow(2, pow(2, N - K, M-1), M)) % M
    b = 1
    v = 0
    T = [0]*(K+2)
    for L in range(K):
        T[L+1] = s = (S[L] + (L+1)*S[L+1]) % M
        v += s * b
        b = (b * base) % M
    v += b
    T[K+1] = 1
    S = T
    res = (res * v) % M
    if K % 2:
        ans -= res
    else:
        ans += res
    ans %= M

    base = (base * rev2) % M
print(ans)
# Original solution
while True:
  n = int(eval(input()))
  if n == 0:
    break
  wlst = list(map(int, input().split()))
  dp = [[0] * n for _ in range(n + 1)]
  
  def check(left1, right1, left2, right2, cnt):
    if left1 > right1 and left2 > right2: return cnt
    if left1 > right1:return dp[right2 - left2 + 1][left2] + cnt
    if left2 > right2:return dp[right1 - left1 + 1][left1] + cnt
    ret = cnt + dp[right1 - left1 + 1][left1] + dp[right2 - left2 + 1][left2]
    if abs(wlst[right1] - wlst[left2]) <= 1:
      ret = max(ret, check(left1, right1 - 1, left2 + 1, right2, cnt + 1))
    if right1 - left1 + 1 >= 2 and dp[2][right1 - 1]:
      ret = max(ret, check(left1, right1 - 2, left2, right2, cnt + 1))
    if right2 - left2 + 1 >= 2 and dp[2][left2]:
      ret = max(ret, check(left1, right1, left2 + 2, right2, cnt + 1))
    return ret
  
  for length in range(2, n + 1):
    for left in range(n - length + 1):
      for k in range(length - 1):
        dp[length][left] = max(dp[length][left], check(left, left + k, left + k + 1, left + length - 1, 0))
  
  print((dp[n][0] * 2))



# Optimized solution
def main():
  while True:
    n = int(eval(input()))
    if n == 0:
      break
    wlst = list(map(int, input().split()))
    connect = [[None] * n for _ in range(n)]
    for i in range(n):
      for j in range(n):
        connect[i][j] = abs(wlst[i] - wlst[j]) <= 1
    
    max_lst = [[False] * (n + 1) for _ in range(n + 1)]
    for i in range(n + 1):
      max_lst[0][i] = True
    
    dp = [[0] * n for _ in range(n + 1)]
    
    def check(length, left):
      ret = dp[length - 1][left]
      for k in range(length - 1, 0, -2):
        if not max_lst[length - k - 1][left + k]:continue
        if connect[left + k - 1][left + length - 1]:
          temp = length - k + 1 + dp[k - 1][left]
          if ret < temp:
            ret = temp
      return ret
    
    for length in range(2, n + 1):
      for left in range(n - length + 1):
        dp[length][left] = check(length, left)
        if dp[length][left] == length:
          max_lst[length][left] = True
    
    print((dp[n][0]))

main()
# Original solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(n):
        a = LI()

        m = {}
        def _f(a):
            l = len(a)
            if l < 2:
                return 0
            k = tuple(a)
            if k in m:
                return m[k]
            r = 0
            for i in range(len(a)-1):
                if abs(a[i]-a[i+1]) >= 2:
                    continue
                t = _f(a[:i] + a[i+2:]) + 2
                if r < t:
                    r = t
            m[k] = r
            return r

        return _f(a)

    while True:
        n = I()
        if n == 0:
            break
        rr.append(f(n))

    return '\n'.join(map(str, rr))



print(main())



# Optimized solution
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    def f(n):
        a = LI()
        l = len(a)
        s = set()
        r = 0
        for i in range(l-1):
            if abs(a[i] - a[i+1]) < 2:
                s.add((i,i+1))
        for i in range(4, l+1):
            for j in range(l-i+1):
                k = j + i - 1
                if (j+1, k-1) in s and abs(a[j] - a[k]) < 2:
                    s.add((j,k))
                    continue
                for m in range(j+2,k,2):
                    if (j,m-1) in s and (m,k) in s:
                        s.add((j,k))
                        break

        m = {}
        m[l] = 0
        def _f(i):
            if i in m:
                return m[i]
            r = 0
            for j in range(i,l+1):
                t = _f(j+1)
                if (i,j) in s:
                    t += j - i + 1
                if r < t:
                    r = t
            m[i] = r
            return r
        return _f(0)

    while True:
        n = I()
        if n == 0:
            break
        rr.append(f(n))

    return '\n'.join(map(str, rr))



print(main())
# Original solution
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,k=list(map(int,input().split()))
a=list(map(int,input().split()))
l=[]
l2=[]
for i in range(n):
  l.append([a[i],i])
l.sort()
for i in range(n):
  l2.append([l[i][1],i])
l2.sort()
mod=10**9+7

g1=[1,1]
g2=[1,1]
inverse=[0,1]
 
for i in range(2,n+1):
  g1.append((g1[-1]*i)%mod)
  inverse.append((-inverse[mod%i]*(mod//i))%mod)
  g2.append((g2[-1]*inverse[-1])%mod)

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

# Optimized solution
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,k=list(map(int,input().split()))
a=list(map(int,input().split()))
a.sort()
mod=10**9+7
 
g1=[1,1]
g2=[1,1]
inverse=[0,1]
 
for i in range(2,n+1):
  g1.append((g1[-1]*i)%mod)
  inverse.append((-inverse[mod%i]*(mod//i))%mod)
  g2.append((g2[-1]*inverse[-1])%mod)
 
ans=0
for i in range(n):
  ans+=a[i]*cmb(i,k-1,mod)
  ans%=mod
for i in range(n):
  ans-=a[i]*cmb(n-i-1,k-1,mod)
  ans+=mod
  ans%=mod
print(ans)
# Original solution
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int

from itertools import accumulate

def mod_factorial(n):
    value = 1
    for i in range(n,0,-1):
        value*=i
        value%=MOD
    return value

def solve(N: int, K: int, A: "List[int]"):
    A.sort()
    n = N-1
    r = K-1
    # nCkTable[i] = iCr
    nCkTable = [0]*(n+1)

    start = 1
    for _ in range(r):
        start *= n
        start %= MOD
        n -= 1

    bunbo = mod_factorial(r)
    bunbo_gyakugen = pow(bunbo,MOD-2,MOD)

    n = N-1
    for i in range(n, r-1, -1):
        nCkTable[i] = start*bunbo_gyakugen%MOD
        start*=pow(i,MOD-2,MOD)
        start*=i-r

    answer = 0
    for i in range(N):
        answer += A[i]*(nCkTable[i]-nCkTable[N-1-i])
        answer%=MOD
    print((answer%MOD))
    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

MOD = 1000000007  # type: int

from itertools import accumulate

def mod_factorial(n):
    value = 1
    for i in range(n,0,-1):
        value*=i
        value%=MOD
    return value

def solve(N: int, K: int, A: "List[int]"):
    A.sort()
    n = N-1
    r = K-1
    # nCkTable[i] = iCr
    nCkTable = [0]*(n+1)

    start = 1
    for _ in range(r):
        start *= n
        start %= MOD
        n -= 1

    bunbo = mod_factorial(r)
    bunbo_gyakugen = pow(bunbo,MOD-2,MOD)

    n = N-1
    for i in range(n, r-1, -1):
        nCkTable[i] = start*bunbo_gyakugen%MOD
        start*=pow(i,MOD-2,MOD)
        start*=i-r
        start%=MOD

    answer = 0
    for i in range(N):
        answer += A[i]*(nCkTable[i]-nCkTable[N-1-i])
        answer%=MOD
    print((answer%MOD))
    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()))
A = sorted(A)
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
  
p = 1000000007
ans = 0  
for i in range(N- K +1):
  ans += ((A[N-1-i] - A[i])*comb(N-1-i, K-1, p))
  ans = ans % p
print(ans)

# Optimized solution
N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
A = sorted(A)
p = 1000000007
frac = [1]
for i in range(N):
  frac.append(frac[i]*(i+1)%p)

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=frac[n]
    b=frac[k]
    c=frac[n-k]
    return (a * power(b, mod-2, mod) * power(c, mod-2, mod)) % mod
  

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

def nCr(n,r):
  if n < 0 or r < 0 or n < r: return 0
  if n - r < r: r = n - r
  if r == 0: return 1
  if r == 1: return n
  
  # 分子のn*(n-1)*...がr個分続くやつ
  numerator = [n-r+k+1 for k in range(r)]
  # 分母：r!=r*(r-1)*...*3*2の要素
  denominator = [k+1 for k in range(r)]
  
  # 分母の要素で割れる部分を割っていく部分
  for p in range(2, r+1):
    # 分母は1,2,3,...rのようになっており、1は意味がないのでスキップした形か
    pivot = denominator[p-1]
    if pivot > 1:
      # 分子のX番目と分母のX-offset番目が共通の約数を持つということだと思う。piv分ずれているのだから、pivの倍数というところか
      offset = (n-r) % p
      for k in range(p-1, r, p):
        # 約分できる要素について割る
        numerator[k - offset]  /= pivot
        denominator[k] /= pivot
        
  ret = 1
  for k in range(r):
    if numerator[k] > 1: ret *= int(numerator[k])
      
  return ret

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

if N == 1 or K == 1:
    print((0))
    exit()

# A_iがminに選ばれる回数と、maxに選ばれる回数それぞれをわけて考えて足していく
ans = 0
for i in range(N):
    # minになれるのは、iの右からK-1個選べる場合
    if N - (i+1) >= K-1:
        ans -= A[i] * nCr(N - (i+1), K-1) % MOD
    # maxになれるのは、iの左からK-1個選べる場合
    if i >= K-1:
        ans += A[i] * nCr(i, K-1) % MOD

    ans %= MOD

print(ans)

# Optimized solution

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




if N == 1 or K == 1:
    print((0))
    exit()


def nCr(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    # nCr = n!  / (r! * (n-r)!)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = MOD
#N = 10 ** 6  # N は必要分だけ用意する
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):
    # (a * b * c) % MOD = (a * b % MOD) * c % MOD みたいなやつを利用して、大きな値にならないように一ステップごとにMODをとる
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

# A_iがminに選ばれる回数と、maxに選ばれる回数それぞれをわけて考えて足していく
ans = 0
for i in range(N):
    # minになれるのは、iの右からK-1個選べる場合
    if N - (i+1) >= K-1:
        ans -= A[i] * nCr(N - (i+1), K-1, MOD)
    # maxになれるのは、iの左からK-1個選べる場合
    if i >= K-1:
        ans += A[i] * nCr(i, K-1, MOD)

    ans %= MOD

print(ans)
# Original solution
n,k=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
mod=10**9+7
ans=0
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
    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):
    ans-=a[i]*cmb(n-i-1,k-1)%mod
    ans%=mod
a=a[::-1]
for i in range(n-k+1):
    ans+=a[i]*cmb(n-i-1,k-1)%mod
    ans%=mod

print((ans%mod))

# Optimized solution
n,k=list(map(int,input().split()))
a=sorted(list(map(int,input().split())))
mod=10**9+7
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 nCr(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD
for i in range(n-k+1):
    ans-=a[i]*nCr(n-i-1,k-1)%mod
    ans%=mod
a=a[::-1]
for i in range(n-k+1):
    ans+=a[i]*nCr(n-i-1,k-1)%mod
    ans%=mod

print((ans%mod))
# Original solution
# modulo:mod

# inverse x^(-1)
def inv(x):
    global mod
    return pow(x,mod-2,mod)

# factorial x!
def fact(x):
    global mod
    res=1
    for i in range(2,x+1):
        res=res*i%mod
    return res

# combination nCr
def combi(n,r):
    if r<0 or r>n:
        return 0
    else:
        return fact(n)*inv(fact(r))*inv(fact(n-r))%mod

mod=10**9+7

N,K=list(map(int,input().split()))
A=[-10**10] # 1-ind
A.extend(list(map(int,input().split())))
A.sort()

# print(A)

ans=0
# min
for i in range(1,N-K+2):
    temp=combi(N-i,K-1)%mod
    '''
    print(i,A[i])
    print(N-i,K-1)
    print('min',temp)
    '''
    ans=(ans-temp*A[i])%mod
# max
for j in range(K,N+1):
    temp=combi(j-1,K-1)
    '''
    print(j)
    print(j-1,K-1)
    print('max',temp*A[j])
    '''
    ans=(ans+temp*A[j])%mod

print(ans)



# Optimized solution
# modulo:mod

# inverse x^(-1)
def inv(x):
    global mod
    return pow(x,mod-2,mod)

# factorial x!
def fact(x):
    global mod
    res=1
    for i in range(2,x+1):
        res=res*i%mod
    return res

# combination nCr
def combi(n,r):
    if r<0 or r>n:
        return 0
    else:
        return fact(n)*inv(fact(r))*inv(fact(n-r))%mod

mod=10**9+7

N,K=list(map(int,input().split()))
A=[-10**10] # 1-ind
A.extend(list(map(int,input().split())))
A.sort()

# print(A)

ans=0
# min
p=1
for i in range(N-K+1,0,-1):
    # print(i,A[i],p)
    temp=A[i]*p%mod    
    ans=(ans-temp)%mod
    # print((N-i+1)*inv(N-K-i+2))
    p=p*(N-i+1)*inv(N-K-i+2)%mod
# max
p=1
for j in range(K,N+1):
    # print(j,A[j],p)
    temp=A[j]*p%mod
    ans=(ans+temp)%mod
    p=p*j*inv(j-K+1)%mod

print(ans)
# Original solution
import sys
input = sys.stdin.readline

def inv(x):
    return pow(x, MOD-2, MOD)
    
def C(n, r):
    if r>n:
        return 0
    #return fact[n]//fact[r]//fact[n-r]
    #return fact[n]*inv(fact[r])*inv(fact[n-r])
    return fact[n]*inv_table[r]*inv_table[n-r]

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

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

fact = [1]

for i in range(1, N+1):
    fact.append(fact[-1]*i%MOD)
    #fact.append(fact[-1]*i)

inv_table = [-1]*(N+1)

for i in range(N+1):
    inv_table[i] = inv(fact[i])
    
ans = 0
c_acc = [0]

for i in range(N+1):
    c_acc.append((c_acc[-1]+C(i, K-2))%MOD)

for i in range(N):
    left = c_acc[i]-c_acc[0]
    right = c_acc[N-i-1]-c_acc[0]
    #print(left, right)
    ans += A[i]*(left-right)
    ans %= MOD

print(ans)

# Optimized solution
def inv(x):
    return pow(x, MOD-2, MOD)

def C(n, r):
    if r>n:
        return 0
    return fact[n]*inv_table[r]*inv_table[n-r]

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

fact = [1]

for i in range(1, N+1):
    fact.append(fact[-1]*i%MOD)
    
inv_table = [-1]*(N+1)

for i in range(N+1):
    inv_table[i] = inv(fact[i])

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

print(ans)
# Original solution
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return list(map(int, input().split()))
sys.setrecursionlimit(10**9)

N, K = mapint()
from collections import Counter
As = list(mapint())
count = Counter(As)
mod = 10**9+7
maxi = 0
cum = 0
pos = {}
neg = {}
pos[0] = 1
neg[0] = 1
As = sorted(list(set(As)))
for i in range(1, 10**5+5):
    pos[i] = pos[i-1]*i%mod
    neg[i] = pow(pos[i], mod-2, mod)
for a in As:
    cum += count[a]
    if cum<K:
        continue
    for i in range(1, min(K, count[a])+1):
        if cum-count[a]-K+i<0: continue
        maxi += a*pos[count[a]]*neg[i]*neg[count[a]-i]*pos[cum-count[a]]*neg[K-i]*neg[cum-count[a]-K+i]
        maxi %= mod
mini = 0
cum = 0
for a in As[::-1]:
    cum += count[a]
    if cum<K:
        continue
    for i in range(1, min(K, count[a])+1):
        if cum-count[a]-K+i<0: continue
        mini += a*pos[count[a]]*neg[i]*neg[count[a]-i]*pos[cum-count[a]]*neg[K-i]*neg[cum-count[a]-K+i]
        mini %= mod
print(((maxi-mini)%mod))

# Optimized solution
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return list(map(int, input().split()))
sys.setrecursionlimit(10**9)

N, K = mapint()
As = list(mapint())
mod = 10**9+7
As.sort()

pos = {}
neg = {}
pos[0] = 1
neg[0] = 1
for i in range(1, 10**5+3):
    pos[i] = (pos[i-1]*i)%mod
    neg[i] = pow(pos[i], mod-2, mod)

mini = 0
maxi = 0
for i, a in enumerate(As):
    if i<=N-K:
        mini += a*pos[(N-i-1)]*neg[K-1]*neg[N-i-K]
        mini %= mod
    if i>=K-1:
        maxi += a*pos[i]*neg[K-1]*neg[i-K+1]
        maxi %= mod
print(((maxi-mini)%mod))