Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
# E

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

mod = 10**9 + 7

A.sort()
# print(A)


def cmb(n, r):
    from operator import mul
    from functools import reduce

    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


ans = 0
num1=None
num2=None

for i in range(N):
    if N - i - 1 >= K-1:
        if num1 is None:
            num1 = cmb(N-i-1, K-1) % mod
        else:
            num1 = int(num1 * ((N-i-K+1) % mod) * pow(N-i, mod-2, mod))
        # print(N-i-1, K-1, num1)

        if A[i] * num1 < 0:
            ans += (-A[i] * num1) % mod
        else:
            ans -= (A[i] * num1) % mod
        # print(f'-: {i}, {num}, {A[i]}')
    if i >= K-1:
        if num2 is None:
            num2 = cmb(i, K-1) % mod
        else:
            num2 = num2 * (i % mod) * pow(i-K+1, mod-2, mod)
        # print(i, K-1, num2)

        if A[i] * num2 < 0:
            ans -= (-A[i] * num2) % mod
        else:
            ans += (A[i] * num2) % mod
        # print(f'+: {i}, {num}, {A[i]}')

print(ans % mod)


Fixed submission:
# -*- coding: utf-8 -*-
# E

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


class Cmb(object):
    def cmb_precalc(self, n, r, mod: int = 10 ** 9 + 7):
        if (r < 0) or (r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % mod

    def cmb_prep(self, N: int, mod: int = 10 ** 9 + 7):
        self.g1 = [1, 1]  # 元テーブル
        self.g2 = [1, 1]  # 逆元テーブル
        inverse = [0, 1]  # 逆元テーブル計算用テーブル

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


ans = 0
cmb = Cmb()
cmb.cmb_prep(N + 5)

for i in range(N):
    if i >= K-1:
        ans += A[i] * cmb.cmb_precalc(i, i-K+1)
    if N - i - 1 >= K-1:
        ans -= A[i] * cmb.cmb_precalc(N-i-1, K-1)

print(ans % mod)

Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
# E

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

mod = 10**9 + 7

A.sort()
# print(A)

def cmb(n, r):
    from operator import mul
    from functools import reduce

    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


ans = 0
num1=None
num2=None

for i in range(N):
    if N - i - 1 >= K-1:
        if num1 is None:
            num1 = cmb(N-i-1, K-1)
        else:
            num1 = int(num1 * (N-i-K+1) / (N-i))
        # print(N-i-1, K-1, num1)
        if A[i] * num1 < 0:
            ans += (-A[i] * num1) % mod
        else:
            ans -= (A[i] * num1) % mod
        # print(f'-: {i}, {num}, {A[i]}')
    if i >= K-1:
        if num2 is None:
            num2 = cmb(i, K-1)
        else:
            num2 = int(num2 * i / (i-K+1))
        # print(i, K-1, num2)
        if A[i] * num2 < 0:
            ans -= (-A[i] * num2) % mod
        else:
            ans += (A[i] * num2) % mod
        # print(f'+: {i}, {num}, {A[i]}')

print(ans % mod)


Fixed submission:
# -*- coding: utf-8 -*-
# E

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


class Cmb(object):
    def cmb_precalc(self, n, r, mod: int = 10 ** 9 + 7):
        if (r < 0) or (r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % mod

    def cmb_prep(self, N: int, mod: int = 10 ** 9 + 7):
        self.g1 = [1, 1]  # 元テーブル
        self.g2 = [1, 1]  # 逆元テーブル
        inverse = [0, 1]  # 逆元テーブル計算用テーブル

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


ans = 0
cmb = Cmb()
cmb.cmb_prep(N + 5)

for i in range(N):
    if i >= K-1:
        ans += A[i] * cmb.cmb_precalc(i, i-K+1)
    if N - i - 1 >= K-1:
        ans -= A[i] * cmb.cmb_precalc(N-i-1, K-1)

print(ans % mod)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
# E

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

mod = 10**9 + 7

A.sort()
# print(A)

def cmb(n, r):
    from operator import mul
    from functools import reduce

    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


ans = 0
for i in range(N):
    if N - i - 1 >= K-1:
        num = cmb(N-i-1, K-1)
        if A[i] * num < 0:
            ans += (-A[i] * num) % mod
        else:
            ans -= (A[i] * num) % mod
        # print(f'-: {i}, {num}, {A[i]}')
    if i >= K-1:
        num = cmb(i, K-1)
        if A[i] * num < 0:
            ans -= (-A[i] * num) % mod
        else:
            ans += (A[i] * num) % mod
        # print(f'+: {i}, {num}, {A[i]}')

print(ans % mod)


Fixed submission:
# -*- coding: utf-8 -*-
# E

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


class Cmb(object):
    def cmb_precalc(self, n, r, mod: int = 10 ** 9 + 7):
        if (r < 0) or (r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % mod

    def cmb_prep(self, N: int, mod: int = 10 ** 9 + 7):
        self.g1 = [1, 1]  # 元テーブル
        self.g2 = [1, 1]  # 逆元テーブル
        inverse = [0, 1]  # 逆元テーブル計算用テーブル

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


ans = 0
cmb = Cmb()
cmb.cmb_prep(N + 5)

for i in range(N):
    if i >= K-1:
        ans += A[i] * cmb.cmb_precalc(i, i-K+1)
    if N - i - 1 >= K-1:
        ans -= A[i] * cmb.cmb_precalc(N-i-1, K-1)

print(ans % mod)