Buggy python submission (verdict: Time Limit Exceeded):
"""
ジャガー「わからん」
先にmax,minそれぞれの和を求める
"""

import sys
input = sys.stdin.readline

mod = 10**9+7

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    # n!/(n-k)!
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    # k!
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    # n!/((n-k)!*k!)
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

def main():
    N,K = map(int,input().split())
    A = sorted([int(i) for i in input().split()])

    cmb = [0] * (N-K+1)
    for i in range(N-K+1):
        cmb[i] = getComb(i+(K-1),K-1,mod)

    sum_min = 0
    sum_max = 0
    for i in range(N):
        if i >= K-1:
            sum_max = (sum_max + (cmb[i-(K-1)] * A[i]) % mod) % mod
        if i <= N-K: 
            sum_min = (sum_min + (cmb[(N-K)-i] * A[i]) % mod) % mod
        
    answer = (sum_max - sum_min) % mod
    print(answer)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p


N,K = map(int,input().split())
A = sorted([int(i) for i in input().split()])

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
fact = [1, 1]
factinv = [1, 1] 
inv = [0, 1]
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print(answer % mod)

Buggy python submission (verdict: Time Limit Exceeded):
"""
ジャガー「わからん」
先にmax,minそれぞれの和を求める
"""

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    # n!/(n-k)!
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    # k!
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    # n!/((n-k)!*k!)
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

def main():
    N,K = map(int,input().split())
    A = sorted([int(i) for i in input().split()])
    #print(A)
    
    mod = 10**9+7

    cmb = [0] * (N-K+1)
    for i in range(N-K+1):
        cmb[i] = getComb(i+(K-1),K-1,mod)
    #print(cmb)

    sum_max = 0
    for i in range(K-1,N):
        sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod
    #print(sum_max)

    sum_min = 0
    for i in range(N-K,-1,-1):
        sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod
    #print(sum_min)

    answer = sum_max - sum_min
    print(answer % mod)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p


N,K = map(int,input().split())
A = sorted([int(i) for i in input().split()])

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
fact = [1, 1]
factinv = [1, 1] 
inv = [0, 1]
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print(answer % mod)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    # n!/(n-k)!
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    # k!
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    # n!/((n-k)!*k!)
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

def main():
    N,K = map(int,input().split())
    A = sorted([int(i) for i in input().split()])

    mod = 10**9+7

    cmb = [0] * N
    for i in range(N):
        cmb[i] = getComb(i,K-2,mod)

    answer = 0
    for i in range(N):
        for j in range(i+(K-1), N):
            answer = (answer + cmb[j-i-1] * ((A[j] - A[i]) % mod)) % mod

    print(answer)

if __name__ == "__main__":
    main()

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p


N,K = map(int,input().split())
A = sorted([int(i) for i in input().split()])

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
fact = [1, 1]
factinv = [1, 1] 
inv = [0, 1]
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print(answer % mod)

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    # n!/(n-k)!
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    # k!
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    # n!/((n-k)!*k!)
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

def main():
    N,K = map(int,input().split())
    A = sorted([int(i) for i in input().split()])

    mod = 10**9+7

    cmb = [0] * N
    for i in range(N):
        cmb[i] = getComb(i,K-2,mod)

    answer = 0
    for i in range(N):
        for j in range(i+(K-1), N):
            answer = (answer + cmb[j-i-1] * ((A[j] - A[i]) % mod)) % mod

    print(answer)

if __name__ == "__main__":

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p


N,K = map(int,input().split())
A = sorted([int(i) for i in input().split()])

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
fact = [1, 1]
factinv = [1, 1] 
inv = [0, 1]
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print(answer % mod)

Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    # n!/(n-k)!
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    # k!
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    # n!/((n-k)!*k!)
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

def main():
    N,K = map(int,input().split())
    A = sorted([int(i) for i in input().split()])

    mod = 10**9+7

    cmb = [0] * N
    for i in range(N):
        cmb[i] = getComb(i,K-2,mod)

    answer = 0
    for i in range(N):
        for j in range(i+(K-1), N):
            answer = (answer + cmb[j-i-1] * ((A[j] - A[i]) % mod)) % mod

    print(answer)

if __name__ == "__main__":

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p


N,K = map(int,input().split())
A = sorted([int(i) for i in input().split()])

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
fact = [1, 1]
factinv = [1, 1] 
inv = [0, 1]
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print(answer % mod)

Buggy python submission (verdict: Time Limit Exceeded):
# TLE :(

def getComb(n, k, MOD):
    if n < k:
        return 0
    if n-k < k:
        k = n-k
    # n!/(n-k)!
    comb = 1
    for x in range(n-k+1, n+1):
        comb = (comb * x) % MOD
    # k!
    d = 1
    for x in range(1, k+1):
        d = (d * x) % MOD
    # n!/((n-k)!*k!)
    comb *= pow(d, MOD-2, MOD)
    return comb % MOD

from sys import stdin
input = stdin.readline

N,K = map(int,input().split())
A = sorted([int(i) for i in input().split()])

mod = 10**9+7

answer = 0
for i in range(N):
    for j in range(i+(K-1), N):
        answer = (answer + getComb(j-(i+1),K-2,mod) * ((A[j] - A[i]) % mod)) % mod

print(answer)

Fixed submission:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

def getComb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p


N,K = map(int,input().split())
A = sorted([int(i) for i in input().split()])

# ref: https://www.planeta.tokyo/entry/5195/
mod = 10**9+7
fact = [1, 1]
factinv = [1, 1] 
inv = [0, 1]
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)

cmb = [0] * (N-K+1)
for i in range(N-K+1):
    cmb[i] = getComb(i+(K-1),K-1,mod)

sum_max = 0
for i in range(K-1,N):
    sum_max = (sum_max + cmb[i-(K-1)] * A[i]) % mod

sum_min = 0
for i in range(N-K,-1,-1):
    sum_min = (sum_min + cmb[(N-K)-i] * A[i]) % mod

answer = sum_max - sum_min
print(answer % mod)