Buggy python submission (verdict: Time Limit Exceeded):
from functools import lru_cache


@lru_cache(None)
def factrial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i
    return ans


@lru_cache(None)
def combinations(n, r,mod):
    return (factrial(n) // (factrial(n - r) * factrial(r))) % mod


def main():
    n, k = map(int, input().split())
    mod = 10 ** 9 + 7
    A = tuple(map(lambda x: x % mod, sorted(map(int, input().split()))))
    ans = 0
    for i, a in enumerate(A[k - 1:], k - 1):
        ans += (a * combinations(i, k - 1,mod)) % mod
        ans %= mod
    for i, a in enumerate(A[: n - k + 1], 1):
        ans -= (a * combinations(n - i, k - 1,mod)) % mod
        ans %= mod
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from functools import lru_cache
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
mod = 10 ** 9 + 7
# 階乗とその逆元の計算
factrial = [0 for _ in range(n + 1)]
factrial[0] = 1
for i in range(1, n + 1):
    factrial[i] = (factrial[i - 1] * i) % mod
inverse_factrial = list()
for f in factrial:
    inverse_factrial.append(pow(f, -1, mod))


@lru_cache(maxsize=None)
def nCr(n, r):
    return (factrial[n] * inverse_factrial[r] * inverse_factrial[n - r]) % mod


ans = 0
# 最大値の和
for i, a in enumerate(A[k - 1:], k - 1):
    ans += ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
# 最小値の和
A.reverse()
for i, a in enumerate(A[k - 1:], k - 1):
    ans -= ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from functools import lru_cache


@lru_cache(None)
def factrial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i
    return ans


@lru_cache(None)
def combinations(n, r):
    return factrial(n) // (factrial(n - r) * factrial(r))


def main():
    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    ans = 0
    mod = 10 ** 9 + 7
    for i, a in enumerate(A[k - 1:], k - 1):
        ans += ((a % mod) * (combinations(i, k - 1) % mod)) % mod
        ans %= mod
    for i, a in enumerate(A[: n - k + 1], 1):
        ans -= ((a % mod) * (combinations(n - i, k - 1)) % mod) % mod
        ans %= mod
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from functools import lru_cache
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
mod = 10 ** 9 + 7
# 階乗とその逆元の計算
factrial = [0 for _ in range(n + 1)]
factrial[0] = 1
for i in range(1, n + 1):
    factrial[i] = (factrial[i - 1] * i) % mod
inverse_factrial = list()
for f in factrial:
    inverse_factrial.append(pow(f, -1, mod))


@lru_cache(maxsize=None)
def nCr(n, r):
    return (factrial[n] * inverse_factrial[r] * inverse_factrial[n - r]) % mod


ans = 0
# 最大値の和
for i, a in enumerate(A[k - 1:], k - 1):
    ans += ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
# 最小値の和
A.reverse()
for i, a in enumerate(A[k - 1:], k - 1):
    ans -= ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
print(ans)

Buggy python submission (verdict: Runtime Error):
from functools import lru_cache


@lru_cache
def factrial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i
    return ans


@lru_cache
def combinations(n, r):
    return factrial(n) // (factrial(n - r) * factrial(r))


def main():
    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    ans = 0
    mod = 10 ** 9 + 7
    for i, a in enumerate(A[k - 1:], k - 1):
        ans += ((a % mod) * (combinations(i, k - 1) % mod)) % mod
        ans %= mod
    for i, a in enumerate(A[: n - k + 1], 1):
        ans -= ((a % mod) * (combinations(n - i, k - 1)) % mod) % mod
        ans %= mod
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from functools import lru_cache
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
mod = 10 ** 9 + 7
# 階乗とその逆元の計算
factrial = [0 for _ in range(n + 1)]
factrial[0] = 1
for i in range(1, n + 1):
    factrial[i] = (factrial[i - 1] * i) % mod
inverse_factrial = list()
for f in factrial:
    inverse_factrial.append(pow(f, -1, mod))


@lru_cache(maxsize=None)
def nCr(n, r):
    return (factrial[n] * inverse_factrial[r] * inverse_factrial[n - r]) % mod


ans = 0
# 最大値の和
for i, a in enumerate(A[k - 1:], k - 1):
    ans += ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
# 最小値の和
A.reverse()
for i, a in enumerate(A[k - 1:], k - 1):
    ans -= ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from functools import lru_cache


@lru_cache(maxsize=10 ** 5)
def factrial(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i
    return ans


@lru_cache(maxsize=10 ** 5)
def combinations(n, r):
    return factrial(n) // (factrial(n - r) * factrial(r))


def main():
    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    ans = 0
    mod = 10 ** 9 + 7
    for i, a in enumerate(A[k - 1:], k - 1):
        ans += ((a % mod) * (combinations(i, k - 1) % mod)) % mod
        ans %= mod
    for i, a in enumerate(A[: n - k + 1], 1):
        ans -= ((a % mod) * (combinations(n - i, k - 1)) % mod) % mod
        ans %= mod
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
from functools import lru_cache
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
mod = 10 ** 9 + 7
# 階乗とその逆元の計算
factrial = [0 for _ in range(n + 1)]
factrial[0] = 1
for i in range(1, n + 1):
    factrial[i] = (factrial[i - 1] * i) % mod
inverse_factrial = list()
for f in factrial:
    inverse_factrial.append(pow(f, -1, mod))


@lru_cache(maxsize=None)
def nCr(n, r):
    return (factrial[n] * inverse_factrial[r] * inverse_factrial[n - r]) % mod


ans = 0
# 最大値の和
for i, a in enumerate(A[k - 1:], k - 1):
    ans += ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
# 最小値の和
A.reverse()
for i, a in enumerate(A[k - 1:], k - 1):
    ans -= ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
print(ans)

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


def combinations_count(n, r, d):
    if d.get((n, r)):
        return d[(n, r)]
    else:
        x = math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
        d[(n, r)] = x
    return x

d=dict()
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
ans = 0
mod = 10 ** 9 + 7
for i, Ma in enumerate(A[::-1]):
    i = n - i
    for j, ma in enumerate(A):
        if ma == Ma or i - j < k:
            break
        ans += (combinations_count(i - j - 2, k - 2,d) * (Ma - ma)) % mod
        ans %= mod
print(ans)

Fixed submission:
from functools import lru_cache
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
mod = 10 ** 9 + 7
# 階乗とその逆元の計算
factrial = [0 for _ in range(n + 1)]
factrial[0] = 1
for i in range(1, n + 1):
    factrial[i] = (factrial[i - 1] * i) % mod
inverse_factrial = list()
for f in factrial:
    inverse_factrial.append(pow(f, -1, mod))


@lru_cache(maxsize=None)
def nCr(n, r):
    return (factrial[n] * inverse_factrial[r] * inverse_factrial[n - r]) % mod


ans = 0
# 最大値の和
for i, a in enumerate(A[k - 1:], k - 1):
    ans += ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
# 最小値の和
A.reverse()
for i, a in enumerate(A[k - 1:], k - 1):
    ans -= ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
print(ans)

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


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


n, k = map(int, input().split())
A = sorted(map(int, input().split()))
ans = 0
mod = 10 ** 9 + 7
for i, Ma in enumerate(A[::-1]):
    i = n - i
    for j, ma in enumerate(A):
        if ma == Ma or i - j < k:
            break
        ans += (combinations_count(i - j - 2, k - 2) * (Ma - ma)) % mod
        ans %= mod
print(ans)


Fixed submission:
from functools import lru_cache
n, k = map(int, input().split())
A = sorted(map(int, input().split()))
mod = 10 ** 9 + 7
# 階乗とその逆元の計算
factrial = [0 for _ in range(n + 1)]
factrial[0] = 1
for i in range(1, n + 1):
    factrial[i] = (factrial[i - 1] * i) % mod
inverse_factrial = list()
for f in factrial:
    inverse_factrial.append(pow(f, -1, mod))


@lru_cache(maxsize=None)
def nCr(n, r):
    return (factrial[n] * inverse_factrial[r] * inverse_factrial[n - r]) % mod


ans = 0
# 最大値の和
for i, a in enumerate(A[k - 1:], k - 1):
    ans += ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
# 最小値の和
A.reverse()
for i, a in enumerate(A[k - 1:], k - 1):
    ans -= ((a % mod) * nCr(i, k-1)) % mod
    ans %= mod
print(ans)