Problem p03212 (python)

Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin

d_in = lambda: int(stdin.readline())  # N = d_in()

cand = [0, 3, 5, 7]
N = d_in()


def check_count(count):
    if count[3] > 0 and count[5] > 0 and count[7] > 0:
        return True
    else:
        return False


def search(flag, nonzero, str_n, count):
    """
    flag : 現在の値がNと等しいか小さいか

    nonzero : 現在の値が0か否か

    str_n : 現在より小さい方の各桁を文字列として持つリスト

    count : {7: flag1, 5: flag2, 3: flag3}
    """
    if len(str_n) == 0:
        if check_count(count):
            return 1
        else:
            return 0
    elif check_count(count) and (not flag):
        return 3**(len(str_n))
    elif not flag:
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            next_count[i] += 1
            zeroflag = nonzero or (i != 0)
            ans += search(False, zeroflag, str_n[1:], next_count)
        return ans
    else:
        next_n = int(str_n[0])
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            if i > next_n:
                break
            elif i == 0:
                zeroflag = nonzero
                ans += search(False, zeroflag, str_n[1:], next_count)
            elif i == next_n:
                next_count[i] += 1
                ans += search(True, True, str_n[1:], next_count)
            else:
                next_count[i] += 1
                ans += search(False, True, str_n[1:], next_count)
        return ans


str_N = list(str(N))
cand_count = {0: 0, 3: 0, 5: 0, 7: 0}
print(search(True, False, str_N, cand_count))

Buggy submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from sys import stdin
import IPython

d_in = lambda: int(stdin.readline())  # N = d_in()

cand = [0, 3, 5, 7]
N = d_in()


def check_count(count):
    if count[3] > 0 and count[5] > 0 and count[7] > 0:
        return True
    else:
        return False


def search(flag, nonzero, str_n, count):
    """
    flag : 現在の値がNと等しいか小さいか

    nonzero : 現在の値が0か否か

    str_n : 現在より小さい方の各桁を文字列として持つリスト

    count : {7: flag1, 5: flag2, 3: flag3}
    """
    if len(str_n) == 0:
        if check_count(count):
            return 1
        else:
            return 0
    elif check_count(count) and (not flag):
        return 3**(len(str_n))
    elif not flag:
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            next_count[i] += 1
            zeroflag = nonzero or (i != 0)
            ans += search(False, zeroflag, str_n[1:], next_count)
        return ans
    else:
        next_n = int(str_n[0])
        ans = 0
        for i in cand:
            if nonzero and i == 0:
                continue
            next_count = count.copy()
            if i > next_n:
                break
            elif i == 0:
                zeroflag = nonzero
                ans += search(False, zeroflag, str_n[1:], next_count)
            elif i == next_n:
                next_count[i] += 1
                ans += search(True, True, str_n[1:], next_count)
            else:
                next_count[i] += 1
                ans += search(False, True, str_n[1:], next_count)
        return ans


str_N = list(str(N))
cand_count = {0: 0, 3: 0, 5: 0, 7: 0}
print(search(True, False, str_N, cand_count))
# IPython.embed()