Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n,k = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort()
  ans = 0
  if k == 1:
    print(0)
  else:
    for i in range(k-1,n):
      if n-i+1<0:
        break
      if i == k-1:
        c = 1
        ans += (a[i]-a[n-i-1])*c
      else:
        c = c*i//(i-k+1)
        ans += (a[i]-a[n-i-1])*c
    print(ans%1000000007)
if __name__ == "__main__":
  main()


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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n,k = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort()
  ans = 0
  if k == 1:
    print(0)
  else:
    for i in range(k-1,n):
      if n-i+1<0:
        break
      if i == k-1:
        c = 1
        ans += (a[i]-a[n-i-1])*c
      else:
        c = c*i//(i-k+1)
        ans += (a[i]-a[n-i-1])*c
    print(ans%1000000007)
if __name__ == "__main__":
  main()

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  n,k = map(int, input().split())
  a = list(map(int, input().split()))
  a.sort()
  ans = 0
  if k == 1:
    print(0)
  else:
    for i in range(k-1,n):
      if n-i+1<0:
        break
      if i == k-1:
        c = 1
        ans += (a[i]-a[n-i-1])*c
      else:
        c = c*i//(i-k+1)
        ans += (a[i]-a[n-i-1])*c
    print(ans%1000000007)
if __name__ == "__main__":
  main()

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

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

  c = 1
  nCk = [1]
  for r in range(k,n+1):
      c = c*r//(r-k+1)
      nCk.append(c)

  if k == 1:
    print(0)
  else:
    for i in range(1,n):
      if n-i < k-1:
        break
      ans += (a[n-i]-a[i-1])*nCk[n-i-k+1]
    print(ans%1000000007)
if __name__ == "__main__":
  main()


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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

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

  c = 1
  nCk = [1]
  for r in range(k,n+1):
      c_ = c*r//(r-k+1)
      c = c_
      nCk.append(c_)

  if k == 1:
    print(0)
  else:
    for i in range(1,n+1):
      if n-i < k-1:
        break
      ans += (a[n-i]-a[i-1])*nCk[n-i-k+1]
    print(ans%1000000007)
if __name__ == "__main__":
  main()

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(1000000000)

n,k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0

c = 1
nCk = [1]
for r in range(k,n+1):
    c_ = c*r//(r-k+1)
    c = c_
    nCk.append(c_)

if k == 1:
  print(0)
else:
  for i in range(1,n+1):
    if n-i < k-1:
      break
    ans += (a[n-i]-a[i-1])*nCk[n-i-k+1]
  print(ans%1000000007)

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**9)

n,k = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
ans = 0

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)
    return nCr[(n,r)]

if k == 1:
  print(0)
else:
  for i in range(1,n+1):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1)
    ans -= a[i-1]*cmb(n-i,k-1)
  print(ans%1000000007)

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

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

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)
    return nCr[(n,r)]

if k == 1:
  print(0)
else:
  for i in range(1,n+1):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1)
    ans -= a[i-1]*cmb(n-i,k-1)
  print(ans%1000000007)

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

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

from operator import mul
from functools import reduce

def cmb(n,r):
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1)
    ans -= a[i-1]*cmb(n-i,k-1)
  print(ans%1000000007)

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)

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

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)
    return nCr[(n,r)]

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1)
    ans -= a[i-1]*cmb(n-i,k-1)
print(ans%1000000007)

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

from operator import mul
from functools import reduce
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p

p = 10 ** 9 + 7
N = 10 ** 5
fact = [1, 1]  
factinv = [1, 1]
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)

if k == 1:
  print(0)
else:
  for i in range(1,n):
    if n-i < k-1:
      break
    ans += a[n-i]*cmb(n-i,k-1,p)
    ans -= a[i-1]*cmb(n-i,k-1,p)
  print(ans%1000000007)