Problem p02804 (python)

Fixed submission:
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
n_max=2*10**5
F,FI=[0]*(n_max+1),[0]*(n_max+1)
F[0],FI[0]=1,1
for i in range(n_max):
  F[i+1]=(F[i]*(i+1))%mod
  FI[i+1]=pow(F[i+1],mod-2,mod)
def comb(x,y):
  return (F[x]*FI[x-y]*FI[y])%mod
total=0
for i in range(n-k+1):
   f=comb(n-i-1,k-1)%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
total=0
from math import factorial
for i in range(n-k+1):
   f=int((factorial(n-i-1)/factorial(k-1)/factorial(n-k-i)))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
total=0
from math import factorial
for i in range(n-k+1):
   f=int((factorial(n-i-1)/factorial(k-1)/factorial(n-k-i)))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
if k==1:
    print("0")
    exit()
total=0
from math import factorial
for i in range(n-k+1):
   f=int((factorial(n-i-1)/factorial(k-1)/factorial(n-k-i)))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
if k==1:
    print("0")
if n==2:
    print((l[1]-l[0])%mod)
total=0
from math import factorial
for i in range(n-k+1):
   f=int((factorial(n-i-1)/factorial(k-1)/factorial(n-k-i)))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
total=0
from math import factorial
for i in range(n-k+1):
   f=int((factorial(n-i-1)/factorial(k-1)/factorial(n-k-i)))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
total=0
from math import factorial
for i in range(n-k+1):
   f=int((factorial(n-i-1)/factorial(k-1)/factorial(n-k-i)))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10**9+7
total=0
from math import factorial
for i in range(n-k+1):
   f=int((factorial(n-i-1)/factorial(k-1)/factorial(n-k-i)))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10*9+7
total=0
from math import factorial
for i in range(n-k):
   f=(factorial(n-i-1)/factorial(k)/factorial(n-k-i-1))%mod
   total+=int((l[n-i-1]-l[i])*f)
   total%=mod
print(total)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10*9+7
min=0
max=0
from math import factorial
for i in range(n-k):
   min+=(l[i]*(factorial(n-i-1)/factorial(k)/factorial(n-k-i-1)))%mod
   max+=(l[n-i-1]*(factorial(n-i-1)/factorial(k)/factorial(n-k-i-1)))%mod
   max=max%mod
   min=min%mod
print(int((max-min))%mod)

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
l=list(map(int,input().split()))
l.sort()
mod=10*9+7
min=0
max=0
from math import factorial
for i in range(n-k):
   min+=(l[i]*(factorial(n-i-1)/factorial(k)/factorial(n-k-i-1)))%mod
   max+=(l[n-i]*(factorial(n-i-1)/factorial(k)/factorial(n-k-i-1)))%mod
   max=max%mod
   min=min%mod
print((max-min)%mod)