Buggy python submission (verdict: Time Limit Exceeded):
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)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = list(map(int, input().split(' ')))
aaa = sorted(list(map(int, input().split(' '))))
def choice(a, b):
    if b == 0 or a == b:
        return 1
    if a <= b:
        return 0
    if b > a - b:
        return choice(a, a - b)
    hoge = 1
    for a in range(a, a-b, -1):
        hoge = hoge * a
    fuga = 1
    for b in range(1, b+1):
        fuga = fuga * b
    return hoge // fuga

ans = 0
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) % 1000000007
        # print(st, ed, ':::', ed-1 - st, k-2, "stCed=", choice_pattern, 'hoge=', (aaa[ed]-aaa[st]), ans)
print(ans)

Fixed submission:
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)