from collections.abc import Iterable def accum_dp(xs: Iterable, f, op, e, init: dict, *, is_reset=True): dp = init.copy() for x in xs: pp = {} if is_reset else dp.copy() dp, pp = pp, dp for fm_key, fm_val in pp.items(): for to_key, to_val in f(fm_key, fm_val, x): dp[to_key] = op(dp.get(to_key, e), to_val) return dp def f(k, v, ix): diff, cmp, ilt, jlt = k cnt, tot = v # (個数, 総和) e, x = ix tot2 = tot + cnt * pow(2, e, MOD) for id in range(2): if not ilt and id > x: continue for jd in range(2): if not jlt and jd > x: continue if cmp == 0 and id > jd: continue n_ilt = ilt | (id < x) n_jlt = jlt | (jd < x) n_cmp = cmp | (id < jd) n_diff = diff + (jd - id) n_tot = tot2 if id == jd == 1 else tot yield (n_diff, n_cmp, n_ilt, n_jlt), (cnt, n_tot) def op(a, b): a1, a2 = a b1, b2 = b return (a1+b1) % MOD, (a2+b2) % MOD def digit_dp() -> int: digits = [int(c) for c in bin(N)[2:]] init = {(0, 0, False, False): (1, 0)} nd = len(digits) xs = [(e, d) for e, d in zip(reversed(range(nd)), digits)] dp = accum_dp(xs, f, op, (0, 0), init) ans = 0 for (diff, _, _, _), v in dp.items(): if diff == 0: ans += v[1] ans %= MOD return ans MOD = 998244353 N = int(input()) ans = digit_dp() print(ans)