MOD = 998244353 def solve(): import sys data = sys.stdin.read().split() if not data: return N = int(data[0]) if N == 0: print(0) return # ?N????????? bin_str = bin(N)[2:] n = len(bin_str) # ???DP from functools import lru_cache @lru_cache(maxsize=None) def dfs(pos, limit, has_started): if pos == n: # ????????????????1?f?0?????f??0? return (1, 0) if has_started else (0, 0) max_digit = 1 if not limit else int(bin_str[pos]) total_count = 0 total_sum = 0 for digit in range(max_digit + 1): next_limit = limit and (digit == max_digit) next_has_started = has_started or (digit == 1) if not next_has_started: # ??????????????? cnt, s = dfs(pos + 1, next_limit, next_has_started) total_count = (total_count + cnt) % MOD total_sum = (total_sum + s) % MOD else: # ??????????????? cnt, s = dfs(pos + 1, next_limit, next_has_started) total_count = (total_count + cnt) % MOD # ??????? # ???????????????????1?f(1)=1? # ?????????1????1?popcount??1? # ?????????????????????? if not has_started and digit == 1: # ???????????f(1)=1 total_sum = (total_sum + cnt + s) % MOD else: # ????????????1???1?popcount? # ?????????????????????? bit_contrib = 1 if digit == 1 else 0 length_contrib = 1 # ????????????????? total_sum = (total_sum + cnt * (bit_contrib + length_contrib) + s) % MOD return total_count, total_sum # ???1?N?f(i)?? # ??????DP?????0?N?f(i)???????f(0)=0 count, result = dfs(0, True, False) # ?????DP???0???????1??????????? print(result % MOD) if __name__ == '__main__': solve()