Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
n = len(str(N)) - 1
ans = 0
for i in range(3, n+1):
  ans += 3**i - (2**i)*3 + 3

t = 3**n - 2**(n+1) + 1

if int(str(N)[0]) == 3:
  for i in range(3*(10**n), N+1):
    s = set([j for j in str(i)])
    if ('5' in s) and ('7' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) == 4:
  ans += t
elif int(str(N)[0]) == 5:
  ans += 3**n - (2**n)*3 + 3 + 2**n - 2
  for i in range(5*(10**n), N+1):
    s = set([j for j in str(i)])
    if ('3' in s) and ('7' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) == 6:
  ans += 2*t
elif int(str(N)[0]) == 7:
  ans += 2*t
  for i in range(7*(10**n), N+1):
    s = set([j for j in str(i)])
    if ('3' in s) and ('5' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) > 7:
  ans += 3*t
print(ans)

Fixed submission:
N = int(input())

def dfs(n):
  if int(n) > N:
    return 0
  if ('3' in n) and ('5' in n) and ('7' in n):
    ret = 1
  else:
    ret = 0
  for c in '357':
    ret += dfs(n+c)
  return ret

print(dfs('0'))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
n = len(str(N)) - 1
ans = 0
for i in range(3, n+1):
  ans += 3**i - (2**i)*3 + 3

t = 3**n - 2**(n+1) + 1

if int(str(N)[0]) == 3:
  for i in range(3*(10**n), N+1):
    s = set([j for j in str(i)])
    if ('5' in s) and ('7' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) == 4:
  ans += t
elif int(str(N)[0]) == 5:
  ans += 3**n - (2**n)*3 + 3 + 2**n - 2
  for i in range(5*(10**n), N+1):
    s = set([j for j in str(i)])
    if ('3' in s) and ('7' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) == 6:
  ans += 2*t
elif int(str(N)[0]) == 7:
  ans += 2*t
  for i in range(7*(10**n), min(N+1, 8*(10**n))):
    s = set([j for j in str(i)])
    if ('3' in s) and ('5' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) > 7:
  ans += 3*t
print(ans)


Fixed submission:
N = int(input())

def dfs(n):
  if int(n) > N:
    return 0
  if ('3' in n) and ('5' in n) and ('7' in n):
    ret = 1
  else:
    ret = 0
  for c in '357':
    ret += dfs(n+c)
  return ret

print(dfs('0'))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
n = len(str(N)) - 1
ans = 0
for i in range(3, n+1):
  ans += 3**i - (2**i)*3 + 3

t = 3**n - 2**(n+1) + 1

if int(str(N)[0]) == 3:
  for i in range(3*(10**n), N+1):
    s = set([j for j in str(i)])
    if ('5' in s) and ('7' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) == 4:
  ans += t
elif int(str(N)[0]) == 5:
  ans += 3**n - (2**n)*3 + 3 + 2**n - 2
  for i in range(5*(10**n), N+1):
    s = set([j for j in str(i)])
    if ('3' in s) and ('7' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) == 6:
  ans += 2*t
elif int(str(N)[0]) == 7:
  ans += 2*t
  for i in range(7*(10**n), min(N+1, 8*(10**n))):
    s = set([j for j in str(i)])
    if ('3' in s) and ('5' in s) and (len(s)==3):
      ans += 1
elif int(str(N)[0]) > 7:
  ans += 3*t
print(ans)

Fixed submission:
N = int(input())

def dfs(n):
  if int(n) > N:
    return 0
  if ('3' in n) and ('5' in n) and ('7' in n):
    ret = 1
  else:
    ret = 0
  for c in '357':
    ret += dfs(n+c)
  return ret

print(dfs('0'))