Problem p02990 (python)

Fixed submission:
N, K = map(int, input().split())
#組み合わせ -------------------------------------------------
mod = int(1e9+7) #適宜変更
Max = 2001 #適宜変更
fac = [0]*(Max)
finv = [0]*(Max)
inv = [0]*(Max)
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 Comb(n,r):
    if n < r:
        return 0
    if n < 0 or r < 0 :
        return 0
    return fac[n]*(finv[r]*finv[n-r]%mod)%mod

for i in range(K):
    i += 1 #iは1からkまで
    B_sla = Comb(K-1, i-1)
    R = N-K-(i-1)
    if R == 0:
        R_ire = 1
    elif R < 0:
        R_ire = 0
    else:
        R_ire = Comb(R+i, R)

    print(int(B_sla*R_ire%mod))

Buggy submission (verdict: Runtime Error):
N, K = map(int, input().split())
L = N-K
M = 1e9+7
import math
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

def kai(x):
    z = 1
    for i in range(x):
        z *= x-i
        z = z%M
    return z

def comb(n,k,p):
  """power_funcを用いて(nCk) mod p を求める"""
  if n<0 or k<0 or n<k: return 0
  if n==0 or k==0: return 1
  a=kai(n) #factorial(n) %p
  b=kai(k) #factorial(k) %p
  c=kai(n-k) #factorial(n-k) %p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
  """a^b mod p を求める"""
  if b==0: return 1
  if b%2==0:
    d=power_func(a,b//2,p)
    return d*d %p
  if b%2==1:
    return (a*power_func(a,b-1,p ))%p
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

for i in range(K):
    i += 1 #iは1からkまで
    B_sla = cmb(K-1, i-1)
    R = L-(i-1)
    if R == 0:
        R_ire = 1
    elif R < 0:
        R_ire = 0
    else:
        R_ire = combinations_count(R+i, R)

    print(int(B_sla*R_ire%M))

Buggy submission (verdict: Runtime Error):
import math
N, K = map(int, input().split())
L = N-K
M = 1e9+7

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



for i in range(K):
    i += 1 #iは1からkまで
    B_sla = cmb(K-1, i-1)
    R = L-(i-1)
    if R == 0:
        R_ire = 1
    elif R < 0:
        R_ire = 0
    else:
        R_ire = cmb(R+i, R)

    print(int(B_sla*R_ire%M))

Buggy submission (verdict: Runtime Error):
import math
N, K = map(int, input().split())
L = N-K
M = 1e9+7

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

def kai(x, mod):
    a = 1
    for wawa in range(x):
        a *= x - wawa
        a = a%mod
    return  a

for i in range(K):
    i += 1 #iは1からkまで
    B_sla = cmb(K-1, i-1)
    R = L-(i-1)
    if R == 0:
        R_ire = 1
    elif R < 0:
        R_ire = 0
    else:
        R_ire = (math.factorial(R+i)/(math.factorial(R)*math.factorial(i))) % M

    print(int(B_sla*R_ire%M))