Buggy python submission (verdict: Time Limit Exceeded):
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
def comb(p):
    if p >= k-1:
        a = math.factorial(p)%mod
        c = math.factorial(p-k+1)%mod
        return (a*pow(b*c, mod-2, mod))%mod
    else:
        return 0
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += comb(i)*(A[i]-A[n-i-1])
    ans %= mod
print(ans)


Fixed submission:
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
fact = [0]*(n-k+1)
a = math.factorial(k-1)%mod
c = 1
for i in range(k-1, n):
    if i == k-1:
        fact[0] = (a*pow(b*c, mod-2, mod))%mod
    else:
        a = a*i%mod
        c = c*(i-k+1)%mod
        fact[i-k+1] = (a*pow(b*c, mod-2, mod))%mod
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += fact[i-k+1]*(A[i]-A[n-i-1])
    ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
def comb(p):
    if p >= k-1:
        a = math.factorial(p)%mod
        c = math.factorial(p-k+1)%mod
        return (a*pow(b*c, mod-2, mod))%mod
    else:
        return 0
A = list(map(int,input().split()))
A.sort()
maxlist = []
ans = 0
for i in range(k-1,n):
    maxlist.append(comb(i))
for i in range(k-1,n):
    ans += (A[i]*maxlist[i-k+1])%mod
    ans %= mod
cur = len(maxlist)-1
for i in range(k-1,n):
    ans += mod-((A[i-k+1]*maxlist[cur])%mod)
    ans %= mod
    cur -= 1
print(ans)


Fixed submission:
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
fact = [0]*(n-k+1)
a = math.factorial(k-1)%mod
c = 1
for i in range(k-1, n):
    if i == k-1:
        fact[0] = (a*pow(b*c, mod-2, mod))%mod
    else:
        a = a*i%mod
        c = c*(i-k+1)%mod
        fact[i-k+1] = (a*pow(b*c, mod-2, mod))%mod
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += fact[i-k+1]*(A[i]-A[n-i-1])
    ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
def comb(p):
    if p >= k-1:
        a = math.factorial(p)%mod
        c = math.factorial(p-k+1)%mod
        return (a*pow(b*c, mod-2, mod))%mod
    else:
        return 0
A = list(map(int,input().split()))
A.sort()
maxlist = []
ans = 0
for i in range(k-1,n):
    maxlist.append(comb(i))
for i in range(k-1,n):
    ans += (A[i]*maxlist[i-k+1])%mod
    ans %= mod
cur = len(maxlist)-1
for i in range(k-1,n):
    ans += mod-((A[i-k+1]*maxlist[cur])%mod)
    ans %= mod
    cur -= 1
print(ans)


Fixed submission:
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
fact = [0]*(n-k+1)
a = math.factorial(k-1)%mod
c = 1
for i in range(k-1, n):
    if i == k-1:
        fact[0] = (a*pow(b*c, mod-2, mod))%mod
    else:
        a = a*i%mod
        c = c*(i-k+1)%mod
        fact[i-k+1] = (a*pow(b*c, mod-2, mod))%mod
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += fact[i-k+1]*(A[i]-A[n-i-1])
    ans %= mod
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
mod = 10**9+7
def comb(p,q):
    a = math.factorial(p)%mod
    b = math.factorial(q)%mod
    c = math.factorial(p-q)%mod
    return (a*pow(b*c, mod-2, mod))%mod
n,k = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
maxlist = []
ans = 0
for i in range(n-k,n+1):
    maxlist.append(comb(i-1,k-1))
for i in range(n-k,n+1):
    ans += (maxlist[i-n+k]*A[i-1])%mod
    ans %= mod
for i in range(n-k+1):
    ans += mod-((A[i]*maxlist[k-i])%mod)
    ans %= mod
print(ans)


Fixed submission:
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
fact = [0]*(n-k+1)
a = math.factorial(k-1)%mod
c = 1
for i in range(k-1, n):
    if i == k-1:
        fact[0] = (a*pow(b*c, mod-2, mod))%mod
    else:
        a = a*i%mod
        c = c*(i-k+1)%mod
        fact[i-k+1] = (a*pow(b*c, mod-2, mod))%mod
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += fact[i-k+1]*(A[i]-A[n-i-1])
    ans %= mod
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
mod = 10**9+7
def comb(p,q):
    a = math.factorial(p)%mod
    b = math.factorial(q)%mod
    c = math.factorial(p-q)%mod
    return (a*pow(b*c, mod-2, mod))%mod
n,k = map(int,input().split())
A = list(map(int,input().split()))
A.sort()
maxlist = []
ans = 0
for i in range(n-k,n+1):
    maxlist.append(comb(i-1,k-1))
for i in range(n-k,n+1):
    ans += (maxlist[i-n+k]*A[i-1])%mod
    ans %= mod
for i in range(n-k+1):
    ans += mod-((A[i]*maxlist[k-i])%mod)
    ans %= mod
print(ans)


Fixed submission:
import math
mod = 10**9+7
n,k = map(int,input().split())
b = math.factorial(k-1)%mod
fact = [0]*(n-k+1)
a = math.factorial(k-1)%mod
c = 1
for i in range(k-1, n):
    if i == k-1:
        fact[0] = (a*pow(b*c, mod-2, mod))%mod
    else:
        a = a*i%mod
        c = c*(i-k+1)%mod
        fact[i-k+1] = (a*pow(b*c, mod-2, mod))%mod
A = list(map(int,input().split()))
A.sort()
ans = 0
for i in range(k-1,n):
    ans += fact[i-k+1]*(A[i]-A[n-i-1])
    ans %= mod
print(ans)