Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
A=sorted(map(int,input().split()))

nCr={}
def cmb(n,r):
    if r==0 or r==n: return 1
    if r==1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)]=(cmb(n-1,r)+cmb(n-1,r-1))%(10**9+7)
    return nCr[(n,r)]

ans=0
for i in range(n-k+1):
    ans+=cmb(n-1-i,k-1)*(A[-i-1]-A[i])%(10**9+7)
    ans%=10**9+7
print(ans)

Fixed submission:
n,k=map(int,input().split())
A=sorted(map(int,input().split()))

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)

ans=0
for i in range(n-k+1):
    ans+=cmb(n-1-i,k-1,10**9+7)*(A[-i-1]-A[i])%(10**9+7)
    ans%=10**9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
n,k=map(int,input().split())
A=sorted(map(int,input().split()))

nCr={}
def cmb(n,r):
    if r==0 or r==n: return 1
    if r==1: return n
    if (n,r) in nCr: return nCr[(n,r)]
    nCr[(n,r)]=(cmb(n-1,r)+cmb(n-1,r-1))%(10**9+7)
    return nCr[(n,r)]

ans=0
for i in range(n-k+1):
    ans+=cmb(n-1-i,n-k-i)*(A[-i-1]-A[i])%(10**9+7)
    ans%=10**9+7
print(ans)

Fixed submission:
n,k=map(int,input().split())
A=sorted(map(int,input().split()))

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)

ans=0
for i in range(n-k+1):
    ans+=cmb(n-1-i,k-1,10**9+7)*(A[-i-1]-A[i])%(10**9+7)
    ans%=10**9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
A=sorted(map(int,input().split()))
C=[1]
for i in range(n-k):
    c=C[i]*(k+i)//(i+1)
    C.append(c)
ans=0
for i in range(n-k+1):
    ans+=C[-i-1]*(A[-i-1]-A[i])
ans%=1000000007
print(ans)

Fixed submission:
n,k=map(int,input().split())
A=sorted(map(int,input().split()))

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)

ans=0
for i in range(n-k+1):
    ans+=cmb(n-1-i,k-1,10**9+7)*(A[-i-1]-A[i])%(10**9+7)
    ans%=10**9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,k=map(int,input().split())
A=sorted(map(int,input().split()))
C=[1]
for i in range(n-k):
    c=C[i]*(k+i)//(i+1)
    C.append(c)
ans=0
for i in range(n-k+1):
    ans+=C[-i-1]*(A[-i-1]-A[i])
    ans%=1000000007
print(ans)

Fixed submission:
n,k=map(int,input().split())
A=sorted(map(int,input().split()))

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)

ans=0
for i in range(n-k+1):
    ans+=cmb(n-1-i,k-1,10**9+7)*(A[-i-1]-A[i])%(10**9+7)
    ans%=10**9+7
print(ans)