#include #include using namespace std; using namespace atcoder; // using mint = modint1000000007; // const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; const int INF = 1e9; // const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, l, r) for (int i = (l); i < (r); ++i) #define rrep(i, n) for (int i = (n)-1; i >= 0; --i) #define rrep2(i, l, r) for (int i = (r)-1; i >= (l); --i) #define all(x) (x).begin(), (x).end() #define allR(x) (x).rbegin(), (x).rend() #define P pair template inline bool chmax(A& a, const B& b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A& a, const B& b) { if (a > b) { a = b; return true; } return false; } #ifndef KWM_T_MATH_MODINT_BINOM_HPP #define KWM_T_MATH_MODINT_BINOM_HPP #include #include namespace kwm_t::math::modint { /** * @brief 二項係数・順列・多項係数(modint用) * * 典型用途: * nCk, nPk, 多項係数 * * 計算量: * 前計算 O(N) * クエリ O(1) * * @tparam Mint modint型 (例: atcoder::static_modint) * * 制約 / 注意: * - mod は素数 * - n < mod(階乗が0にならない範囲) * * 使用例: * Binom C(2000000); * C.com(n, k); * * verified: */ template struct Binom { std::vector fact, ifact; Binom(int n = 2000006) { init(n); } void init(int n) { fact.resize(n + 1); ifact.resize(n + 1); fact[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = fact[i - 1] * i; } ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) { ifact[i - 1] = ifact[i] * i; } } // nCk Mint com(int n, int k) const { if (k < 0 || k > n) return 0; return fact[n] * ifact[k] * ifact[n - k]; } Mint operator()(int n, int k) const { return com(n, k); } // nPk Mint perm(int n, int k) const { if (k < 0 || k > n) return 0; return fact[n] * ifact[n - k]; } // nCk (nが大きくkが小さい場合) Mint com_sub(long long n, long long k) const { if (k < 0 || k > n) return 0; if (n - k < k) k = n - k; assert(k < (int)fact.size()); Mint res = ifact[k]; for (long long i = 0; i < k; ++i) { res *= (n - i); } return res; } // 多項係数 template Mint multinomial(int n, const Ints&... ms) const { Mint res = fact[n]; int sum = 0; for (int m : {ms...}) { if (m < 0 || m > n) return 0; res *= ifact[m]; sum += m; } if (sum > n) return 0; res *= ifact[n - sum]; return res; } // 1/x Mint inv(int x) const { if (x < (int)fact.size()) { return fact[x - 1] * ifact[x]; } return Mint(x).inv(); } // nCk の逆数 Mint inv_com(int n, int k) const { if (k < 0 || k > n) return 0; return ifact[n] * fact[k] * fact[n - k]; } }; } // namespace kwm_t::math::modint #endif // KWM_T_MATH_MODINT_BINOM_HPP #ifndef KWM_T_ALGORITHM_RUN_LENGTH_ENCODING_HPP #define KWM_T_ALGORITHM_RUN_LENGTH_ENCODING_HPP #include #include #include namespace kwm_t::algorithm { /** * @brief Run Length Encoding(連長圧縮) * * 任意の forward iterator に対して適用可能 * * 計算量: * O(N) * * 制約: * - 要素型に == が定義されていること * verified * https://atcoder.jp/contests/abc452/submissions/74695137 * https://atcoder.jp/contests/awc0051/submissions/75170743 */ template std::vector::value_type, int>> run_length_encoding(It first, It last) { using T = typename std::iterator_traits::value_type; std::vector> ret; if (first == last) return ret; It it = first; while (it != last) { It jt = it; int cnt = 0; while (jt != last && *jt == *it) { ++jt; ++cnt; } ret.emplace_back(*it, cnt); it = jt; } return ret; } // コンテナ版 template std::vector> run_length_encoding(const Container& c) { return run_length_encoding(c.begin(), c.end()); } } // namespace kwm_t::algorithm #endif // KWM_T_ALGORITHM_RUN_LENGTH_ENCODING_HPP #ifndef KWM_T_DATASTRUCTURE_INTERVAL_MAP_HPP #define KWM_T_DATASTRUCTURE_INTERVAL_MAP_HPP #include #include #include #include #include #include /** * @brief 半開区間 [l, r) の集合を管理するデータ構造(Interval Map) * * 典型用途: * - 区間の追加・削除・値の更新 * - 区間のマージ管理(重なり・隣接の統合) * - 区間被覆判定 * - 区間被覆を利用した貪欲・シミュレーション問題 * * 計算量: * - 各操作: O(k log N) * (k は分割・削除・マージされる区間数) * * @tparam T * - 区間に対応する値の型 * * @param limit * - 管理する座標の上限(番兵として使用) * * 制約 / 注意: * - 半開区間 [l, r) を扱う * - 内部で番兵(-INF, INF)を保持 * - 区間は常に「互いに素かつ同じ値の隣接区間がマージされた状態」を保つ * * 使用例: * IntervalMap st(1e9); * st.assign(2, 5, 1); * st.assign(3, 4, 2); * bool ok = st.isCovered(2, 3); * * verified: * https://atcoder.jp/contests/awc0053/submissions/75170416 */ namespace kwm_t::data_structure { template class IntervalMap { private: struct Range { int l, r; T val; bool operator<(const Range& o) const { return l < o.l; } }; std::set st; int INF; using iterator = typename std::set::iterator; bool isSentinel(const Range& x) const { return x.l == -INF || x.l == INF; } // --- split --- iterator split( int pos, const std::function& onInsert, const std::function& onErase ) { auto it = std::prev(st.lower_bound({ pos + 1, 0, T() })); if (isSentinel(*it)) return std::next(it); if (it->l == pos) return it; if (it->r <= pos) return std::next(it); Range cur = *it; st.erase(it); onErase(cur.l, cur.r, cur.val); st.insert({ cur.l, pos, cur.val }); onInsert(cur.l, pos, cur.val); auto res = st.insert({ pos, cur.r, cur.val }); onInsert(pos, cur.r, cur.val); return res.first; } void eraseRange( iterator it, const std::function& onErase ) { onErase(it->l, it->r, it->val); st.erase(it); } void addRange( int l, int r, const T& val, const std::function& onInsert ) { onInsert(l, r, val); st.insert({ l, r, val }); } public: // --- 空構造 --- explicit IntervalMap(int limit) : INF(limit + 1) { st.insert({ -INF, -INF, T() }); st.insert({ INF, INF, T() }); } // --- 配列初期化 --- template IntervalMap( const Container& data, const std::function& onInsert = [](int, int, const T&) {}, const std::function& onErase = [](int, int, const T&) {} ) : INF((int)data.size() + 1) { st.insert({ -INF, -INF, T() }); st.insert({ INF, INF, T() }); int n = (int)data.size(); for (int i = 0; i < n;) { int j = i + 1; while (j < n && data[i] == data[j]) ++j; st.insert({ i, j, data[i] }); onInsert(i, j, data[i]); i = j; } } // --- 区間代入 --- void insert( int l, int r, const T& val, const std::function& onInsert = [](int, int, const T&) {}, const std::function& onErase = [](int, int, const T&) {} ) { if (l >= r) return; auto itr = split(r, onInsert, onErase); auto itl = split(l, onInsert, onErase); // [l, r) に含まれる既存区間を削除 auto it = itl; while (it != itr) { auto jt = std::next(it); eraseRange(it, onErase); it = jt; } int nl = l; int nr = r; // 左マージ it = std::prev(st.lower_bound({ l + 1, 0, T() })); if (!isSentinel(*it) && it->r == l && it->val == val) { nl = it->l; eraseRange(it, onErase); } // 右マージ it = st.lower_bound({ r, 0, T() }); if (it != st.end() && !isSentinel(*it) && it->l == r && it->val == val) { nr = it->r; eraseRange(it, onErase); } addRange(nl, nr, val, onInsert); } void insertPoint( int x, const T& val, const std::function& onInsert = [](int, int, const T&) {}, const std::function& onErase = [](int, int, const T&) {} ) { insert(x, x + 1, val, onInsert, onErase); } // --- 区間削除 --- void eraseRange( int l, int r, const std::function& onInsert = [](int, int, const T&) {}, const std::function& onErase = [](int, int, const T&) {} ) { if (l >= r) return; auto itr = split(r, onInsert, onErase); auto itl = split(l, onInsert, onErase); auto it = itl; while (it != itr) { auto jt = std::next(it); eraseRange(it, onErase); it = jt; } } void erasePoint( int x, const std::function& onInsert = [](int, int, const T&) {}, const std::function& onErase = [](int, int, const T&) {} ) { eraseRange(x, x + 1, onInsert, onErase); } // --- 被覆判定 --- bool isCovered(int l, int r) const { if (l >= r) return true; auto it = std::prev(st.lower_bound({ l + 1, 0, T() })); if (isSentinel(*it)) return false; return it->l <= l && r <= it->r; } bool isCovered(int x) const { return isCovered(x, x + 1); } // --- 値取得 --- std::optional getValue(int x) const { auto res = get(x); if (!res) return std::nullopt; return std::get<2>(*res); } // --- 区間・値取得 --- std::optional get(int x) const { auto it = std::prev(st.lower_bound({ x + 1, 0, T() })); if (isSentinel(*it)) return std::nullopt; if (!(it->l <= x && x < it->r)) return std::nullopt; return *it; } // --- 先頭 --- std::optional front() const { auto it = std::next(st.begin()); if (isSentinel(*it)) return std::nullopt; return *it; } // --- 末尾 --- std::optional back() const { auto it = std::prev(st.end()); it = std::prev(it); if (isSentinel(*it)) return std::nullopt; return *it; } // --- 区間数 --- int rangeCount() const { return (int)st.size() - 2; } }; } // namespace kwm_t::data_structure #endif int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); kwm_t::math::modint::Binom binom; int n, q; cin >> n >> q; string s; cin >> s; auto cat = [&](int n, bool inv = false) -> mint { if (inv) return binom.ifact[2 * n] * binom.fact[n] * binom.fact[n + 1]; return binom.fact[2 * n] * binom.ifact[n] * binom.ifact[n + 1]; }; // +1:p,-1;q auto rastY = [&](int p, int q)->mint { if (p < 0 || q < 0 || p < q)return 0; return binom(p + q, p) - binom(p + q, p + 1); }; // +1:p,-1;q auto rastN = [&](int p, int q)->mint { if (p < 0 || q < 0 || p > q)return 0; return binom(p + q, q) - binom(p + q, q + 1); }; mint ans = 1;// 最終ブロックは自前で計算しろ int ngcnt = 0; auto add = [&](int l, int r, char c) -> void { if (r == n)return; int sz = r - l; if (sz % 2 == 0) ans *= cat(sz / 2); else ngcnt++; }; auto del = [&](int l, int r, char c) -> void { if (r == n)return; int sz = r - l; if (sz % 2 == 0) ans *= cat(sz / 2, true); else ngcnt--; }; kwm_t::data_structure::IntervalMap mp(s, add, del); while (q--) { int t; cin >> t; if (t == 1) { int i; cin >> i; i--; if (s[i] == 'Y') s[i] = 'N'; else s[i] = 'Y'; mp.insertPoint(i, s[i], add, del); } else { int k; cin >> k; if (ngcnt) { cout << 0 << endl; continue; } auto range = mp.get(n - 1).value(); k -= range.l / 2; if (range.val == 'Y') { mint val = ans * rastY(range.r - range.l - k, k); cout << val.val() << endl; } else { mint val = ans * rastN(range.r - range.l - k, k); cout << val.val() << endl; } } } return 0; }