Buggy python submission (verdict: Time Limit Exceeded):
from math import factorial as jj
mod=10**9+7
n,k=map(int,input().split())
l=sorted(map(int,input().split()))
ans=0

ml=jj(n-1)//(jj(k-1)*jj(n-k))
ans-=ml*(l[0]-l[n-1])%mod
for i in range(1,n-k+1):
  ml=ml*(n-i-k+1)//(n-i)
  ans-=ml*(l[i]-l[n-1-i])%mod
print(ans%mod)


Fixed submission:
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
mod=10**9+7
fact=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2,n+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
for i in range(n):
    if i>=k-1:
        ans+=l[i]*fact[i]*inv[k-1]*inv[i-k+1]%mod
    if n-i-1>=k-1:
        ans-=l[i]*fact[n-i-1]*inv[k-1]*inv[n-i-1-k+1]%mod
    ans%=mod
print(ans)

Buggy python submission (verdict: Runtime Error):
form math import factorial as jj
mod=10**9+7
n,k=map(int,input().split())
l=sorted(map(int,input().split()))
ans=0

ml=jj(n-1)//(jj(k-1)*jj(n-k))
ans-=ml*(l[0]-l[n-1])%mod
for i in range(1,n-k+1):
  ml=ml*(n-i-k+1)//(n-i)
  ans-=ml*(l[i]-l[n-1-i])%mod
print(ans%mod)

Fixed submission:
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
mod=10**9+7
fact=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2,n+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
for i in range(n):
    if i>=k-1:
        ans+=l[i]*fact[i]*inv[k-1]*inv[i-k+1]%mod
    if n-i-1>=k-1:
        ans-=l[i]*fact[n-i-1]*inv[k-1]*inv[n-i-1-k+1]%mod
    ans%=mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
mod=10**9+7
n,k=map(int,input().split())
l=sorted(map(int,input().split()))
ll=list(map(lambda x:-1*x,l[::-1]))
ans=0
b,s=1,1
for i in range(k,n):
  s*=i
  b*=(i-k+1)
ml=s//b
ans-=ml*(l[0]+ll[0])
ans%=mod
for i in range(1,n-k+1):
  ml=ml*(n-i-k+1)//(n-i)
  ans-=ml*(l[i]+ll[i])%mod


print(ans%mod)


Fixed submission:
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
mod=10**9+7
fact=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2,n+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
for i in range(n):
    if i>=k-1:
        ans+=l[i]*fact[i]*inv[k-1]*inv[i-k+1]%mod
    if n-i-1>=k-1:
        ans-=l[i]*fact[n-i-1]*inv[k-1]*inv[n-i-1-k+1]%mod
    ans%=mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
mod=10**9+7
n,k=map(int,input().split())
l=sorted(map(int,input().split()))
ans=0
ll=1
ans+=ll*l[k-1]%mod
b,s=1,1
for i in range(k,n):
  ll=ll*i//(i-k+1)
  ans+=l[i]*ll%mod
  s*=i
  b*=(i-k+1)
ml=s//b

ans-=ml*l[0]
ans%=mod
for i in range(1,n-k+1):
  ml=ml*(n-i-k+1)//(n-i)
  ans-=ml*l[i]%mod

print(ans%mod)


Fixed submission:
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
mod=10**9+7
fact=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2,n+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
for i in range(n):
    if i>=k-1:
        ans+=l[i]*fact[i]*inv[k-1]*inv[i-k+1]%mod
    if n-i-1>=k-1:
        ans-=l[i]*fact[n-i-1]*inv[k-1]*inv[n-i-1-k+1]%mod
    ans%=mod
print(ans)

Buggy python submission (verdict: Runtime Error):
mod=10**9+7
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
ans=0

ll=1
ans+=ll*l[k-1]%mod
ml=1
for i in range(k,n):
  ll=ll*i//(i-k+1)
  ans+=l[i]*ll%mod
  ml*=(i/(i-k+1))
ml//=1

ans-=ml*l[0]
ans%=mod
for i in range(1,n-k+1):
  ml=ml*(n-i-k+1)//(n-i)
  ans-=ml*l[i]%mod

print(int(ans%mod))


Fixed submission:
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
mod=10**9+7
fact=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2,n+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
for i in range(n):
    if i>=k-1:
        ans+=l[i]*fact[i]*inv[k-1]*inv[i-k+1]%mod
    if n-i-1>=k-1:
        ans-=l[i]*fact[n-i-1]*inv[k-1]*inv[n-i-1-k+1]%mod
    ans%=mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import math 
mod=10**9+7
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
ans=0

ll=1
ans+=ll*l[k-1]%mod
for i in range(k,n):
  ll=ll*i//(i-k+1)
  ans+=l[i]*ll%mod

j=math.factorial(k-1)
ml=math.factorial(n-1)//(math.factorial(n-k)*j)
ans-=ml*l[0]
ans%=mod
for i in range(1,n-k+1):
  ml=ml*(n-i-k+1)//(n-i)
  ans-=ml*l[i]
  ans%=mod

print(ans%mod)


Fixed submission:
n,k=map(int,input().split())
l=sorted(list(map(int,input().split())))
mod=10**9+7
fact=[1]*(n+1)
inv=[1]*(n+1)
for i in range(2,n+1):
    fact[i]=i*fact[i-1]%mod
inv[-1]=pow(fact[-1],mod-2,mod)
for i in range(n,1,-1):
    inv[i-1]=inv[i]*i%mod
ans=0
for i in range(n):
    if i>=k-1:
        ans+=l[i]*fact[i]*inv[k-1]*inv[i-k+1]%mod
    if n-i-1>=k-1:
        ans-=l[i]*fact[n-i-1]*inv[k-1]*inv[n-i-1-k+1]%mod
    ans%=mod
print(ans)