Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        for j in range(0, i - k + 2):
            ans += (a[i] - a[j]) * cmb(i - j - 1, k - 2)
            #print(j, i, ' ', a[j],a[i], ' ', a[i] - a[j])
    ans %= pow(10, 9) + 7
    print(ans)
else:
    print(0)


Fixed submission:
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        maxs = a[i]
        ncr = cmb(i, k-1)
        #print(maxs, ncr, maxs * ncr)
        ans += maxs * ncr
        ans %= mod
    for i in range(n-k+1):
        mins = a[i]
        ncr = cmb(n-i-1, k-1)
        #print(mins, ncr, mins * ncr)
        ans -= mins * ncr
        ans %= mod
    print(ans)
else:
    print(0)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        for j in range(0, i - k + 2):
            ans += (a[i] - a[j]) * cmb(i - j - 1, k - 2)
            #print(j, i, ' ', a[j],a[i], ' ', a[i] - a[j])
    ans %= pow(10, 9) + 7
    print(ans)
else:
    print(0)


Fixed submission:
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        maxs = a[i]
        ncr = cmb(i, k-1)
        #print(maxs, ncr, maxs * ncr)
        ans += maxs * ncr
        ans %= mod
    for i in range(n-k+1):
        mins = a[i]
        ncr = cmb(n-i-1, k-1)
        #print(mins, ncr, mins * ncr)
        ans -= mins * ncr
        ans %= mod
    print(ans)
else:
    print(0)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result


n,k = map(int,input().split())
a = [int(i) for i in input().split()]

a.sort()
#print(a)
if k > 1:
    ans = 0
    for i in range(k-1, n):
        for j in range(0, i - k + 2):
            ans += (a[i] - a[j]) * cmb(i - j - 1, k - 2)
            #print(j, i, ' ', a[j],a[i], ' ', a[i] - a[j])
    ans %= pow(10, 9) + 7
    print(ans)
else:
    print(0)

Fixed submission:
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        maxs = a[i]
        ncr = cmb(i, k-1)
        #print(maxs, ncr, maxs * ncr)
        ans += maxs * ncr
        ans %= mod
    for i in range(n-k+1):
        mins = a[i]
        ncr = cmb(n-i-1, k-1)
        #print(mins, ncr, mins * ncr)
        ans -= mins * ncr
        ans %= mod
    print(ans)
else:
    print(0)

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

n,k = map(int,input().split())
a = [int(i) for i in input().split()]

a.sort()
ans = 0
#print(a)
if k > 1:
    for i in range(k-1, n):
        for j in range(0, i - k + 2):
            ans += (a[i] - a[j]) * factorial(i - j - 1) // factorial(k - 2) // factorial(i - j - 1 - k + 2)
            #print(j, i, ' ', a[j],a[i], ' ', a[i] - a[j])

    ans %= pow(10, 9) + 7
    print(ans)
else:
    print(0)

Fixed submission:
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        maxs = a[i]
        ncr = cmb(i, k-1)
        #print(maxs, ncr, maxs * ncr)
        ans += maxs * ncr
        ans %= mod
    for i in range(n-k+1):
        mins = a[i]
        ncr = cmb(n-i-1, k-1)
        #print(mins, ncr, mins * ncr)
        ans -= mins * ncr
        ans %= mod
    print(ans)
else:
    print(0)

Buggy python submission (verdict: Time Limit Exceeded):
n,k = map(int,input().split())
a = [int(i) for i in input().split()]

from itertools import combinations
b = list(combinations(a, k))
ans = 0
for i in range(len(b)):
    ans += max(b[i]) - min(b[i])
ans %= pow(10, 9) + 7

print(ans)

Fixed submission:
def cmb(n, r):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % mod

N = pow(10, 5)  # N は必要分だけ用意する
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用
mod = pow(10, 9) + 7

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % mod)
    inv.append((-inv[mod % i] * (mod // i)) % mod)
    factinv.append((factinv[-1] * inv[-1]) % mod)

n,k = map(int,input().split())
a = [int(i) for i in input().split()]
a.sort()
if k > 1:
    ans = 0
    for i in range(k-1, n):
        maxs = a[i]
        ncr = cmb(i, k-1)
        #print(maxs, ncr, maxs * ncr)
        ans += maxs * ncr
        ans %= mod
    for i in range(n-k+1):
        mins = a[i]
        ncr = cmb(n-i-1, k-1)
        #print(mins, ncr, mins * ncr)
        ans -= mins * ncr
        ans %= mod
    print(ans)
else:
    print(0)