Buggy python submission (verdict: Runtime Error):
from math import factorial
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**4
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 )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  l = len(a)
  for i in range(l):
    s += a[i] * cmb(i, k - 1, mod)
    s -= a[i] * cmb(l - i -1 , k - 1, mod)

    s %= mod
  print(s)
main()

Fixed submission:
from math import factorial
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 )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  l = len(a)
  for i in range(l):
    s += a[i] * cmb(i, k - 1, mod)
    s -= a[i] * cmb(l - i -1 , k - 1, mod)

    s %= mod
  print(s)
main()

Buggy python submission (verdict: Runtime Error):
from math import factorial
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**4
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 )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  if k == 1:
    print(0)
    return
  for i in range(len(a)):
    for j in range(k - 1, len(a) - i):
      s += (a[i+j] - a[i]) * cmb(j-1, k-2, mod) % mod
      s %= mod
  print(s)
main()

Fixed submission:
from math import factorial
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 )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  l = len(a)
  for i in range(l):
    s += a[i] * cmb(i, k - 1, mod)
    s -= a[i] * cmb(l - i -1 , k - 1, mod)

    s %= mod
  print(s)
main()

Buggy python submission (verdict: Runtime Error):
from math import factorial
def comb(n, r):
    return int(factorial(n) / factorial(r) / factorial(n - r))

n, k = map(int, input().split())
a = sorted(map(int, input().split()))

s = 0
d = 10**9+7
for i in range(len(a)):
  for j in range(k - 1, len(a) - i):
    s += (a[i+j] - a[i]) * comb(j-1, k-2) % d
    s %= d
print(s)

Fixed submission:
from math import factorial
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 )

n, k = map(int, input().split())
a = sorted(map(int, input().split()))


mod = 10**9+7
def main():
  s = 0
  l = len(a)
  for i in range(l):
    s += a[i] * cmb(i, k - 1, mod)
    s -= a[i] * cmb(l - i -1 , k - 1, mod)

    s %= mod
  print(s)
main()