MOD = 998244353 N, M = map(int, input().split()) # Precompute N^c mod MOD for c from 0 to 30 powN = [1] * (31) for c in range(1, 31): powN[c] = (powN[c-1] * N) % MOD # Compute the binary representation of M as a list of 30 bits (MSB first) bits = [] for i in range(30): bit = (M >> (29 - i)) & 1 bits.append(bit) # Initialize the DP table: dp[tight][count] dp = [[0] * 31 for _ in range(2)] dp[1][0] = 1 # Start with tight=True and count=0 for i in range(30): next_dp = [[0] * 31 for _ in range(2)] for tight in [0, 1]: for count in range(31): if dp[tight][count] == 0: continue max_bit = bits[i] if tight else 1 for b in [0, 1]: if b > max_bit: continue new_tight = 1 if (tight and (b == max_bit)) else 0 new_count = count + b if new_count > 30: continue # Cannot have more than 30 bits set next_dp[new_tight][new_count] = (next_dp[new_tight][new_count] + dp[tight][count]) % MOD dp = next_dp # Sum all possibilities for tight and count total = 0 for tight in [0, 1]: for count in range(31): total = (total + dp[tight][count] * powN[count]) % MOD print(total)