Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
total = 0
def _cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**5
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

for i in range(2, N + 1):
    g1.append(( g1[-1] * i ) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)

mem = {}

def cmb(n, r, mod):
    if (n,r) not in mem:
        mem[(n,r)] = _cmb(n, r, mod)
    return mem[(n,r)]

for i in range(n-k+1):
    for j in range(i+k-1,n):
        total += cmb(j-i-1,k-2,mod)*(a[j]-a[i])
        total %= mod
print(total)

Fixed submission:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
total = 0
def cmb(n, r, mod):
    if (r<0 or r>n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**5
g1 = [1,1]
g2 = [1,1]
inverse = [0,1]

for i in range(2, N + 1):
    g1.append((g1[-1]*i) % mod)
    inverse.append((-inverse[mod%i] * (mod//i)) % mod)
    g2.append((g2[-1]*inverse[-1]) % mod)

for i in range(n-k+1):
    total += cmb(n-i-1,k-1,mod)*(a[n-i-1]-a[i])
    total %= mod
print(total)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
total = 0
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**5
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

for i in range(2, N + 1):
    g1.append(( g1[-1] * i ) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)

for i in range(n-k+1):
    for j in range(i+k-1,n):
        total += cmb(j-i-1,k-2,mod)*(a[j]-a[i])
        total %= mod
print(total)

Fixed submission:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
total = 0
def cmb(n, r, mod):
    if (r<0 or r>n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**5
g1 = [1,1]
g2 = [1,1]
inverse = [0,1]

for i in range(2, N + 1):
    g1.append((g1[-1]*i) % mod)
    inverse.append((-inverse[mod%i] * (mod//i)) % mod)
    g2.append((g2[-1]*inverse[-1]) % mod)

for i in range(n-k+1):
    total += cmb(n-i-1,k-1,mod)*(a[n-i-1]-a[i])
    total %= mod
print(total)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
total = 0
def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**4
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]

for i in range(2, N + 1):
    g1.append(( g1[-1] * i ) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)

for i in range(n-k+1):
    for j in range(i+k-1,n):
        total += cmb(j-i-1,k-2,mod)*(a[j]-a[i])
        total %= mod
print(total)

Fixed submission:
n, k = map(int, input().split())
a = [int(i) for i in input().split()]
a.sort()
total = 0
def cmb(n, r, mod):
    if (r<0 or r>n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7
N = 10**5
g1 = [1,1]
g2 = [1,1]
inverse = [0,1]

for i in range(2, N + 1):
    g1.append((g1[-1]*i) % mod)
    inverse.append((-inverse[mod%i] * (mod//i)) % mod)
    g2.append((g2[-1]*inverse[-1]) % mod)

for i in range(n-k+1):
    total += cmb(n-i-1,k-1,mod)*(a[n-i-1]-a[i])
    total %= mod
print(total)