Problem p02990 (python)

Fixed submission:
def Myne(N,K):
    Input1 = [str(input()) for i in range(1)]
    N,K = list(map(int,Input1[0].split()))
    ANS = []
    if N == K:
        ANS.append(1)
        print(1)
        for i in range(K-1):
            ANS.append(0)
            print(0)
    else:
        M = max(K,N-K)
        Mem = [[1 for j in range(i+1)] for i in range(M)]
        for j in range(M):
            I = len(Mem[j])
            for i in range(I):
                if j>0 and i!=0 and i!= I-1:
                    Mem[j][i] = int((Mem[j-1][i-1]+Mem[j-1][i])%(10**9+7))

        i = 0
        ANS.append(N-K+1)
        print(N-K+1)
        for i in range(1,K):
            if N-K-1 >= i+1:
                ans = Mem[K-1][i]*(Mem[N-K-1][i-1]+2*Mem[N-K-1][i]+Mem[N-K-1][i+1])
                ans = int(ans % (10**9+7))
                ANS.append(ans)
                print(ans)                
            elif N-K-1 >= i:
                ans = Mem[K-1][i]*(Mem[N-K-1][i-1]+2*Mem[N-K-1][i])
                ans = int(ans % (10**9+7))
                ANS.append(ans)
                print(ans)
            elif N-K-1 == i-1:
                ans = Mem[K-1][i]*Mem[N-K-1][i-1]
                ans = int(ans % (10**9+7))
                ANS.append(ans)
                print(ans)
            else:
                ANS.append(0)
                print(0)
    return ANS

if __name__ == '__main__':
    Myne(1,1)

Buggy submission (verdict: Runtime Error):
    Input1 = [str(input()) for i in range(1)]
    N,K = list(map(int,Input1[0].split()))
    ANS = []
    if N == K:
        ANS.append(1)
        print(1)
        for i in range(K-1):
            ANS.append(0)
            print(0)
    else:
        M = max(K,N-K)
        Mem = [[1 for j in range(i+1)] for i in range(M)]
        for j in range(M):
            I = len(Mem[j])
            for i in range(I):
                if j>0 and i!=0 and i!= I-1:
                    Mem[j][i] = int((Mem[j-1][i-1]+Mem[j-1][i])%(10**9+7))

        i = 0
        ANS.append(N-K+1)
        print(N-K+1)
        for i in range(1,K):
            if N-K-1 >= i+1:
                ans = Mem[K-1][i]*(Mem[N-K-1][i-1]+2*Mem[N-K-1][i]+Mem[N-K-1][i+1])
                ans = int(ans % (10**9+7))
                ANS.append(ans)
                print(ans)                
            elif N-K-1 >= i:
                ans = Mem[K-1][i]*(Mem[N-K-1][i-1]+2*Mem[N-K-1][i])
                ans = int(ans % (10**9+7))
                ANS.append(ans)
                print(ans)
            elif N-K-1 == i-1:
                ans = Mem[K-1][i]*Mem[N-K-1][i-1]
                ans = int(ans % (10**9+7))
                ANS.append(ans)
                print(ans)
            else:
                ANS.append(0)
                print(0)

Buggy submission (verdict: Runtime Error):
#import numpy as np
#from math import gcd

    
##################################################################

if __name__ == "__main__":
    Input1 = [input() for i in range(1)]
    N,K = list(map(int,Input1[0].split()))
    if N == K:
        print(1)
        for i in range(K-1):
            print(0)
    else:
        M = max(K,N-K)
        Mem = [[1 for j in range(i+1)] for i in range(M)]
        for j in range(M):
            I = len(Mem[j])
            for i in range(I):
                if j>0 and i!=0 and i!= I-1:
                    Mem[j][i] = Mem[j-1][i-1]+Mem[j-1][i]

        i = 0
        print(N-K+1)
        for i in range(1,K):
            if N-K-1 >= i+1:
                ans = Mem[K-1][i]*(Mem[N-K-1][i-1]+2*Mem[N-K-1][i]+Mem[N-K-1][i+1])
                ans = int(ans % (1E9+7))
                print(ans)                
            elif N-K-1 >= i:
                ans = Mem[K-1][i]*(Mem[N-K-1][i-1]+2*Mem[N-K-1][i])
                ans = int(ans % (1E9+7))
                print(ans)
            elif N-K-1 == i-1:
                ans = Mem[K-1][i]*Mem[N-K-1][i-1]
                ans = int(ans % (1E9+7))
                print(ans)
            else:
                print(0)