#pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target ("avx") #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 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; } //////////////////////////////////////////////////////////////////////////////// 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; template struct SegmentTree { const OpTT opTT; const OpST opST; const OpSS opSS; const T idT; const S idS; int n; vector ts; vector ss; SegmentTree(int n_, const OpTT opTT, const OpST opST, const OpSS opSS, const T &idT, const S &idS) : opTT(opTT), opST(opST), opSS(opSS), idT(idT), idS(idS) { for (n = 1; n < n_; n <<= 1) {} ts.assign(n << 1, idT); ss.assign(n << 1, idS); } T &at(int a) { return ts[n + a]; } void build() { for (int a = n; --a; ) ts[a] = opTT(ts[a << 1], ts[a << 1 | 1]); } void update(int a, int b, const S &s) { update(1, 0, n, a, b, s); } T calc(int a, int b) { return calc(1, 0, n, a, b); } private: void update(int u, int l, int r, int a, int b, const S &s) { if (a < l) a = l; if (b > r) b = r; if (a >= b) return; if (a == l && b == r) { ts[u] = opST(s, ts[u], r - l); ss[u] = opSS(s, ss[u]); return; } const int uL = u << 1, uR = u << 1 | 1; const int mid = (l + r) >> 1; // speed-up: if (ss[u] != idS) if (ss[u]) { ts[uL] = opST(ss[u], ts[uL], mid - l); ts[uR] = opST(ss[u], ts[uR], r - mid); ss[uL] = opSS(ss[u], ss[uL]); ss[uR] = opSS(ss[u], ss[uR]); ss[u] = idS; } update(uL, l, mid, a, b, s); update(uR, mid, r, a, b, s); ts[u] = opTT(ts[uL], ts[uR]); return; } T calc(int u, int l, int r, int a, int b) { if (a < l) a = l; if (b > r) b = r; if (a >= b) return idT; if (a == l && b == r) { return ts[u]; } const int uL = u << 1, uR = u << 1 | 1; const int mid = (l + r) >> 1; // speed-up: if (ss[u] != idS) if (ss[u]) { ts[uL] = opST(ss[u], ts[uL], mid - l); ts[uR] = opST(ss[u], ts[uR], r - mid); ss[uL] = opSS(ss[u], ss[uL]); ss[uR] = opSS(ss[u], ss[uR]); ss[u] = idS; } const T resL = calc(uL, l, mid, a, b); const T resR = calc(uR, mid, r, a, b); ts[u] = opTT(ts[uL], ts[uR]); return opTT(resL, resR); } }; namespace s0 { using T = Int; using S = Int; T opTT(const T &t0, const T &t1) { return t0 + t1; } T opST(const S &s, const T &t, int sz) { return s * sz + t; } S opSS(const S &s0, const S &s1) { return s0 + s1; } constexpr T idT = 0; constexpr S idS = 0; using SegTree = SegmentTree; SegTree factory(int n) { return SegTree(n, &opTT, &opST, &opSS, idT, idS); } } // namespace s0 namespace segtree2 { struct T { Mint x[2]; T() : x{} {} }; using S = Mint; T opTT(const T &t0, const T &t1) { T tt; tt.x[0] = t0.x[0] + t1.x[0]; tt.x[1] = t0.x[1] + t1.x[1]; return tt; } T opST(const S &s, const T &t, int sz) { T tt; tt.x[0] = t.x[0]; tt.x[1] = t.x[1] + s * t.x[0]; return tt; } S opSS(const S &s0, const S &s1) { return s0 + s1; } const T idT; constexpr S idS = 0; using SegTree = SegmentTree; SegTree factory(int n) { return SegTree(n, &opTT, &opST, &opSS, idT, idS); } } // namespace segtree2 namespace segtree3 { struct T { Mint x[3]; T() : x{} {} }; using S = Mint; T opTT(const T &t0, const T &t1) { T tt; tt.x[0] = t0.x[0] + t1.x[0]; tt.x[1] = t0.x[1] + t1.x[1]; tt.x[2] = t0.x[2] + t1.x[2]; return tt; } T opST(const S &s, const T &t, int sz) { T tt; tt.x[0] = t.x[0]; tt.x[1] = t.x[1] + s * t.x[0]; tt.x[2] = t.x[2] + s * (2 * t.x[1] + s * t.x[0]); return tt; } S opSS(const S &s0, const S &s1) { return s0 + s1; } const T idT; constexpr S idS = 0; using SegTree = SegmentTree; SegTree factory(int n) { return SegTree(n, &opTT, &opST, &opSS, idT, idS); } } // namespace segtree3 int N, M; vector A; int main() { for (; ~scanf("%d%d", &N, &M); ) { A.resize(N + M); for (int i = 0; i < N; ++i) { A[i] = N - 1 - i; } for (int i = N; i < N + M; ++i) { scanf("%d", &A[i]); --A[i]; } // cerr<<"A = ";pv(A.begin(),A.end()); if (N == 1) { printf("%u\n", Mint(M).x); continue; } if (N == 2) { Mint ans2 = M; for (int i = N; i < N + M; ++i) { for (int a = 0; a < 2; ++a) for (int b = 0; b < 2; ++b) if (a != b) { Mint tmp = 1; tmp *= ((A[i - 1] == -2) ? Mint(2).inv() : (A[i - 1] == a) ? 1 : 0); tmp *= ((A[i] == -2) ? Mint(2).inv() : (A[i] == b) ? 1 : 0); ans2 += tmp; } } printf("%u\n", ans2.x); continue; } vector ls(N + M + 1, 0); for (int i = 0; i < N + M; ++i) { ls[i + 1] = ls[i] + ((A[i] == -2) ? 1 : 0); } // cerr<<"ls = ";pv(ls.begin(),ls.end()); Mint ans[2][2] = {}; vector pw0(N + M + 1), pw1(N + M + 1), pw2(N + M + 1), invPw1(N + M + 1), invPw2(N + M + 1); pw0[0] = pw1[0] = pw2[0] = invPw1[0] = invPw2[0] = 1; pw0[1] = Mint(N); pw1[1] = Mint(N - 1); pw2[1] = Mint(N - 2); invPw1[1] = Mint(N - 1).inv(); invPw2[1] = Mint(N - 2).inv(); for (int i = 2; i <= N + M; ++i) { pw0[i] = pw0[i - 1] * pw0[1]; pw1[i] = pw1[i - 1] * pw1[1]; pw2[i] = pw2[i - 1] * pw2[1]; invPw1[i] = invPw1[i - 1] * invPw1[1]; invPw2[i] = invPw2[i - 1] * invPw2[1]; } { vector app(N, 0); auto seg0 = s0::factory(N + M); auto seg1 = segtree2::factory(N + M); auto seg2 = segtree3::factory(N + M); for (int i = 0; i < N + M; ++i) { if (A[i] == -2) { seg1.at(i).x[0] = pw0[ls[i]] * invPw1[ls[i + 1]]; seg2.at(i).x[0] = pw0[ls[i]] * invPw2[ls[i + 1]]; } } seg1.build(); seg2.build(); for (int i = 0; i < N + M; ++i) { // get if (i >= N) { if (A[i] != -2) { { const int m = seg0.calc(app[A[i]], app[A[i]] + 1); Mint tmp = 0; tmp += pw1[ls[i] - ls[app[A[i]] + 1]] * Mint(1 + m); tmp += Mint(N - 1 - m) * (pw1[ls[i] - ls[app[A[i]] + 1]] - pw2[ls[i] - ls[app[A[i]] + 1]]); tmp *= pw0[ls[app[A[i]]] + (ls[N + M] - ls[i + 1])]; ans[0][0] += tmp; } { const auto res1 = seg1.calc(app[A[i]], i); const auto res2 = seg2.calc(app[A[i]], i); Mint tmp = 0; tmp += pw1[ls[i]] * (res1.x[0] + res1.x[1]); tmp += pw1[ls[i]] * (Mint(N - 1) * res1.x[0] - res1.x[1]) - pw2[ls[i]] * (Mint(N - 1) * res2.x[0] - res2.x[1]); tmp *= pw0[ls[N + M] - ls[i + 1]]; ans[1][0] += tmp; } } else { { const auto res1 = seg1.calc(0, i); const auto res2 = seg2.calc(0, i); Mint tmp = 0; // tmp += pw1[ls[i]] * (Mint(N) * res1.x[0] + Mint(N - 1) * res1.x[1] - res1.x[2]); // tmp += pw1[ls[i]] * (Mint(N) * Mint(N - 1) * res1.x[0] - Mint(2 * N - 1) * res1.x[1] + res1.x[2]) tmp += pw1[ls[i]] * (Mint(N) * res1.x[0] + Mint(N - 1) * res1.x[1]); tmp += pw1[ls[i]] * (Mint(N) * Mint(N - 1) * res1.x[0] - Mint(2 * N - 1) * res1.x[1]) - pw2[ls[i]] * (Mint(N) * Mint(N - 1) * res2.x[0] - Mint(2 * N - 1) * res2.x[1] + res2.x[2]); tmp *= pw0[ls[N + M] - ls[i + 1]]; ans[1][1] += tmp; } } } // add if (A[i] != -2) { seg0.update(app[A[i]], i, 1); seg1.update(app[A[i]], i, 1); seg2.update(app[A[i]], i, 1); app[A[i]] = i; } } } { reverse(A.begin(), A.end()); for (int i = 0; i < N + M; ++i) { ls[i + 1] = ls[i] + ((A[i] == -2) ? 1 : 0); } // cerr<<"A = ";pv(A.begin(),A.end()); // cerr<<"ls = ";pv(ls.begin(),ls.end()); vector app(N, 0); auto seg1 = segtree2::factory(N + M); auto seg2 = segtree2::factory(N + M); for (int i = 0; i < N + M; ++i) { if (A[i] == -2) { seg1.at(i).x[0] = pw0[ls[i]] * invPw1[ls[i + 1]]; seg2.at(i).x[0] = pw0[ls[i]] * invPw2[ls[i + 1]]; } } seg1.build(); seg2.build(); for (int i = 0; i < M + N; ++i) { // get { if (A[i] != -2) { {} { const auto res1 = seg1.calc(app[A[i]], min(i, M)); const auto res2 = seg2.calc(app[A[i]], min(i, M)); Mint tmp = 0; tmp += pw1[ls[i]] * (res1.x[0] + res1.x[1]); tmp += pw1[ls[i]] * (Mint(N - 1) * res1.x[0] - res1.x[1]) - pw2[ls[i]] * (Mint(N - 1) * res2.x[0] - res2.x[1]); tmp *= pw0[ls[N + M] - ls[i + 1]]; ans[0][1] += tmp; } } else { {} } } // add if (A[i] != -2) { seg1.update(app[A[i]], i, 1); seg2.update(app[A[i]], i, 1); app[A[i]] = i; } } reverse(A.begin(), A.end()); for (int i = 0; i < N + M; ++i) { ls[i + 1] = ls[i] + ((A[i] == -2) ? 1 : 0); } } printf("%u\n", ((ans[0][0] + ans[0][1] + ans[1][0] + ans[1][1]) / Mint(N).pow(ls[N + M])).x); #ifdef LOCAL Mint brt[2][2] = {}; for (int i = 0; i < N + M; ++i) for (int j = N; j < N + M; ++j) { if (i < j) { set as(A.begin() + i + 1, A.begin() + j); as.erase(-2); const int m = as.size(); Mint tmp = 0; tmp += Mint(N - 1).pow(ls[j] - ls[i + 1]) * Mint(1 + m); tmp += Mint(N - 1 - m) * (Mint(N - 1).pow(ls[j] - ls[i + 1]) - Mint(N - 2).pow(ls[j] - ls[i + 1])); tmp *= Mint(N).pow(ls[i] + (ls[N + M] - ls[j + 1])); if (A[i] != -2) { if (A[j] != -2) { if (A[i] == A[j] && as.find(A[i]) == as.end()) { brt[0][0] += tmp; } } else { if (as.find(A[i]) == as.end()) { brt[0][1] += tmp; } } } else { if (A[j] != -2) { if (as.find(A[j]) == as.end()) { brt[1][0] += tmp; } } else { brt[1][1] += Mint(N - m) * tmp; } } } } cerr << "brt = " << ((brt[0][0] + brt[0][1] + brt[1][0] + brt[1][1]) / Mint(N).pow(ls[N + M])) << "; " << brt[0][0] << " " << brt[0][1] << " " << brt[1][0] << " " << brt[1][1] << endl; cerr << "ans = " << ((ans[0][0] + ans[0][1] + ans[1][0] + ans[1][1]) / Mint(N).pow(ls[N + M])) << "; " << ans[0][0] << " " << ans[0][1] << " " << ans[1][0] << " " << ans[1][1] << endl; #endif } return 0; }