Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(1000000)
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())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
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())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
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())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
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())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
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())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
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
n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)
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)]
n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
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)]
n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result
n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1)
    ans %= 10 ** 9+7
print(ans)

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
# import sys
# sys.setrecursionlimit(100000)
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)]


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
li = []
l = [1]
num = 1
cnt = 1
for i in range(n-1):
  if lis[i] == lis[i+1]:
    cnt += 1
    l.append(num)
  else:
    num = i + 2
    l.append(num)
    for _ in range(cnt):
      li.append(i+1)
    cnt = 1
for _ in range(cnt):
  li.append(n)

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

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)

Buggy python submission (verdict: Runtime Error):
# import sys
# sys.setrecursionlimit(100000)
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)]


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
li = []
l = [1]
num = 1
cnt = 1
for i in range(n-1):
  if lis[i] == lis[i+1]:
    cnt += 1
    l.append(num)
  else:
    num = i + 2
    l.append(num)
    for _ in range(cnt):
      li.append(i+1)
    cnt = 1
for _ in range(cnt):
  li.append(n)

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

Fixed submission:
import sys
sys.setrecursionlimit(1000000)
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 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル


n,k = map(int,input().split())
lis = list(map(int,input().split()))
lis.sort()
ans = 0
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 )
for i in range(n):
    if i >= k-1:
        ans += lis[i] * cmb(i,k-1,mod)
    if n-i-1 >= k-1:
        ans -= lis[i] * cmb(n-i-1,k-1,mod)
    ans %= 10 ** 9+7
print(ans)