# n, m逆にしちゃった(このコードを見てる人は注意!) mod = 998244353 m, n = map(int,input().split()) ns = bin(n)[2:] size = len(ns) # A[N] = K に固定すると, pow(N, popcount) # 桁DP, popcountで主客転倒する dp = [0] * 31 tmp = 0 for i in range(size): ndp = [0] * 31 # 継続 for k in range(30): ndp[k+1] += dp[k] for k in range(31): ndp[k] += dp[k] # 子供 if i > 0: ndp[1] += 1 # 新規 if i > 0 and ns[i] == "1": ndp[tmp] += 1 for k in range(31): dp[k] = ndp[k] % mod if ns[i] == "1": tmp += 1 dp[0] += 1 dp[0] %= mod dp[tmp] += 1 dp[tmp] %= mod ans = 0 for i in range(31): ans += pow(m, i, mod) * dp[i] ans %= mod print(ans)