Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i // j
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()

Fixed submission:
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i * pow(j, mod - 2, mod) % mod
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i // j
        ans += (A[i] - A[N-i-1]) * comb
    
    print(ans % mod)

solve()

Fixed submission:
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i * pow(j, mod - 2, mod) % mod
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
f_i = stdin

mod = 10 ** 9 + 7

N, K = map(int, f_i.readline().split())
A = sorted(map(int, f_i.readline().split()))

comb = 1
ans = A[K-1] - A[N-K]
for i, j, A_s, A_t in zip(range(K, N), range(1, N), A[K:], A[N-K-1::-1]):
    comb = comb * i // j
    ans += (A_s - A_t) * comb

print(ans % mod)

Fixed submission:
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i * pow(j, mod - 2, mod) % mod
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j, A_s, A_t in zip(range(K, N), range(1, N), A[K:], A[N-K-1::-1]):
        comb = comb * i // j
        ans += (A_s - A_t) * comb
    
    print(ans % mod)

solve()

Fixed submission:
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i * pow(j, mod - 2, mod) % mod
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    num = [1] * (N - K + 1)
    for i, j in zip(range(K, N), range(1, N)):
        num[j] = num[j-1] * i // j
    
    ans = sum(a * n % mod for a, n in zip(A[K-1:], num))
    ans -= sum(a * n % mod for a, n in zip(A, num[::-1]))
    
    print(ans % mod)

solve()

Fixed submission:
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i * pow(j, mod - 2, mod) % mod
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()

Buggy python submission (verdict: Runtime Error):
def solve():
    from sys import stdin
    #f_i = stdin
    f_i = open('test_in_4')#
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    num = [1] * (N - K + 1)
    for i, j in zip(range(K, N), range(1, N)):
        num[j] = num[j-1] * i // j
    
    ans = sum(a * n % mod for a, n in zip(A[K-1:], num))
    ans -= sum(a * n % mod for a, n in zip(A, num[::-1]))
    
    print(ans % mod)

solve()

Fixed submission:
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i * pow(j, mod - 2, mod) % mod
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()

Buggy python submission (verdict: Time Limit Exceeded):
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    num = [1] * (N - K + 1)
    for i, j in zip(range(K, N), range(1, N)):
        num[j] = num[j-1] * i // j
    
    ans = sum(a * n for a, n in zip(A[K-1:], num))
    ans -= sum(a * n for a, n in zip(A, num[::-1]))
    
    print(ans % mod)

solve()

Fixed submission:
def solve():
    from sys import stdin
    f_i = stdin
    
    mod = 10 ** 9 + 7
    
    N, K = map(int, f_i.readline().split())
    A = sorted(map(int, f_i.readline().split()))
    
    comb = 1
    ans = A[K-1] - A[N-K]
    for i, j in zip(range(K, N), range(1, N)):
        comb = comb * i * pow(j, mod - 2, mod) % mod
        ans += (A[i] - A[N-i-1]) * comb
        ans %= mod
    
    print(ans)

solve()