Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
import time

MOD = 1000000007  # type: int


def solve(N: int, K: int, A: "List[int]"):
    #フェルマーの小定理を最後に使って逆元を求めてもいい
    p = MOD
    a = N
    time.sleep(10)

    fac = [1]*(a+1)
    inv = [1]*(a+1)
    finv = [1]*(a+1)

    for i in range(2, a+1):
        fac[i] = fac[i-1] * i % p
        inv[i] = p - (inv[p%i] * (p//i) )% p #拡張ユークリッドの互除法を応用し、逆元をO(1)で求める
        finv[i] = finv[i-1] * inv[i] % p

    def cmb(a, b):
        if a < b or a < 0 or b < 0:
            return 0
        return fac[a] * (finv[b] * finv[a - b] % p) % p

    ans = 0
    A.sort()
    n = A[N-1]
    b = 0
    c = 0
    for i in range(N-1, -1, -1):
        if A[i] == n:
            c += 1
            continue
        else:
            ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1

    ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans_m = 0
    n = A[0]
    b = 0
    c = 0
    for i in range(N):
        if A[i] == n:
            c += 1
            continue
        else:
            ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1
    ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans -= ans_m
    if ans < 0:
        ans += MOD
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int


def solve(N: int, K: int, A: "List[int]"):
    #フェルマーの小定理を最後に使って逆元を求めてもいい
    p = MOD
    a = N

    fac = [1]*(a+1)
    inv = [1]*(a+1)
    finv = [1]*(a+1)

    for i in range(2, a+1):
        fac[i] = fac[i-1] * i % p
        inv[i] = p - (inv[p%i] * (p//i) )% p #拡張ユークリッドの互除法を応用し、逆元をO(1)で求める
        finv[i] = finv[i-1] * inv[i] % p

    def cmb(a, b):
        if a < b or a < 0 or b < 0:
            return 0
        return fac[a] * (finv[b] * finv[a - b] % p) % p

    ans = 0
    A.sort()
    n = A[N-1]
    b = 0
    c = 0
    for i in range(N-1, -1, -1):
        if A[i] == n:
            c += 1
            continue
        else:
            ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1

    ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans_m = 0
    n = A[0]
    b = 0
    c = 0
    for i in range(N):
        if A[i] == n:
            c += 1
            continue
        else:
            ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1
    ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans -= ans_m
    if ans < 0:
        ans += MOD
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int


def solve(N: int, K: int, A: "List[int]"):
    def cmb(a, b):
        #フェルマーの小定理を最後に使って逆元を求めてもいい
        p = MOD

        if a < b or a < 0 or b < 0:
            return 0
        fac = [1]*(a+1)
        inv = [1]*(a+1)
        finv = [1]*(a+1)

        for i in range(2, a+1):
            fac[i] = fac[i-1] * i % p
            inv[i] = p - (inv[p%i] * (p//i) )% p #拡張ユークリッドの互除法を応用し、逆元をO(1)で求める
            finv[i] = finv[i-1] * inv[i] % p

        return fac[a] * (finv[b] * finv[a - b] % p) % p

    ans = 0
    A.sort()
    n = A[N-1]
    b = 0
    c = 0
    for i in range(N-1, -1, -1):
        if A[i] == n:
            c += 1
            continue
        else:
            ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1

    ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans_m = 0
    n = A[0]
    b = 0
    c = 0
    for i in range(N):
        if A[i] == n:
            c += 1
            continue
        else:
            ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1
    ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans -= ans_m
    if ans < 0:
        ans += MOD
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()


Fixed submission:
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int


def solve(N: int, K: int, A: "List[int]"):
    #フェルマーの小定理を最後に使って逆元を求めてもいい
    p = MOD
    a = N

    fac = [1]*(a+1)
    inv = [1]*(a+1)
    finv = [1]*(a+1)

    for i in range(2, a+1):
        fac[i] = fac[i-1] * i % p
        inv[i] = p - (inv[p%i] * (p//i) )% p #拡張ユークリッドの互除法を応用し、逆元をO(1)で求める
        finv[i] = finv[i-1] * inv[i] % p

    def cmb(a, b):
        if a < b or a < 0 or b < 0:
            return 0
        return fac[a] * (finv[b] * finv[a - b] % p) % p

    ans = 0
    A.sort()
    n = A[N-1]
    b = 0
    c = 0
    for i in range(N-1, -1, -1):
        if A[i] == n:
            c += 1
            continue
        else:
            ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1

    ans = (ans + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans_m = 0
    n = A[0]
    b = 0
    c = 0
    for i in range(N):
        if A[i] == n:
            c += 1
            continue
        else:
            ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
            b += c
            n = A[i]
            c = 1
    ans_m = (ans_m + n * (cmb(N-b, K) - cmb(N-b-c, K)))% MOD
    ans -= ans_m
    if ans < 0:
        ans += MOD
    print(ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()