Problem p02804 (python)

Fixed submission:
from collections import deque
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
mod = (10**9)+7
for h in range(N):
    B.append(A[h])
    C.append(A[h])


def cmb(n, r):  # 【Python】組み合わせ(nCr) 計算の高速化より引用
    if (r < 0 or r > n):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod


N_cmb = 10**5
g1 = [1, 1]  # 元テーブル
g2 = [1, 1]  # 逆元テーブル
inverse = [0, 1]  # 逆元テーブル計算用テーブル

for ZZZ in range(2, N_cmb + 1):
    g1.append((g1[-1] * ZZZ) % mod)
    inverse.append((-inverse[mod % ZZZ] * (mod//ZZZ)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)


ans = 0
for i in range(N):
    if N - i >= K:
        kari = cmb(N - i - 1, K-1) % mod
        # print(kari)
        ans += (kari * B.pop()) % mod
        ans -= (kari * C.popleft()) % mod
        ans = ans % mod
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
mod = (10**9)+7
for h in range(N):
    B.append(A[h])
    C.append(A[h])


def cmb(n, r):  # 【Python】組み合わせ(nCr) 計算の高速化より引用
    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_cmb = 10**4
g1 = [1, 1]  # 元テーブル
g2 = [1, 1]  # 逆元テーブル
inverse = [0, 1]  # 逆元テーブル計算用テーブル

for ZZZ in range(2, N_cmb + 1):
    g1.append((g1[-1] * ZZZ) % mod)
    inverse.append((-inverse[mod % ZZZ] * (mod//ZZZ)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)


ans = 0
for i in range(N):
    if N - i >= K:
        kari = cmb((N - i - 1), K-1) % mod
        # print(kari)
        ans += (kari * B.pop()) % mod
        ans -= (kari * C.popleft()) % mod
        ans = ans % mod
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
mod = (10**9)+7
for h in range(N):
    B.append(A[h])
    C.append(A[h])


def cmb(n, r):  # 【Python】組み合わせ(nCr) 計算の高速化より引用
    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_cmb = 10**4
g1 = [1, 1]  # 元テーブル
g2 = [1, 1]  # 逆元テーブル
inverse = [0, 1]  # 逆元テーブル計算用テーブル

for i in range(2, N_cmb + 1):
    g1.append((g1[-1] * i) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)


ans = 0
for i in range(N):
    if N - i >= K:
        kari = cmb((N - i - 1), K-1) % mod
        # print(kari)
        ans += (kari * B.pop()) % mod
        ans -= (kari * C.popleft()) % mod
        ans = ans % mod
print(ans)

Buggy submission (verdict: Runtime Error):
from collections import deque
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
for h in range(N):
    B.append(A[h])
    C.append(A[h])


def cmb(n, r):  # 【Python】組み合わせ(nCr) 計算の高速化より引用
    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_cmb = 10**4
g1 = [1, 1]  # 元テーブル
g2 = [1, 1]  # 逆元テーブル
inverse = [0, 1]  # 逆元テーブル計算用テーブル

for i in range(2, N_cmb + 1):
    g1.append((g1[-1] * i) % mod)
    inverse.append((-inverse[mod % i] * (mod//i)) % mod)
    g2.append((g2[-1] * inverse[-1]) % mod)

mod = (10**9)+7
ans = 0
for i in range(N):
    if N - i >= K:
        kari = cmb((N - i - 1), K-1) % mod
        # print(kari)
        ans += (kari * B.pop()) % mod
        ans -= (kari * C.popleft()) % mod
        ans = ans % mod
print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb  # AtCoderに出すときはspecialをmiscに変える
from collections import deque
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
for h in range(N):
    B.append(A[h])
    C.append(A[h])
mod = (10**9)+7
minlist = [0] * N
maxlist = [0] * N
ans = 0

kari = comb((N - 1), K-1, exact=True)
# print(kari)
karimod = kari % mod
ans += (karimod * B.pop()) % mod
ans -= (karimod * C.popleft()) % mod
ans = ans % mod

for i in range(1, N):
    if N - i >= K:
        if N - i == 1:
            kari = 1
        else:
            kari //= N - i
            kari *= N - i - K+1
        karimod = kari % mod
        # print(kari)
        ans += (karimod * B.pop()) % mod
        ans -= (karimod * C.popleft()) % mod
        ans = ans % mod
print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb  # AtCoderに出すときはspecialをmiscに変える
from collections import deque
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A = sorted(A)
B = deque()
C = deque()
for h in range(N):
    B.append(A[h])
    C.append(A[h])
mod = (10**9)+7
minlist = [0] * N
maxlist = [0] * N
ans = 0

kari = comb((N - 1), K-1, exact=True)
# print(kari)
karimod = kari % mod
ans += (karimod * B.pop()) % mod
ans -= (karimod * C.popleft()) % mod
ans = ans % mod

for i in range(1, N):
    if N - i >= K:
        if N - i == 1:
            kari = 1
        else:
            kari //= N - i
            kari *= N - i - K+1
        karimod = kari % mod
        # print(kari)
        ans += (karimod * B.pop()) % mod
        ans -= (karimod * C.popleft()) % mod
        ans = ans % mod
print(ans)

Buggy submission (verdict: Runtime Error):
from scipy.special import comb  # AtCoderに出すときはspecialをmiscに変える
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
A = sorted(A)
mod = (10**9)+7
minlist = [0] * N
maxlist = [0] * N
ans = 0

kari = comb((N - 1), K-1, exact=True)
# print(kari)
karimod = kari % mod
ans += (karimod * A[N-1]) % mod
ans -= (karimod * A[0]) % mod
ans = ans % mod

for i in range(1, N):
    if N - i >= K:
        if N - i == 1:
            kari = 1
        else:
            kari //= N - i
            kari *= N - i - K+1
        karimod = kari % mod
        # print(kari)
        ans += (karimod * A[N-i-1]) % mod
        ans -= (karimod * A[i]) % mod
        ans = ans % mod
print(ans)