Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb)
    plus += (A[-i-1] * comb)
print((plus - minus) % mod)

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    N, K = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    minus = 0
    plus = 0
    fact = [0] * (N+1)
    fact[0] = 1
    pow_fact = [0] * (N+1)
    pow_fact[0] = 1
    fa = 1
    for i in range(1, N+1):
        fa *= i
        fact[i] = fa
        pow_fact[i] = pow(fa, mod-2, mod)
    b = pow_fact[K-1]
    for i in range(N-K+1):
        a = fact[N-i-1]
        c = pow_fact[N-i-K]
        comb = (a*b*c) % mod
        minus += (A[i] * comb)
        plus += (A[-i-1] * comb)
    print((plus - minus) % mod)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    N, K = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    minus = 0
    plus = 0
    fact = [0] * (N+1)
    fact[0] = 1
    pow_fact = [0] * (N+1)
    pow_fact[0] = 1
    fa = 1
    for i in range(1, N+1):
        fa *= i
        fact[i] = fa
        pow_fact[i] = pow(fa, mod-2, mod)
    b = pow_fact[K-1]
    for i in range(N-K+1):
        a = fact[N-i-1]
        c = pow_fact[N-i-K]
        comb = (a*b*c) % mod
        minus += (A[i] * comb)
        plus += (A[-i-1] * comb)
    print((plus - minus) % mod)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    N, K = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    minus = 0
    plus = 0
    fact = [0] * (N+1)
    fact[0] = 1
    fa = 1
    for i in range(1, N+1):
        fa *= i
        fact[i] = fa
    b = fact[K-1]
    for i in range(N-K+1):
        a = fact[N-i-1]
        c = fact[N-i-K]
        comb = (a*pow(b, mod-2, mod)*pow(c, mod-2, mod)) % mod
        minus += (A[i] * comb)
        plus += (A[-i-1] * comb)
    print((plus - minus) % mod)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    N, K = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    minus = 0
    plus = 0
    fact = [0] * (N+1)
    fact[0] = 1
    fa = 1
    for i in range(1, N+1):
        fa *= i
        fact[i] = fa
    b = fact[K-1]
    for i in range(N-K+1):
        a = fact[N-i-1]
        c = fact[N-i-K]
        comb = (a*pow(b, mod-2, mod)*pow(c, mod-2, mod)) % mod
        minus += (A[i] * comb)
        plus += (A[-i-1] * comb)
    print((plus - minus) % mod)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)

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

@lru_cache()
def comb_mod(n, k, b, mod):
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    a = factorial(n)
    c = factorial(n-k)
    return (a*pow(b, mod-2, mod)*pow(c, mod-2, mod)) % mod

def main():
    import sys
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    N, K = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    minus = 0
    plus = 0
    b = factorial(K-1)
    for i in range(N-K+1):
        comb = comb_mod(N-i-1, K-1, b, mod)
        minus += (A[i] * comb)
        plus += (A[-i-1] * comb)
    print((plus - minus) % mod)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)

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

@lru_cache()
def comb_mod(n, k, b, mod):
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    a = factorial(n)
    c = factorial(n-k)
    return (a*pow(b, mod-2, mod)*pow(c, mod-2, mod)) % mod

def main():
    import sys
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    N, K = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    minus = 0
    plus = 0
    b = factorial(K-1)
    for i in range(N-K+1):
        comb = comb_mod(N-i-1, K-1, b, mod)
        minus += (A[i] * comb)
        plus += (A[-i-1] * comb)
    print((plus - minus) % mod)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)

Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial
def comb_mod(n, k, mod):
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    a = factorial(n)
    b = factorial(k)
    c = factorial(n-k)
    return (a*pow(b, mod-2, mod)*pow(c, mod-2, mod)) % mod

def main():
    import sys
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    N, K = map(int, input().split())
    A = sorted(list(map(int, input().split())))
    minus = 0
    plus = 0
    for i in range(N-K+1):
        comb = comb_mod(N-i-1, K-1, mod)
        minus += (A[i] * comb)
        plus += (A[-i-1] * comb)
    print((plus - minus) % mod)
if __name__ == '__main__':
    main()

Fixed submission:
import sys
input = sys.stdin.readline
mod = 10 ** 9 + 7
N, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
minus = 0
plus = 0
fact = [0] * (N+1)
fact[0] = 1
pow_fact = [0] * (N+1)
pow_fact[0] = 1
fa = 1
for i in range(1, N+1):
    fa *= i
    fa %= mod
    fact[i] = fa
    pow_fact[i] = pow(fa, mod-2, mod)
b = pow_fact[K-1]
for i in range(N-K+1):
    a = fact[N-i-1]
    c = pow_fact[N-i-K]
    comb = (a*b*c) % mod
    minus += (A[i] * comb) % mod
    plus += (A[-i-1] * comb) % mod
print((plus - minus) % mod)