Buggy python submission (verdict: Runtime Error):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

MOD = 10 ** 9 + 7

# 組合せ nCr (MOD) 逆元を使う方法
def perm(n,k):
    if k > n or k < 0: return 0
    return fact[n] * fact_inv[n-k] % MOD

def cmb(n, k):
    if k < 0 or k > n: return 0 
    return fact[n] * fact_inv[k] % MOD * fact_inv[n-k] % MOD

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10 ** 5 + 100 # 階乗テーブルの上限
fact, fact_inv = make_fact(U, MOD)
N, K = lr()
A = np.array(lr())
A.sort()
coef = np.zeros(N, np.int64)
coef[K-1:] = [cmb(n, K-1) for n in range(K-1, N)]
answer = (A * coef % MOD).sum() % MOD
coef = np.zeros(N, np.int64)
coef[:-K+1] = [cmb(n, K-1) for n in range(N-1, K-2, -1)]
answer -= (A * coef % MOD).sum() % MOD
print(answer%MOD)
# 53

Fixed submission:
import sys
from math import factorial
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10**6
MOD = 10 ** 9 + 7
fact, fact_inv = make_fact(U, MOD)
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0

for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fact[i] % MOD * fact_inv[K-1] % MOD * fact_inv[i-(K-1)] % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * fact[N-(i+1)] % MOD * fact_inv[K-1] % MOD * fact_inv[N-(i+1)-(K-1)] % MOD
    count %= MOD

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = lr()
A.sort()
diffs = [0] * N
MOD = 10 ** 9 + 7
if K == 1:
    print(0)
    exit()
for x in range(K-1, N):
    # x は最大値と最小値のindexの差
    for i in range(N-x):
        diffs[x] += A[i+x] - A[i]

def cmb(n, k):
    # nCkを計算する
    k = min(k, n-k)
    return fac[n] * ifac[k] * ifac[n-k] % MOD

def make_tables(n):
    # 階乗テーブル、逆元の階乗テーブルを作成する
    fac = [1, 1] # 階乗テーブル
    ifac = [1, 1] # 逆元の階乗テーブル
    inverse = [0, 1] # 逆元テーブル 0の階乗は1 0の逆元は0

    for i in range(2, n+1):
        fac.append((fac[-1] * i) % MOD)
        inverse.append((-inverse[MOD % i] * (MOD//i)) % MOD)
        ifac.append((ifac[-1] * inverse[-1]) % MOD)
    return fac, ifac

fac, ifac = make_tables(N+1)
answer = 0
for i in range(K-1, N):
    # i-1 C K-2 を足す
    answer += diffs[i] * cmb(i-1, K-2)

print(answer % MOD)
# 51

Fixed submission:
import sys
from math import factorial
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10**6
MOD = 10 ** 9 + 7
fact, fact_inv = make_fact(U, MOD)
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0

for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fact[i] % MOD * fact_inv[K-1] % MOD * fact_inv[i-(K-1)] % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * fact[N-(i+1)] % MOD * fact_inv[K-1] % MOD * fact_inv[N-(i+1)-(K-1)] % MOD
    count %= MOD

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from math import factorial
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

MOD = 10 ** 9 + 7
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0
fac = factorial
l = [fac(x) * pow(fac(K-1) * fac(max(0, x - (K-1))) % MOD, MOD-2, MOD) % MOD for x in range(N)]
for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * l[i]
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * l[(N-(i+1))]
    count %= MOD

print(count)


Fixed submission:
import sys
from math import factorial
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10**6
MOD = 10 ** 9 + 7
fact, fact_inv = make_fact(U, MOD)
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0

for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fact[i] % MOD * fact_inv[K-1] % MOD * fact_inv[i-(K-1)] % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * fact[N-(i+1)] % MOD * fact_inv[K-1] % MOD * fact_inv[N-(i+1)-(K-1)] % MOD
    count %= MOD

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from math import factorial
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

MOD = 10 ** 9 + 7
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0
fac = factorial
for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fac(i) * pow(fac(K-1) * fac(i-(K-1)) % MOD, MOD-2, MOD) % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    n = (N-(i+1)); k = K-1
    count -= A[i] * fac(n) * pow(fac(k) * fac(n-k) % MOD, MOD-2, MOD) % MOD
    count %= MOD

print(count)


Fixed submission:
import sys
from math import factorial
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10**6
MOD = 10 ** 9 + 7
fact, fact_inv = make_fact(U, MOD)
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0

for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fact[i] % MOD * fact_inv[K-1] % MOD * fact_inv[i-(K-1)] % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * fact[N-(i+1)] % MOD * fact_inv[K-1] % MOD * fact_inv[N-(i+1)-(K-1)] % MOD
    count %= MOD

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
from math import factorial
#import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

MOD = 10 ** 9 + 7
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0
fac = factorial
for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fac(i) * pow(fac(K-1) * fac(i-(K-1)) % MOD, MOD-2, MOD) % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    n = (N-(i+1)); k = K-1
    count -= A[i] * fac(n) * pow(fac(k) * fac(n-k) % MOD, MOD-2, MOD) % MOD
    count %= MOD

print(count)


Fixed submission:
import sys
from math import factorial
import numpy as np

stdin = sys.stdin

ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split())) # applies to numbers only
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

def cumprod(arr, MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        arr[:, n] *= arr[:, n-1]; arr[:, n] %= MOD
    for n in range(1, Lsq):
        arr[n] *= arr[n-1, -1]; arr[n] %= MOD
    return arr.ravel()[:L]

def make_fact(U, MOD):
    x = np.arange(U, dtype=np.int64); x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64); x[0] = pow(int(fact[-1]), MOD-2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    return fact, fact_inv

U = 10**6
MOD = 10 ** 9 + 7
fact, fact_inv = make_fact(U, MOD)
N, K = rl()
A = rl()
A.sort()
min = A[0]
A = [(x - min)%MOD for x in A]
#最大値として何回登場するか
count = 0

for i in range(K-1, N):
    # A[i] * iC(k-1)
    count += A[i] * fact[i] % MOD * fact_inv[K-1] % MOD * fact_inv[i-(K-1)] % MOD
    count %= MOD
for i in range(N-K+1):
    # A[i] * (N-(i+1)) C (k-1)
    count -= A[i] * fact[N-(i+1)] % MOD * fact_inv[K-1] % MOD * fact_inv[N-(i+1)-(K-1)] % MOD
    count %= MOD

print(count)