#include #include using namespace std; using mint = atcoder::modint998244353; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } class RollbackUnionFind { private: vector par_or_rank; vector> history_par; int n; public: RollbackUnionFind(int sz_) : n(sz_), par_or_rank(sz_, -1), history_par(0), ans(0) {} int ans; int leader(int a) { if (par_or_rank[a] < 0) { return a; } return leader(par_or_rank[a]); } bool same(int a, int b) { return leader(a) == leader(b); } void merge(int a, int b) { a = leader(a); b = leader(b); history_par.push_back({a, par_or_rank[a]}); history_par.push_back({b, par_or_rank[b]}); if (a == b) { return; } if (-par_or_rank[a] < -par_or_rank[b]) { swap(a, b); } par_or_rank[a] += par_or_rank[b]; par_or_rank[b] = a; } void undo() { par_or_rank[history_par.back().first] = history_par.back().second; history_par.pop_back(); par_or_rank[history_par.back().first] = history_par.back().second; history_par.pop_back(); } void clear() { while (history_par.size()) { undo(); } } }; int main() { fast_io(); int n, q; cin >> n >> q; vector p2(n + 1, 1); for (int i = 1; i <= n; i++) { p2[i] = p2[i - 1] * 2; } mint inv_2 = mint(2).inv(); RollbackUnionFind uf(2 * n); mint ans = p2[n]; for (; q--;) { int type; cin >> type; if (type == 1) { int u, v; cin >> u >> v; u--, v--; if (!uf.same(u, v)) { uf.merge(u, v); uf.merge(u + n, v + n); if (uf.same(u, u + n) || uf.same(v, v + n)) { ans = 0; } else { ans *= inv_2; } } } if (type == 2) { int u, v; cin >> u >> v; u--, v--; if (!uf.same(u, v + n)) { uf.merge(u, v + n); uf.merge(u + n, v); if (uf.same(u, u + n) || uf.same(v, v + n)) { ans = 0; } else { ans *= inv_2; } } } if (type == 3) { ans = p2[n]; uf.clear(); } cout << ans.val() << "\n"; } }