Problem p03253 (python)

Fixed submission:
from math import sqrt
N, M = map(int, input().split())

import sys
sys.setrecursionlimit(10**6)

MOD = 10**9 + 7
F = [M]
for x in range(2, int(sqrt(M))+1):
    if M % x == 0:
        F.append(x)
        if x != M // x:
            F.append(M // x)
F.sort(reverse=1)
L = len(F)
FF = [1]*(L+1)
fact = [1]*100
rfact = [1]*100
for i in range(L):
    FF[i+1] = FF[i] * (N - i) % MOD
for i in range(99):
    fact[i+1] = r = fact[i] * (i+1)
    rfact[i+1] = pow(r, MOD-2, MOD)
memo = {}
def dfs(i, rest, k):
    key = (i, rest, k)
    if i == L:
        if rest > 1:
            return 0
        return FF[k]
    if key in memo:
        return memo[key]
    f = F[i]
    r = dfs(i+1, rest, k)
    c = 0
    while rest % f == 0:
        rest //= f
        c += 1
        r = (r + dfs(i+1, rest, k+c) * rfact[c]) % MOD
    memo[key] = r
    return r
if M == 1:
    print(1)
else:
    print(dfs(0, M, 0))

Buggy submission (verdict: Runtime Error):
from math import sqrt
N, M = map(int, input().split())

import sys
sys.setrecursionlimit(10**6)

MOD = 10**9 + 7
F = [M]
for x in range(2, int(sqrt(M))+1):
    if M % x == 0:
        F.append(x)
        if x != M // x:
            F.append(M // x)
F.sort(reverse=1)
L = len(F)
FF = [1]*(L+1)
fact = [1]*100
rfact = [1]*100
for i in range(L):
    FF[i+1] = FF[i] * (N - i) % MOD
for i in range(99):
    fact[i+1] = r = fact[i] * (i+1)
    rfact[i+1] = pow(r, MOD-2, MOD)
memo = {}
def dfs(i, rest, k):
    key = (i, rest, k)
    if i == L:
        if rest > 1:
            return 0
        return FF[k]
    if key in memo:
        return memo[key]
    f = F[i]
    r = dfs(i+1, rest, k)
    c = 0
    while rest % f == 0:
        rest //= f
        c += 1
        r = (r + dfs(i+1, rest, k+c) * rfact[c]) % MOD
    memo[key] = r
    return r
print(dfs(0, M, 0))

Buggy submission (verdict: Runtime Error):
from math import sqrt
N, M = map(int, input().split())

import sys
sys.setrecursionlimit(10**6)

MOD = 10**9 + 7
F = [M]
for x in range(2, int(sqrt(M))+1):
    if M % x == 0:
        F.append(x)
        F.append(M // x)
F.sort(reverse=1)
L = len(F)
FF = [1]*(L+1)
fact = [1]*100
rfact = [1]*100
for i in range(L):
    FF[i+1] = FF[i] * (N - i) % MOD
for i in range(99):
    fact[i+1] = r = fact[i] * (i+1)
    rfact[i+1] = pow(r, MOD-2, MOD)
memo = {}
def dfs(i, rest, k):
    key = (i, rest, k)
    if i == L:
        if rest > 1:
            return 0
        return FF[k]
    if key in memo:
        return memo[key]
    f = F[i]
    r = dfs(i+1, rest, k)
    c = 0
    while rest % f == 0:
        rest //= f
        c += 1
        r = (r + dfs(i+1, rest, k+c) * rfact[c]) % MOD
    memo[key] = r
    return r
print(dfs(0, M, 0))

Buggy submission (verdict: Runtime Error):
from math import sqrt
N, M = map(int, input().split())

MOD = 10**9 + 7
F = [M]
for x in range(2, int(sqrt(M))+1):
    if M % x == 0:
        F.append(x)
        F.append(M // x)
F.sort(reverse=1)
L = len(F)
FF = [1]*(L+1)
fact = [1]*41
rfact = [1]*41
for i in range(L):
    FF[i+1] = FF[i] * (N - i) % MOD
for i in range(40):
    fact[i+1] = r = fact[i] * (i+1)
    rfact[i+1] = pow(r, MOD-2, MOD)
memo = {}
def dfs(i, rest, k):
    key = (i, rest, k)
    if i == L:
        if rest > 1:
            return 0
        return FF[k]
    if key in memo:
        return memo[key]
    f = F[i]
    r = dfs(i+1, rest, k)
    c = 0
    while rest % f == 0:
        rest //= f
        c += 1
        r = (r + dfs(i+1, rest, k+c) * rfact[c]) % MOD
    memo[key] = r
    return r
print(dfs(0, M, 0))