Buggy python submission (verdict: Time Limit Exceeded):
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

N = 1000000007

def combi(n,r,N):
    if n<r or n<0 or r<0:
        ans = 0
        return ans
    if n==r:
        ans = 1
        return ans
    r= min(r, n-r)
    ans = f[n]*rf[r]*rf[n-r]%N
    return ans

f = [0]*(10**5+1)
f[1]=1

for i in range(2,10**5+1):
    f[i] = f[i-1]*i

rf = [0]*(10**5+1)

for i in range(1,10**5+1):
    rf[i] = pow(f[i],N-2,N)

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1,N)
    max_sum+=a[i]*t
    max_sum%=N
    min_sum+=a[n-i-1]*t
    min_sum%=N
if max_sum-min_sum<0:
    max_sum+=N
print(max_sum-min_sum)


Fixed submission:
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

MOD = 1000000007

factorial = [1]
inverse = [1]
for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1)
    max_sum+=a[i]*t
    max_sum%=MOD
    min_sum+=a[n-i-1]*t
    min_sum%=MOD
if max_sum-min_sum<0:
    max_sum+=MOD
print(max_sum-min_sum)

Buggy python submission (verdict: Time Limit Exceeded):
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

N = 1000000007

def combi(n,r,N):
    if n<r or n<0 or r<0:
        ans = 0
        return ans
    if n==r:
        ans = 1
        return ans
    r= min(r, n-r)
    ans = f[n]*rf[r]*rf[n-r]%N
    return ans

f = [0]*(10**5+1)
f[1]=1

for i in range(2,10**5+1):
    f[i] = f[i-1]*i

rf = [0]*(10**5+1)

for i in range(1,10**5+1):
    rf[i] = pow(f[i],N-2,N)

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1,N)
    max_sum+=a[i]*t
    max_sum%=N
    min_sum+=a[n-i-1]*t
    min_sum%=N
if max_sum-min_sum<0:
    max_sum+=N
print(max_sum-min_sum)


Fixed submission:
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

MOD = 1000000007

factorial = [1]
inverse = [1]
for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1)
    max_sum+=a[i]*t
    max_sum%=MOD
    min_sum+=a[n-i-1]*t
    min_sum%=MOD
if max_sum-min_sum<0:
    max_sum+=MOD
print(max_sum-min_sum)

Buggy python submission (verdict: Time Limit Exceeded):
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

N = 1000000007

def combi(n,r,N):
    if n<r or n<0 or r<0:
        ans = 0
        return ans
    if n==r:
        ans = 1
        return ans
    r= min(r, n-r)
    ans = f[n]*rf[r]*rf[n-r]%N
    return ans

f = [0]*(10**5+1)
f[1]=1

for i in range(2,10**5+1):
    f[i] = f[i-1]*i

rf = [0]*(10**5+1)

for i in range(1,10**5+1):
    rf[i] = pow(f[i],N-2,N)

max_sum=0
min_sum=0

for i in range(k-1,n):
    max_sum+=a[i]*combi(i,k-1,N)
    max_sum%=N
for i in range(n-k+1):
    min_sum+=a[i]*combi(n-i-1,k-1,N)
    min_sum%=N
if max_sum-min_sum<0:
    max_sum+=N
print(max_sum-min_sum)


Fixed submission:
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

MOD = 1000000007

factorial = [1]
inverse = [1]
for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1)
    max_sum+=a[i]*t
    max_sum%=MOD
    min_sum+=a[n-i-1]*t
    min_sum%=MOD
if max_sum-min_sum<0:
    max_sum+=MOD
print(max_sum-min_sum)

Buggy python submission (verdict: Runtime Error):
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

N = 1000000007

def combi(n,r,N):
    if n<r or n<0 or r<0:
        ans = 0
        return ans
    if n==r:
        ans = 1
        return ans
    r= min(r, n-r)
    ans = f[n]*rf[r]*rf[n-r]%N
    return ans

f = [0]*(n+1)
f[1]=1

