Buggy python submission (verdict: Runtime Error):
inf = pow(10, 9)+7

def nCr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return n;
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i in range(r)]
    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1: 
            result *= numerator[k]
    return int(result)


n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
key = nCr(n-1, k-1)
for i in range(n-k+1):
    ans += ((a[-i-1] - a[i]))*(key)
    ans %= inf
    if n-i-1 != 0:
	    key = int(((key%inf)*(n-k-i)*pow((n-1-i), inf-2, inf))%inf)
    else:
        break
print(ans)

Fixed submission:
inf = pow(10, 9)+7

n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
comb = 1
for i in range(k, n+1):
    ans += (a[i-1]-a[-i])*comb
    ans %= inf
    comb = (comb * i * pow(i-k+1, inf-2, inf))%inf
print(ans)

Buggy python submission (verdict: Runtime Error):
inf = pow(10, 9)+7

def nCr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return n;
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i in range(r)]
    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1: 
            result *= numerator[k]
    return int(result)


n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
key = nCr(n-1, k-1)
for i in range(n-k+1):
    ans += ((a[-i-1] - a[i]))*(key)
    ans %= inf
    if n-i-1 != 0:
	    key = int(key*(n-k-i)/(n-1-i))
    else:
        break
print(ans)

Fixed submission:
inf = pow(10, 9)+7

n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
comb = 1
for i in range(k, n+1):
    ans += (a[i-1]-a[-i])*comb
    ans %= inf
    comb = (comb * i * pow(i-k+1, inf-2, inf))%inf
print(ans)

Buggy python submission (verdict: Runtime Error):
inf = pow(10, 9)+7

def nCr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return n;
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i in range(r)]
    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1: 
            result *= numerator[k]
    return int(result)


n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
key = nCr(n-1, k-1)
for i in range(n-k+1):
    ans += ((a[-i-1] - a[i])%inf)*(key%inf)
    ans %= inf
    if n-i-1 != 0:
	    key = int(key*(n-k-i)/(n-1-i))
    else:
        break
print(ans)

Fixed submission:
inf = pow(10, 9)+7

n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
comb = 1
for i in range(k, n+1):
    ans += (a[i-1]-a[-i])*comb
    ans %= inf
    comb = (comb * i * pow(i-k+1, inf-2, inf))%inf
print(ans)

Buggy python submission (verdict: Runtime Error):
inf = pow(10, 9)+7

def nCr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return n;
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i in range(r)]
    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1: 
            result *= numerator[k]
    return int(result)


n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
key = nCr(n-1, k-1)
for i in range(n-k+1):
    ans += ((a[-i-1] - a[i])%inf)*(key%inf)
    ans %= inf
    if n-i-1 != 0:
	    key = key*(n-k-i)//(n-1-i)
    else:
        break
print(ans)

Fixed submission:
inf = pow(10, 9)+7

n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
comb = 1
for i in range(k, n+1):
    ans += (a[i-1]-a[-i])*comb
    ans %= inf
    comb = (comb * i * pow(i-k+1, inf-2, inf))%inf
print(ans)

Buggy python submission (verdict: Runtime Error):
inf = pow(10, 9)+7

def nCr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return n;
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i in range(r)]
    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1: 
            result *= numerator[k]
    return result


n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
key = (nCr(n-1, k-1))
for i in range(n-k+1):
    ans += ((a[-i-1] - a[i])%inf)*(key%inf)
    ans %= inf
    if n-i-1 != 0:
	    key = key*(n-k-i)//(n-1-i)
    else:
        break
print(int(ans))

Fixed submission:
inf = pow(10, 9)+7

n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
comb = 1
for i in range(k, n+1):
    ans += (a[i-1]-a[-i])*comb
    ans %= inf
    comb = (comb * i * pow(i-k+1, inf-2, inf))%inf
print(ans)

Buggy python submission (verdict: Runtime Error):
inf = pow(10, 9)+7

def nCr(n, r):
    r = min(r, n - r)
    if r == 0: return 1;
    if r == 1: return n;
    numerator = [n - r + i + 1 for i in range(r)]
    denominator  = [i + 1 for i in range(r)]
    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1: 
            result *= numerator[k]
    return result


n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
key = (nCr(n-1, k-1))
for i in range(n-k+1):
    ans += ((a[-i-1] - a[i])%inf)*(key%inf)
    ans %= inf
    key = key*(n-k-i)//(n-1-i)
print(int(ans))

Fixed submission:
inf = pow(10, 9)+7

n, k = map(int, input().split())
a = [int(x) for x in input().split()]
a.sort()

ans = 0
comb = 1
for i in range(k, n+1):
    ans += (a[i-1]-a[-i])*comb
    ans %= inf
    comb = (comb * i * pow(i-k+1, inf-2, inf))%inf
print(ans)