Buggy python submission (verdict: Time Limit Exceeded):
import sys

def main():
    input = sys.stdin.readline

    MOD = pow(10, 9) + 7

    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    B = A[::-1]
    fact = [1]
    for i in range(1, n+1):
        fact.append((fact[i-1]*i)%MOD)
    now = k-2
    S = []
    while now <= n-2:
        tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
        S.append(tmp)
        now += 1

    ans = 0
    for i, a in enumerate(A):
        now = k-2
        j = 0
        while now <= n-2-i:
            tmp = S[j]
            ans = (ans - a*tmp)%MOD
            now += 1
            j += 1

    for i, b in enumerate(B):
        now = k-2
        j = 0
        while now <= n-2-i:
            tmp = S[j]
            ans = (ans + b*tmp)%MOD
            now += 1
            j += 1
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

    MOD = pow(10, 9) + 7

    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    B = A[::-1]
    fact = [1]
    for i in range(1, n+1):
        fact.append((fact[i-1]*i)%MOD)
    now = k-2
    S = []
    while now <= n-2:
        tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
        S.append(tmp)
        now += 1
    cumsum = [0, ]
    for i, s in enumerate(S):
        cumsum.append((cumsum[i]+s) % MOD)
    ans = 0
    for i, a in enumerate(A):
        ans = (ans - a*cumsum[-(i+1)])%MOD
        if cumsum[-(i+1)] == 0:
            break
    for i, b in enumerate(B):
        ans = (ans + b*cumsum[-(i+1)])%MOD
        if cumsum[-(i+1)] == 0:
            break
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def main():
    input = sys.stdin.readline

    MOD = pow(10, 9) + 7

    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    B = A[::-1]
    fact = [1]
    for i in range(1, n+1):
        fact.append((fact[i-1]*i)%MOD)

    ans = 0
    for i, a in enumerate(A):
        now = k-2
        while now <= n-2-i:
            tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
            ans = (ans - a*tmp)%MOD
            now += 1
        now = k-2

    for i, b in enumerate(B):
        now = k-2
        while now <= n-2-i:
            tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
            ans = (ans + b*tmp)%MOD
            now += 1
    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

    MOD = pow(10, 9) + 7

    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    B = A[::-1]
    fact = [1]
    for i in range(1, n+1):
        fact.append((fact[i-1]*i)%MOD)
    now = k-2
    S = []
    while now <= n-2:
        tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
        S.append(tmp)
        now += 1
    cumsum = [0, ]
    for i, s in enumerate(S):
        cumsum.append((cumsum[i]+s) % MOD)
    ans = 0
    for i, a in enumerate(A):
        ans = (ans - a*cumsum[-(i+1)])%MOD
        if cumsum[-(i+1)] == 0:
            break
    for i, b in enumerate(B):
        ans = (ans + b*cumsum[-(i+1)])%MOD
        if cumsum[-(i+1)] == 0:
            break
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
MOD = pow(10, 9) + 7

n, k = map(int, input().split())
A = sorted(map(int, input().split()))
B = A[::-1]
fact = [1]
for i in range(1,n+1):
    fact.append((fact[i-1]*i)%MOD)

ans = 0
for i, a in enumerate(A):
    now = k-2
    while now <= n-2-i:
        tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
        ans = (ans - a*tmp)%MOD
        now += 1

for i, b in enumerate(B):
    now = k-2
    while now <= n-2-i:
        tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
        ans = (ans + b*tmp)%MOD
        now += 1
print(ans)


Fixed submission:
import sys

def main():
    input = sys.stdin.readline

    MOD = pow(10, 9) + 7

    n, k = map(int, input().split())
    A = sorted(map(int, input().split()))
    B = A[::-1]
    fact = [1]
    for i in range(1, n+1):
        fact.append((fact[i-1]*i)%MOD)
    now = k-2
    S = []
    while now <= n-2:
        tmp = ((fact[now] % MOD * pow(fact[now-(k-2)], MOD-2, MOD)) % MOD * pow(fact[k-2], MOD-2, MOD)) % MOD
        S.append(tmp)
        now += 1
    cumsum = [0, ]
    for i, s in enumerate(S):
        cumsum.append((cumsum[i]+s) % MOD)
    ans = 0
    for i, a in enumerate(A):
        ans = (ans - a*cumsum[-(i+1)])%MOD
        if cumsum[-(i+1)] == 0:
            break
    for i, b in enumerate(B):
        ans = (ans + b*cumsum[-(i+1)])%MOD
        if cumsum[-(i+1)] == 0:
            break
    print(ans)


if __name__ == '__main__':
    main()