#include <bits/stdc++.h> #include <atcoder/modint> using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); /* A_Nを固定する。 k=N, N-1, ..., 1と見ていった時、0->1となっていてはいけない。 つまり、A_Nのpopcountをkとすると、 A_0=0, A_1, ..., A_N-1のN箇所のどこかでそのbitが1になる。 よって、(M以下でpopcountがkになるもの)*N^kの和が答え。 bit DPでやる。 */ ll N, M; cin >> N >> M; ll popsame=0; vector<mint> dp(31); for (ll i=30; i>=0; i--){ ll c = (M>>i) & 1; popsame += c; vector<mint> pd(31); if (c){ //same->small pd[popsame-1] += 1; //small->small for (int j=0; j<=30; j++){ pd[j] += dp[j]; if (j) pd[j] += dp[j-1]; } } else{ //small->small for (int j=0; j<=30; j++){ pd[j] += dp[j]; if (j) pd[j] += dp[j-1]; } } swap(dp, pd); } dp[popsame]++; mint ans=0, pw=1; for (int i=0; i<=30; i++){ ans += dp[i] * pw; pw *= N; } cout << ans.val() << endl; return 0; }