Buggy python submission (verdict: Time Limit Exceeded):
def main(): 
  def modinv(a, mod=10**9+7):
      return pow(a, mod-2, mod)

  def comb(n, r, mod=10**9+7):
      r = min(r, n-r)
      res = 1
      for i in range(r):
          res = res * (n - i) * modinv(i+1, mod) % mod
      return res

  import sys
  input = sys.stdin.readline
  mod = 10**9+7  
  n,k = [int(i) for i in input().split()]
  a = [int(i) for i in input().split()]
  a = sorted(a)
  a = tuple(a)

  ans = 0
  for i in range(0,n-k+1):
    p = a[i]*comb(n-1-i,k-1,mod)
    ans -= (p%mod)
    ans %= mod
  for i in range(k-1,n):
    q = a[i]*comb(i,k-1,mod)
    ans += (q%mod)
    ans %= mod
  print(ans)
if __name__ == '__main__':
    main()

Fixed submission:
def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [1,1]
    inv = [0,1]
    for i in range(2, N+1):
        fact.append((fact[-1]*i) % mod)
        # モジュラ逆数の性質
        inv.append((-inv[mod%i] * (mod//i)) % mod)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
fact, fact_inv = make_array_for_comb(n,mod)
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(0,n-k+1):
  p = a[i]*comb(n-1-i,k-1,mod)
  ans -= (p%mod)
  ans %= mod
for i in range(k-1,n):
  q = a[i]*comb(i,k-1,mod)
  ans += (q%mod)
  ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
  
def comb(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(n):
  p = a[i]*comb(n-1-i,k-1,mod)
  q = a[i]*comb(i,k-1,mod)
  if n-i < k:
    p = 0
  if i < k-1:
    q = 0
  #print(p,q)
  ans += (q%mod)
  ans -= (p%mod)
  ans %= mod
print(ans)

Fixed submission:
def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [1,1]
    inv = [0,1]
    for i in range(2, N+1):
        fact.append((fact[-1]*i) % mod)
        # モジュラ逆数の性質
        inv.append((-inv[mod%i] * (mod//i)) % mod)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
fact, fact_inv = make_array_for_comb(n,mod)
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(0,n-k+1):
  p = a[i]*comb(n-1-i,k-1,mod)
  ans -= (p%mod)
  ans %= mod
for i in range(k-1,n):
  q = a[i]*comb(i,k-1,mod)
  ans += (q%mod)
  ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
  
def comb(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(n):
  p = a[i]*comb(n-1-i,k-1,mod)
  q = a[i]*comb(i,k-1,mod)
  if n-i < k:
    p = 0
  if i < k-1:
    q = 0
  #print(p,q)
  ans += q
  ans -= p
  ans %= mod
print(ans)

Fixed submission:
def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [1,1]
    inv = [0,1]
    for i in range(2, N+1):
        fact.append((fact[-1]*i) % mod)
        # モジュラ逆数の性質
        inv.append((-inv[mod%i] * (mod//i)) % mod)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
fact, fact_inv = make_array_for_comb(n,mod)
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(0,n-k+1):
  p = a[i]*comb(n-1-i,k-1,mod)
  ans -= (p%mod)
  ans %= mod
for i in range(k-1,n):
  q = a[i]*comb(i,k-1,mod)
  ans += (q%mod)
  ans %= mod
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)
  
def comb(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res
  
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(n):
  p = a[i]*comb(n-1-i,k-1,mod)
  q = a[i]*comb(i,k-1,mod)
  if n-i < k:
    p = 0
  if i < k-1:
    q = 0
  #print(p,q)
  ans += q
  ans -= p
  ans %= mod
print(ans)

Fixed submission:
def make_array_for_comb(N, mod=10**9+7):
    fact = [1,1]
    fact_inv = [1,1]
    inv = [0,1]
    for i in range(2, N+1):
        fact.append((fact[-1]*i) % mod)
        # モジュラ逆数の性質
        inv.append((-inv[mod%i] * (mod//i)) % mod)
        fact_inv.append((fact_inv[-1]*inv[i]) % mod)
    return fact, fact_inv

def comb(n, r, mod=10**9+7):
  if (r < 0) or (n < r):
      return 0
  r = min(r, n - r)
  return fact[n] * fact_inv[r] * fact_inv[n-r] % mod

import sys
input = sys.stdin.readline
mod = 10**9+7  
n,k = [int(i) for i in input().split()]
fact, fact_inv = make_array_for_comb(n,mod)
a = [int(i) for i in input().split()]
a = sorted(a)
a = tuple(a)

ans = 0
for i in range(0,n-k+1):
  p = a[i]*comb(n-1-i,k-1,mod)
  ans -= (p%mod)
  ans %= mod
for i in range(k-1,n):
  q = a[i]*comb(i,k-1,mod)
  ans += (q%mod)
  ans %= mod
print(ans)