#include using namespace std; #include using namespace atcoder; using ll = long long; using ld = long double; using P = pair; using Graph = vector>; using vi = vector; using vl = vector; using vll = vector; using vvi = vector; using vvl = vector; using vvll = vector; using vs = vector; using vc = vector; using vvc = vector; using pll = pair; using vpll = vector; using mint = modint998244353; const long double EPS = 1e-18; const long long INF = 1e18; const long double PI = acos(-1.0L); #define reps(i, a, n) for (ll i = (a); i < (ll)(n); i++) #define rep(i, n) for (ll i = (0); i < (ll)(n); i++) #define rrep(i, n) for (ll i = (1); i < (ll)(n + 1); i++) #define repd(i, n) for (ll i = n - 1; i >= 0; i--) #define rrepd(i, n) for (ll i = n; i >= 1; i--) #define ALL(n) begin(n), end(n) #define IN(a, x, b) (a <= x && x < b) #define INIT \ std::ios::sync_with_stdio(false); \ std::cin.tie(0); template inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } struct RollbackUnionFind { vector data; stack> history; int inner_snap; RollbackUnionFind(int sz) : inner_snap(0) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); history.emplace(x, data[x]); history.emplace(y, data[y]); if (x == y) return false; if (data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return true; } int find(int k) { if (data[k] < 0) return k; return find(data[k]); } int same(int x, int y) { return find(x) == find(y); } int size(int k) { return (-data[find(k)]); } void undo() { data[history.top().first] = history.top().second; history.pop(); data[history.top().first] = history.top().second; history.pop(); } void snapshot() { inner_snap = int(history.size() >> 1); } int get_state() { return int(history.size() >> 1); } void rollback(int state = -1) { if (state == -1) state = inner_snap; state <<= 1; assert(state <= (int)history.size()); while (state < (int)history.size()) undo(); } }; /** * @brief RollbackつきUnion Find * @docs docs/data-structure/rollback-union-find.md */ int main() { ll N, Q; cin >> N >> Q; RollbackUnionFind uf(4e5 + 50); uf.snapshot(); mint ans = 0; mint def = mint(2).pow(N); ans = def; while (Q--) { ll query; cin >> query; if (query == 1) { ll x, y; cin >> x >> y; if (!uf.same(x, y)) { ans /= 2; } uf.unite(x, y); uf.unite(x + N, y + N); if (uf.same(x, x + N) || uf.same(y, y + N)) { ans = 0; } } else if (query == 2) { ll x, y; cin >> x >> y; if (!uf.same(x, y + N)) { ans /= 2; } uf.unite(x, y + N); uf.unite(x + N, y); if (uf.same(x, x + N) || uf.same(y, y + N)) { ans = 0; } } else { uf.rollback(-1); ans = def; } cout << ans.val() << endl; } }