for i in range(2,10**5+1):
    f[i] = f[i-1]*i

rf = [0]*(n+1)

for i in range(1,10**5+1):
    rf[i] = pow(f[i],N-2,N)

max_sum=0
min_sum=0

for i in range(k-1,n):
    max_sum+=a[i]*combi(i,k-1,N)
    max_sum%=N
for i in range(n-k+1):
    min_sum+=a[i]*combi(n-i-1,k-1,N)
    min_sum%=N
if max_sum-min_sum<0:
    max_sum+=N
print(max_sum-min_sum)


Fixed submission:
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

MOD = 1000000007

factorial = [1]
inverse = [1]
for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1)
    max_sum+=a[i]*t
    max_sum%=MOD
    min_sum+=a[n-i-1]*t
    min_sum%=MOD
if max_sum-min_sum<0:
    max_sum+=MOD
print(max_sum-min_sum)

Buggy python submission (verdict: Time Limit Exceeded):
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

N = 1000000007

def combi(n,r,N):
    if n<r or n<0 or r<0:
        ans = 0
        return ans
    if n==r:
        ans = 1
        return ans
    r= min(r, n-r)
    ans = f[n]*rf[r]*rf[n-r]%N
    return ans

f = [0]*(n+1)
f[1]=1

for i in range(2,n+1):
    f[i] = f[i-1]*i

rf = [0]*(n+1)

for i in range(1,n+1):
    rf[i] = pow(f[i],N-2,N)

max_sum=0
min_sum=0

for i in range(k-1,n):
    max_sum+=a[i]*combi(i,k-1,N)
    max_sum%=N
for i in range(n-k+1):
    min_sum+=a[i]*combi(n-i-1,k-1,N)
    min_sum%=N
if max_sum-min_sum<0:
    max_sum+=N
print(max_sum-min_sum)


Fixed submission:
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

MOD = 1000000007

factorial = [1]
inverse = [1]
for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1)
    max_sum+=a[i]*t
    max_sum%=MOD
    min_sum+=a[n-i-1]*t
    min_sum%=MOD
if max_sum-min_sum<0:
    max_sum+=MOD
print(max_sum-min_sum)

Buggy python submission (verdict: Time Limit Exceeded):
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

N = 1000000007

def fac(n,r,N):
    ans=1
    for i in range(r):
        ans=ans*(n-i)%N
    return ans

def combi(n,r,N):
    if n<r or n<0 or r<0:
        ans = 0
        return ans
    if n==r:
        ans = 1
        return ans
    r= min(r, n-r)
    ans = f[n]*rf[r]*rf[n-r]%N
    return ans

f = [0]*(n+1)

for i in range(1,n+1):
    f[i] = fac(i,i,N)

rf = [0]*(n+1)

for i in range(1,n+1):
    rf[i] = pow(f[i],N-2,N)

max_sum=0
min_sum=0

for i in range(k-1,n):
    max_sum+=a[i]*combi(i,k-1,N)
    max_sum%=N
for i in range(n-k+1):
    min_sum+=a[i]*combi(n-i-1,k-1,N)
    min_sum%=N
if max_sum-min_sum<0:
    max_sum+=N
print(max_sum-min_sum)


Fixed submission:
n , k = map(int, input().split())
a = list(map(int,input().split()))
a.sort()

MOD = 1000000007

factorial = [1]
inverse = [1]
for i in range(1, n+2):
    factorial.append(factorial[-1] * i % MOD)
    inverse.append(pow(factorial[-1], MOD - 2, MOD))

def combi(n, r):
    if n < r or r < 0:
        return 0
    elif r == 0:
        return 1
    return factorial[n] * inverse[r] * inverse[n - r] % MOD

max_sum=0
min_sum=0

for i in range(k-1,n):
    t = combi(i,k-1)
    max_sum+=a[i]*t
    max_sum%=MOD
    min_sum+=a[n-i-1]*t
    min_sum%=MOD
if max_sum-min_sum<0:
    max_sum+=MOD
print(max_sum-min_sum)