Buggy python submission (verdict: Runtime Error):
MOD = 10 ** 9 + 7
MAX = 10 ** 5 + 100

n, k = map(int, input().split())
a = sorted(map(int, input().split()))

def nCr(n, r):
    return (fac[n] * modInverse[fac[r]] % MOD * modInverse[fac[n - r]] % MOD) % MOD

def modularInverse(n):
    dp = [0] * (n + 1)
    dp[0] = dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = dp[MOD % i] * (MOD - MOD // i) % MOD
    return dp

fac = [1, 1]
for i in range(2, MAX + 1):
    fac.append((fac[-1] * i) % MOD)
modInverse = modularInverse(MAX)

ret = 0
for i in range(n - k + 2):
    for j in range(i + k - 1, n):
        diff = a[j] - a[i]
        between = max(0, j - i - 1)
        choose = max(0, k - 2)
        ret += (nCr(between, choose) * diff) % MOD
        ret %= MOD
print(ret)

Fixed submission:
MOD = 10 ** 9 + 7

n, k = map(int, input().split())
a = sorted(map(int, input().split()))

factorialNumInverse = [None] * (n + 1)
naturalNumInverse = [None] * (n + 1)
fact = [None] * (n + 1)

def inverseofNumber():
    naturalNumInverse[0] = naturalNumInverse[1] = 1
    for i in range(2, n + 1):
        naturalNumInverse[i] = (naturalNumInverse[MOD % i] * (MOD - MOD // i) % MOD)

def inverseofFactorial():
    factorialNumInverse[0] = factorialNumInverse[1] = 1
    for i in range(2, n + 1):
        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % MOD

def factorial():
    fact[0] = 1
    for i in range(1, n + 1):
        fact[i] = (fact[i - 1] * i) % MOD

inverseofNumber()
inverseofFactorial()
factorial()

def nCk(n, k):
    ans = ((fact[n] * factorialNumInverse[k]) % MOD * factorialNumInverse[n - k]) % MOD
    return ans

def calcSum(a):
    ret = 0
    for i in range(k - 1, n):
        ret += (nCk(i, k - 1) * a[i]) % MOD
        ret %= MOD
    return ret

print((calcSum(a) - calcSum(a[::-1])) % MOD)

Buggy python submission (verdict: Runtime Error):
MOD = 10 ** 9 + 7
MAX = 10 ** 5

n, k = map(int, input().split())
a = sorted(map(int, input().split()))

def nCr(n, r):
    return (fac[n] * modInverse[fac[r]] % MOD * modInverse[fac[n - r]] % MOD) % MOD

def modularInverse(n):
    dp = [0] * (n + 1)
    dp[0] = dp[1] = 1
    for i in range(2, n + 1):
        dp[i] = dp[MOD % i] * (MOD - MOD // i) % MOD
    return dp

fac = [1, 1]
for i in range(2, MAX + 1):
    fac.append((fac[-1] * i) % MOD)
modInverse = modularInverse(MAX)

ret = 0
for i in range(n - k + 2):
    for j in range(i + k - 1, n):
        diff = a[j] - a[i]
        between = max(0, j - i - 1)
        choose = max(0, k - 2)
        ret += (nCr(between, choose) * diff) % MOD
        ret %= MOD
print(ret)

Fixed submission:
MOD = 10 ** 9 + 7

n, k = map(int, input().split())
a = sorted(map(int, input().split()))

factorialNumInverse = [None] * (n + 1)
naturalNumInverse = [None] * (n + 1)
fact = [None] * (n + 1)

def inverseofNumber():
    naturalNumInverse[0] = naturalNumInverse[1] = 1
    for i in range(2, n + 1):
        naturalNumInverse[i] = (naturalNumInverse[MOD % i] * (MOD - MOD // i) % MOD)

def inverseofFactorial():
    factorialNumInverse[0] = factorialNumInverse[1] = 1
    for i in range(2, n + 1):
        factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % MOD

def factorial():
    fact[0] = 1
    for i in range(1, n + 1):
        fact[i] = (fact[i - 1] * i) % MOD

inverseofNumber()
inverseofFactorial()
factorial()

def nCk(n, k):
    ans = ((fact[n] * factorialNumInverse[k]) % MOD * factorialNumInverse[n - k]) % MOD
    return ans

def calcSum(a):
    ret = 0
    for i in range(k - 1, n):
        ret += (nCk(i, k - 1) * a[i]) % MOD
        ret %= MOD
    return ret

print((calcSum(a) - calcSum(a[::-1])) % MOD)