Problem p03212 (python)

Fixed submission:
N = int(input())
S = str(N)
def f(s, p, x):
    if len(s) == 0:
        return 1 if x == 7 else 0
    if p:
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], True, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '8' or s[0] == '9':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], True, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '7':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], False, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '6':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '5':
        return f(s[1:], True, x | 1) + f(s[1:], False, x | 2) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '4':
        return f(s[1:], True, x | 1) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '3':
        return f(s[1:], False, x | 1) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '2' or s[0] == '1':
        return (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '0':
        return 0
ans = f(S, False, 0)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(100000)
N = int(input())
S = str(N)
def f(s, p, x):
    if len(s) == 0:
        return 1 if x == 7 else 0
    if p:
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], True, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '8' or s[0] == '9':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], True, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '7':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], False, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '6':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '5':
        return f(s[1:], True, x | 1) + f(s[1:], False, x | 2) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '4':
        return f(s[1:], True, x | 1) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '3':
        return f(s[1:], False, x | 1) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '2' or s[1] == '1':
        return (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '0':
        return 0
ans = f(S, False, 0)
print(ans)

Buggy submission (verdict: Runtime Error):
N = int(input())
S = str(N)
def f(s, p, x):
    if len(s) == 0:
        return 1 if x == 7 else 0
    if p:
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], True, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '8' or s[0] == '9':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], True, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '7':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + f(s[1:], False, x | 4) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '6':
        return f(s[1:], True, x | 1) + f(s[1:], True, x | 2) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '5':
        return f(s[1:], True, x | 1) + f(s[1:], False, x | 2) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '4':
        return f(s[1:], True, x | 1) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '3':
        return f(s[1:], False, x | 1) + (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '2' or s[1] == '1':
        return (f(s[1:], True, x) if x == 0 else 0)
    if s[0] == '0':
        return 0
ans = f(S, False, 0)
print(ans)