#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef LOCAL # include "debug_print.hpp" # define debug(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__) #else # define debug(...) (static_cast(0)) #endif using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define rrep(i,n) for(int i=(n)-1; i>=0; i--) #define FOR(i,a,b) for(int i=(a); i<(b); i++) #define RFOR(i,a,b) for(int i=(b-1); i>=(a); i--) #define ALL(v) v.begin(), v.end() #define RALL(v) v.rbegin(), v.rend() #define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ); #define pb push_back using ll = long long; using D = double; using LD = long double; using P = pair; template using PQ = priority_queue>; template using minPQ = priority_queue, greater>; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b ostream &operator<<(ostream &os, const pair &p) { os << p.first << " " << p.second; return os; } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const vector &v) { int s = (int)v.size(); for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i]; return os; } template istream &operator>>(istream &is, vector &v) { for (auto &x : v) is >> x; return is; } void in() {} template void in(T &t, U &...u) { cin >> t; in(u...); } void out() { cout << "\n"; } template void out(const T &t, const U &...u) { cout << t; if (sizeof...(u)) cout << sep; out(u...); } void outr() {} template void outr(const T &t, const U &...u) { cout << t; outr(u...); } #include "atcoder/modint.hpp" #include "atcoder/convolution.hpp" using mint = atcoder::modint998244353; constexpr int mod = 998244353; constexpr ll modpow(const ll& a, ll n, ll mod) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2, mod); t = t * t % mod; if (n & 1) t = t * a % mod; return t; } int calc_primitive_root(int mod) { if (mod == 2) return 1; if (mod == 167772161) return 3; if (mod == 469762049) return 3; if (mod == 754974721) return 11; if (mod == 998244353) return 3; int divs[20] = {}; divs[0] = 2; int cnt = 1; long long x = (mod - 1) / 2; while (x % 2 == 0) x /= 2; for (long long i = 3; i * i <= x; i += 2) { if (x % i == 0) { divs[cnt++] = i; while (x % i == 0) x /= i; } } if (x > 1) divs[cnt++] = x; for (int g = 2;; g++) { bool ok = true; for (int i = 0; i < cnt; i++) { if (modpow(g, (mod - 1) / divs[i], mod) == 1) { ok = false; break; } } if (ok) return g; } } constexpr int mx = 200005; ll fact[mx]; ll inv[mx]; ll finv[mx]; ll p; ll com(int n, int m){ if(n < 0 or m < 0 or n < m) return 0; return (fact[n] * finv[m] * finv[n-m]) % p; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n; in(n,p); ll g = calc_primitive_root(p); rep(i,mx) fact[i] = inv[i] = finv[i] = 1; for(int i = 2; i < mx; i++){ fact[i] = fact[i-1] * i % p; inv[i] = (p-inv[p%i]) * (p/i) % p; finv[i] = finv[i-1] * inv[i] % p; } vector nv; while(n > 0){ nv.pb(n%p); n /= p; } reverse(ALL(nv)); int k = nv.size(); vector pow(p,1); vector ipow(p); FOR(i,1,p){ pow[i] = pow[i-1]*g%p; ipow[pow[i]] = i%(p-1); } queue> q; rep(i,k){ vector c(p-1); rep(j,nv[i]+1){ ll cc = com(nv[i],j); c[ipow[cc]]++; } q.push(c); } while(q.size() > 1){ auto f1 = q.front(); q.pop(); auto f2 = q.front(); q.pop(); q.push(atcoder::convolution(f1,f2)); } mint ans = 0; int m = q.front().size(); rep(i,m){ ll x = pow[i%(p-1)]; ans += q.front()[i] * x; } out(ans.val()); }