Buggy python submission (verdict: Time Limit Exceeded):
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def invmod(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


n, k = map(int, input().split())
k_ = k - 2
a = sorted([int(i) for i in input().split(" ")])
c = 10**9 + 7

comb = [1 for i in range(n)]
for i in range(1, n-1):
    comb[i] = comb[i-1] * (k_+i) * invmod(i, c) % c
ans = 0
for i in range(n-1, max(-1, n-k-2), -1):
    for j in range(i-k+2):
        #print("ij", i, j)
        tmp = a[i] - a[j]
        
        ans += tmp*comb[i-1-j-k_] % c
        #print("comb_temp", comb[i-2-j-k_], tmp)
        #print("ans", ans)
print(ans%c)



Fixed submission:
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def invmod(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


n, k = map(int, input().split())
k_ = k - 1
a = sorted([int(i) for i in input().split(" ")])
c = 10**9 + 7

comb = [1 for i in range(n-k_)]
for i in range(1, n-k_):
    comb[i] = comb[i-1] * (k_+i) * invmod(i, c) % c
ans = 0
for i in range(k_, n):       
        ans += a[i]*comb[i-k_] % c
        #print("comb_temp", comb[i-2-j-k_], tmp)
        #print("ans", ans)
for i in range(n-k+1):
    ans -= a[i]*comb[n-k-i]
    ans = ans % c
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def invmod(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


n, k = map(int, input().split())
k_ = k - 2
a = sorted([int(i) for i in input().split(" ")])
c = 10**9 + 7
b_pro = [1 for i in range(n)]
inv_pro = [1 for i in range(n)]
for i in range(2,n+1):
    b_pro[i-1] = b_pro[i-2]*i % c
    inv_pro[i-1] = inv_pro[i-2] * invmod(i, c) % c
if k_ == 0:
    comb = [1 for i in range(n)]
else:
    comb = [1] + [b_pro[i]*inv_pro[i-k_]%c*inv_pro[k_-1]%c for i in range(k_, n-2)]
ans = 0
for i in range(n-1, max(-1, n-k-2), -1):
    for j in range(i-k+2):
        #print("ij", i, j)
        tmp = a[i] - a[j]
        
        ans += tmp*comb[i-1-j-k_] % c
        #print("comb_temp", comb[i-2-j-k_], tmp)
        #print("ans", ans)
print(ans%c)

Fixed submission:
def xgcd(a, b):
    x0, y0, x1, y1 = 1, 0, 0, 1
    while b != 0:
        q, a, b = a // b, b, a % b
        x0, x1 = x1, x0 - q * x1
        y0, y1 = y1, y0 - q * y1
    return a, x0, y0

def invmod(a, m):
    g, x, y = xgcd(a, m)
    if g != 1:
        raise Exception('modular inverse does not exist')
    else:
        return x % m


n, k = map(int, input().split())
k_ = k - 1
a = sorted([int(i) for i in input().split(" ")])
c = 10**9 + 7

comb = [1 for i in range(n-k_)]
for i in range(1, n-k_):
    comb[i] = comb[i-1] * (k_+i) * invmod(i, c) % c
ans = 0
for i in range(k_, n):       
        ans += a[i]*comb[i-k_] % c
        #print("comb_temp", comb[i-2-j-k_], tmp)
        #print("ans", ans)
for i in range(n-k+1):
    ans -= a[i]*comb[n-k-i]
    ans = ans % c
print(ans)