#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using Int = long long; template ostream &operator<<(ostream &os, const pair &a) { return os << "(" << a.first << ", " << a.second << ")"; }; template ostream &operator<<(ostream &os, const vector &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; } template void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; } template bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; } template bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; } #define COLOR(s) ("\x1b[" s "m") //////////////////////////////////////////////////////////////////////////////// template struct ModInt { static constexpr unsigned M = M_; unsigned x; constexpr ModInt() : x(0U) {} constexpr ModInt(unsigned x_) : x(x_ % M) {} constexpr ModInt(unsigned long long x_) : x(x_ % M) {} constexpr ModInt(int x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} constexpr ModInt(long long x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; //////////////////////////////////////////////////////////////////////////////// constexpr unsigned MO = 998244353; using Mint = ModInt; #define ModInt ModIntRunTime //////////////////////////////////////////////////////////////////////////////// struct ModInt { static unsigned M; unsigned x; ModInt() : x(0U) {} ModInt(unsigned x_) : x(x_ % M) {} ModInt(unsigned long long x_) : x(x_ % M) {} ModInt(int x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModInt(long long x_) : x(((x_ %= static_cast(M)) < 0) ? (x_ + static_cast(M)) : x_) {} ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; } ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; } ModInt &operator*=(const ModInt &a) { x = (static_cast(x) * a.x) % M; return *this; } ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); } ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b; } ModInt inv() const { unsigned a = M, b = x; int y = 0, z = 1; for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast(q) * z; y = z; z = w; } assert(a == 1U); return ModInt(y); } ModInt operator+() const { return *this; } ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; } ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); } ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); } ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); } ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); } template friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); } template friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); } template friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); } template friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); } explicit operator bool() const { return x; } bool operator==(const ModInt &a) const { return (x == a.x); } bool operator!=(const ModInt &a) const { return (x != a.x); } friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; } }; unsigned ModInt::M; //////////////////////////////////////////////////////////////////////////////// #undef ModInt // a x + b y = g int gojo(int a, int b, int &x, int &y) { if (b) { const int g = gojo(b, a % b, y, x); y -= (a / b) * x; return g; } else { x = 1; y = 0; return a; } } int D, M; // generating // a: upper triangular // a[i][i] = 0 or a[i][i] | M constexpr int MAX_D = 110; ModIntRunTime a[MAX_D][MAX_D]; void init() { for (int i = 0; i < D; ++i) { fill(a[i], a[i] + D, 0); } } void add(vector bs) { // call by value for (int i = 0; i < D; ++i) if (bs[i].x) { const int a0 = a[i][i].x ? a[i][i].x : M; int x, y; const int g = gojo(a0, bs[i].x, x, y); const int z = -(int)bs[i].x / g, w = a0 / g; // [ g ] = [ x y ] [ a ] // [ 0 ] [ z w ] [ b ] a[i][i] = g; for (int j = i + 1; j < D; ++j) { const ModIntRunTime t = x * a[i][j] + y * bs[j]; bs[j] = z * a[i][j] + w * bs[j]; a[i][j] = t; } } } bool check(vector bs) { // call by value for (int i = 0; i < D; ++i) if (bs[i]) { if (!a[i][i].x || bs[i].x % a[i][i].x != 0) { return false; } const int x = bs[i].x / a[i][i].x; for (int j = i + 1; j < D; ++j) { bs[j] -= x * a[i][j]; } } return true; } vector lexMax() { vector bs(D, 0); for (int i = 0; i < D; ++i) if (a[i][i].x) { const int x = (M - 1 - bs[i].x) / a[i][i].x; for (int j = i; j < D; ++j) { bs[j] += x * a[i][j]; } } return bs; } template T count() { T prod = 1; for (int i = 0; i < D; ++i) if (a[i][i].x) { prod *= (M / a[i][i].x); } return prod; } int main() { int Q; for (; ~scanf("%d%d", &M, &Q); ) { ModIntRunTime::M = M; D = 0; for (Int x = 1001001001LL; x; x /= M) ++D; init(); for (int q = 0; q < Q; ++q) { // for(int i=0;i V(D); for (int i = 0; i < D; ++i) { V[i] = X; X /= M; } reverse(V.begin(), V.end()); // cerr< prods(D + 1); prods[D] = 1; for (int i = D; --i >= 0; ) { prods[i] = (a[i][i].x ? (M / a[i][i].x) : 1) * prods[i + 1]; } if (K < prods[0]) { vector V(D, 0); Int k = K; for (int i = 0; i < D; ++i) if (a[i][i].x) { const Int r = k / prods[i + 1]; k %= prods[i + 1]; const ModIntRunTime s = r - ((Int)V[i].x / (Int)a[i][i].x); for (int j = i; j < D; ++j) { V[j] += s * a[i][j]; } } Int ans = 0; for (int i = 0; i < D; ++i) { (ans *= M) += V[i].x; } printf("%lld\n", ans); } else { puts("-1"); } } break; case 3: { Int X; scanf("%lld", &X); ++X; vector V(D); for (int i = 0; i < D; ++i) { V[i] = X; X /= M; } reverse(V.begin(), V.end()); // cerr< prods(D + 1); prods[D] = 1; for (int i = D; --i >= 0; ) { prods[i] = (a[i][i].x ? (M / a[i][i].x) : 1) * prods[i + 1]; } Int ans = 0; if (X) { ans += prods[0]; } else { vector W(D, 0); for (int i = 0; i < D; ++i) { if (a[i][i].x) { const Int w = (Int)W[i].x % (Int)a[i][i].x; const Int q = (Int)(V[i].x - w) / (Int)a[i][i].x; const Int r = (Int)(V[i].x - w) % (Int)a[i][i].x; // cerr<<" W = "<