Problem p02990 (python)

Fixed submission:
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

MOD = (10 ** 9) + 7

INF = 10 ** 10

MAX = 510000

def com_init():
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, MAX):
        fac[i] = fac[i - 1] * i % MOD
        inv[i] = MOD - inv[MOD%i] * (MOD // i) % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD


def com(n, k):
    if n < k:
        return 0
    if (n < 0) or (k < 0):
        return 0
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD


if __name__ == "__main__":
    # K個の青ボールとN-K個の赤ボール
    # 同じ色のボールは区別可能
    N, K = [int(x) for x in stdin.readline().rstrip().split()]
    
    fac = [0] * MAX
    finv = [0] * MAX
    inv = [0] * MAX

    com_init()

    for i in range(1, K+1):
        ans = com(N-K+1, i) * com(K-1, i-1) % MOD
        print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

MODN = (10 ** 9) + 7

INF = 10 ** 10

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


# K個の青ボールとN-K個の赤ボール
# 同じ色のボールは区別可能
N, K = [int(x) for x in stdin.readline().rstrip().split()]

blue_num = K
red_num = N - K

#red_p = permutations_count(red_num, 1)
pos_cand_num = red_num + 1

for i in range(1, K+1):
    ans = combinations_count(pos_cand_num, i) % MODN * combinations_count(K-1, i-1) % MODN
    print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

MODN = (10 ** 9) + 7

INF = 10 ** 10

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))


# K個の青ボールとN-K個の赤ボール
# 同じ色のボールは区別可能
N, K = [int(x) for x in stdin.readline().rstrip().split()]

blue_num = K
red_num = N - K

red_p = permutations_count(red_num, 1)
pos_cand_num = red_p + 1

for i in range(1, K+1):
    ans = combinations_count(pos_cand_num, i) % MODN * combinations_count(K-1, i-1) % MODN
    print(ans)

Buggy submission (verdict: Runtime Error):
from sys import stdin
import math
import fractions
from collections import deque
from collections import Counter
import itertools

MODN = (10 ** 9) + 7

def permutations_count(n, r):
    return (math.factorial(n) // math.factorial(n - r))

def combinations_count(n, r):
    return (math.factorial(n) // (math.factorial(n - r) * math.factorial(r)))


INF = 10 ** 10


# K個の青ボールとN-K個の赤ボール
# 同じ色のボールは区別可能
N, K = [int(x) for x in stdin.readline().rstrip().split()]
#num = [i for i in range(N)]

blue_num = K
red_num = N - K

# 1<=i<=K を満たすiそれぞれについて答えを計算し、MODNで割った余りを答える

for i in range(1, K+1):
    if i == 1:
        ans = (red_num+1) % MODN
        print(ans)
    else:
        cand_space = K - 1
        first_set_red_num = i - 1
        if cand_space >= first_set_red_num:
            c_num = combinations_count(cand_space, first_set_red_num) % MODN
            remind_red_num = red_num - first_set_red_num
            cand_space2 = first_set_red_num + 2

            if cand_space2 > remind_red_num:
                c_num2 = ((cand_space2 ** remind_red_num) // math.factorial(remind_red_num)) % MODN
                ans = c_num * c_num2
                ans = ans % MODN
            else:
                #if remind_red_num < cand_space2:
                #ans = c_num * ((first_set_red_num+2) ** remind_red_num)
                c_num2 = combinations_count(remind_red_num+1, first_set_red_num+2-1) % MODN
                ans = c_num * c_num2
                ans = ans % MODN
            print(ans)