Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    def ncr(n, r, mod):
        r = min(r, n-r)
        numer = denom = 1
        for i in range(1, r+1):
            numer = numer * (n+1-i) % mod
            denom = denom * i % mod
        return numer * pow(denom, mod-2, mod) % mod

    n,k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    res_max, res_min = 0, 0
    for i in range(k-1, n):
        res_max += a[i] * ncr(i, k-1, mod) % mod
    for i in range(n-k+1):
        res_min += a[i] * ncr(n-i-1, k-1, mod) % mod
    
    print((res_max - res_min)%mod)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    max_n = 10**5
    fac, inv = [1]*(max_n+1), [0]*(max_n+1)
    for i in range(2, max_n+1):
        fac[i] = fac[i-1] * i % mod
    inv[-1] = pow(fac[-1], mod-2, mod)
    for i in range(max_n, 0, -1):
        inv[i-1] = inv[i] * i % mod

    def ncr(n, r):
        return fac[n]*inv[r]*inv[n-r]%mod

    n,k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    res_max,res_min = 0, 0
    for i in range(k-1, n):
        temp = ncr(i, k-1)
        res_max += a[i]*temp
        res_min += a[-(i+1)]*temp

    print((res_max - res_min)%mod)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    def ncr(n, r, mod):
        r = min(r, n-r)
        numer = denom = 1
        for i in range(1, r+1):
            numer = numer * (n+1-i) % mod
            denom = denom * i % mod
        return numer * pow(denom, mod-2, mod) % mod

    n,k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    res_max, res_min = 0, 0
    for i in range(k-1, n):
        res_max += a[i] * ncr(i, k-1, mod) % mod
    for i in range(n-k+1):
        res_min += a[i] * ncr(n-i-1, k-1, mod) % mod
    
    print((res_max - res_min)%mod)

if __name__ == '__main__':
    main()

Fixed submission:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    mod = 10**9 + 7

    max_n = 10**5
    fac, inv = [1]*(max_n+1), [0]*(max_n+1)
    for i in range(2, max_n+1):
        fac[i] = fac[i-1] * i % mod
    inv[-1] = pow(fac[-1], mod-2, mod)
    for i in range(max_n, 0, -1):
        inv[i-1] = inv[i] * i % mod

    def ncr(n, r):
        return fac[n]*inv[r]*inv[n-r]%mod

    n,k = map(int, input().split())
    a = list(map(int, input().split()))
    a.sort()

    res_max,res_min = 0, 0
    for i in range(k-1, n):
        temp = ncr(i, k-1)
        res_max += a[i]*temp
        res_min += a[-(i+1)]*temp

    print((res_max - res_min)%mod)

if __name__ == '__main__':
    main()