Problem p03253 (python)

Fixed submission:
from collections import defaultdict
def factorization(n):
    arr = {}
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt

    if temp != 1:
        arr[temp] = 1
    if len(arr.keys()) == 0:
        arr[n] = 1
    return arr


def prepare(n, MOD):

    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs

import math
from itertools import combinations
from collections import Counter
MOD = 1000000007

def solve():
    N, M = map(int, input().split())
    temp = factorization(M)
    #print(temp)
    max_num = max(temp.values())
    factorials, invs = prepare(N+max_num-1, MOD)
    #print(factorials, invs)
    ans = 1
    for i, j in temp.items():
        if i == 1:
            break
        ans *= factorials[N+j-1]
        ans %= MOD
        ans *= invs[N-1]
        ans %= MOD
        ans *= invs[j]
        ans %= MOD
        #print(ans)
    print(ans)


if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
def factorization(n):
    arr = {}
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt

    if temp != 1:
        arr[temp] = 1
    #if len(arr.keys()) == 0:
    #    arr[n] = 1
    return arr


def prepare(n, MOD):

    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs

import math
from itertools import combinations
from collections import Counter
MOD = 1000000007

def solve():
    N, M = map(int, input().split())
    temp = factorization(M)
    #print(temp)
    max_num = max(temp.values())
    factorials, invs = prepare(N+max_num-1, MOD)
    #print(factorials, invs)
    ans = 1
    for i, j in temp.items():
        ans *= factorials[N+j-1]
        ans %= MOD
        ans *= invs[N-1]
        ans %= MOD
        ans *= invs[j]
        ans %= MOD
        #print(ans)
    print(ans)


if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])
    if temp != 1:
        arr.append([temp, 1])
    if arr == []:
        arr.append([n, 1])
    return arr


def prepare(n, MOD):

    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs

import math
from itertools import combinations
from collections import Counter
MOD = 1000000007

def solve():
    N, M = map(int, input().split())
    factorials, invs = prepare(N, MOD)
    #print(factorials, invs)
    temp = factorization(M)
    nums = []
    for i, j in temp:
        for k in range(j):
            nums.append(i)
    #print(nums)

    divisors = set()
    for i in range(1, len(nums)+1):
        for temp in combinations(nums, i):
            y = 1
            for x in temp:
                y = y * x
            divisors.add(y)
    #print(divisors)

    pattern = set()
    for x in divisors:
        m = M // x
        temp_m = factorization(m)
        nums_m = []
        for i, j in temp_m:
            for k in range(j):
                nums_m.append(i)
        nums_m.append(x)
        nums_m.sort()
        pattern.add(tuple(nums_m))
    #print(pattern)

    ans = 0
    fact_N = factorials[N]
    for x in pattern:
        if 1 in x:
            ones = (N-len(x)) + 1
            cnt = Counter(list(x))
            #print(cnt)
            perm = fact_N * invs[ones]
            #print(perm)
        else:
            ones = (N-len(x))
            cnt = Counter(list(x))
            #print(cnt)
            perm = fact_N
            for value in cnt.values():
                perm = perm * invs[value]
            perm = perm * invs[ones]
            #print(perm)
        ans += perm
    print(ans % MOD)

if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])
    if temp != 1:
        arr.append([temp, 1])
    if arr == []:
        arr.append([n, 1])
    return arr


def prepare(n, MOD):

    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs

import math
from itertools import combinations
from collections import Counter
MOD = 1000000007

def solve():
    N, M = map(int, input().split())
    factorials, invs = prepare(N, MOD)
    #print(factorials, invs)
    temp = factorization(M)
    nums = []
    for i, j in temp:
        for k in range(j):
            nums.append(i)
    #print(nums)

    divisors = set()
    for i in range(1, len(nums)+1):
        for temp in combinations(nums, i):
            y = 1
            for x in temp:
                y = y * x
            divisors.add(y)
    #print(divisors)

    pattern = set()
    for x in divisors:
        m = M // x
        temp_m = factorization(m)
        nums_m = []
        for i, j in temp_m:
            for k in range(j):
                nums_m.append(i)
        nums_m.append(x)
        nums_m.sort()
        pattern.add(tuple(nums_m))
    #print(pattern)

    ans = 0
    fact_N = factorials[N]
    for x in pattern:
        if 1 in x:
            ones = (N-len(x)) + 1
            cnt = Counter(list(x))
            print(cnt)
            perm = fact_N * invs[ones]
            print(perm)
        else:
            ones = (N-len(x))
            cnt = Counter(list(x))
            print(cnt)
            perm = fact_N
            for value in cnt.values():
                perm = perm * invs[value]
            perm = perm * invs[ones]
            print(perm)
        ans += perm
    print(ans % MOD)

if __name__ == "__main__":
    solve()

Buggy submission (verdict: Runtime Error):
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])
    if temp != 1:
        arr.append([temp, 1])
    if arr == []:
        arr.append([n, 1])
    return arr


def prepare(n, MOD):

    # 1! - n! の計算
    f = 1
    factorials = [1]  # 0!の分
    for m in range(1, n + 1):
        f *= m
        f %= MOD
        factorials.append(f)
    # n!^-1 の計算
    inv = pow(f, MOD - 2, MOD)
    # n!^-1 - 1!^-1 の計算
    invs = [1] * (n + 1)
    invs[n] = inv
    for m in range(n, 1, -1):
        inv *= m
        inv %= MOD
        invs[m - 1] = inv

    return factorials, invs

import math
from itertools import combinations
from collections import Counter
MOD = 1000000007

def solve():
    N, M = map(int, input().split())
    factorials, invs = prepare(N, MOD)
    #print(factorials, invs)
    temp = factorization(M)
    nums = []
    for i, j in temp:
        for k in range(j):
            nums.append(i)
    #print(nums)

    divisors = set()
    for i in range(1, len(nums)+1):
        for temp in combinations(nums, i):
            y = 1
            for x in temp:
                y = y * x
            divisors.add(y)
    #print(divisors)

    pattern = set()
    for x in divisors:
        m = M // x
        temp_m = factorization(m)
        nums_m = []
        for i, j in temp_m:
            for k in range(j):
                nums_m.append(i)
        nums_m.append(x)
        nums_m.sort()
        pattern.add(tuple(nums_m))
    #print(pattern)

    ans = 0
    fact_N = factorials[N]
    for x in pattern:
        if 1 in x:
            ones = (N-len(x)) + 1
            cnt = Counter(list(x))
            print(cnt)
            perm = fact_N * invs[ones]
            print(perm)
        else:
            ones = (N-len(x))
            cnt = Counter(list(x))
            print(cnt)
            perm = fact_N
            for value in cnt.values():
                perm = perm * invs[value]
            perm = perm * invs[ones]
            print(perm)
        ans += perm
    print(ans % MOD)

if __name__ == "__main__":
    solve()