Problem p02990 (python)

Fixed submission:
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
MAX = 4000
g1 = [1, 1]
g2 = [1, 1]
inverse = [0, 1]
for i in range( 2, MAX + 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())
for i in range(1,k+1):
    pre = cmb(k-1,i-1,mod)
    pos = cmb(n-k+1,i,mod)
    print((pre*pos)%mod)

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
if K == 1:
    print(1)
else:
    for i in range(1,K+1):
        if i == 1 or i == K:
            print(comb(N-K+1,i,10**9+7))
        else:
            k = factorial(N-K+1) #% (10**9+7)
            l = factorial(N-K+1-i) #% (10**9+7)
            m = factorial(i-1) #% (10**9+7)
            #print(k,l,m)
            #print( (k//(l*m)))
            print( (k//(l*m)) % (10**9+7))
           #print(comb(N-K+1,i,10**9+7))

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
if K == 1:
    print(1)
else:
    for i in range(1,K+1):
        if i == 1 or i == K:
            print(comb(N-K+1,i,10**9+7))
        else:
            k = factorial(N-K+1) % (10**9+7)
            l = factorial(N-K+1-i) % (10**9+7)
            m = factorial(i-1) % (10**9+7)
            #print(k,l,m)
            print( (k//(l*m)))
            #print( (k//(l*m)))
            #print(comb(N-K+1,i,10**9+7))

Buggy submission (verdict: Runtime Error):
N,K = map(int,input().split())
if K == 1:
    print(1)
else:
    for i in range(1,K+1):
        if i == 1 or i == K:
            print(comb(N-K+1,i,10**9+7))
        else:
            k = factorial(N-K+1) % (10**9+7)
            l = factorial(N-K+1-i) % (10**9+7)
            m = factorial(i-1) % (10**9+7)
            #print(k,l,m)
            print( (k//(l*m)) % (10**9+7) )
            #print( (k//(l*m)))
            #print(comb(N-K+1,i,10**9+7))

Buggy submission (verdict: Runtime Error):
from math import factorial
def comb(n,k,p):
  #from math import factorial
  if n<0 or k<0 or n<k: return 0
  if n==0 or k==0: return 1
  a=factorial(n) %p
  b=factorial(k) %p
  c=factorial(n-k) %p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,n,p):
  bi=str(format(n,"b"))
  res=1
  for i in range(len(bi)):
    res=(res*res) %p
    if bi[i]=="1":
      res=(res*a) %p
  return res

N,K = map(int,input().split())
for i in range(1,K+1):
    if i == 1 or i == K:
        print(comb(N-K+1,i,10**9+7))
    else:
        k = factorial(N-K+1) #% 10**9+7
        l = factorial(N-K+1-i)
        m = factorial(i-1)
        #print(k,l,m)
        print( (k//(l*m)) % (10**9+7) )
        #print( (k//(l*m)))
        #print(comb(N-K+1,i,10**9+7))

Buggy submission (verdict: Runtime Error):
from math import factorial
def comb(n,k,p):
  #from math import factorial
  if n<0 or k<0 or n<k: return 0
  if n==0 or k==0: return 1
  a=factorial(n) %p
  b=factorial(k) %p
  c=factorial(n-k) %p
  return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,n,p):
  bi=str(format(n,"b"))#2進表現に
  res=1
  for i in range(len(bi)):
    res=(res*res) %p
    if bi[i]=="1":
      res=(res*a) %p
  return res
#print(comb(5,3,10**9+7))
N,K = map(int,input().split())
for i in range(1,K+1):
    if i == 1 or i == K:
        print(comb(N-K+1,i,10**9+7))
    else:
        k = factorial(N-K+1)
        l = factorial(N-K+1-i)
        m = factorial(i-1)
        #print(k,l,m)
        #print( (k//(l*m)) % 10**9+7 )
        print( (k//(l*m)))
        #print(comb(N-K+1,i,10**9+7))