// #define _GLIBCXX_DEBUG #include // clang-format off std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,const __int128_t &u){if(!u)os<<"0";__int128_t tmp=u<0?(os<<"-",-u):u;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os< template constexpr inline Int mod_inv(Int a, Int mod) { static_assert(std::is_signed_v); Int x= 1, y= 0, b= mod; for (Int q= 0, z= 0; b;) z= x, x= y, y= z - y * (q= a / b), z= a, a= b, b= z - b * q; return assert(a == 1), x < 0 ? mod - (-x) % mod : x % mod; } namespace math_internal { using namespace std; using u8= unsigned char; using u32= unsigned; using i64= long long; using u64= unsigned long long; using u128= __uint128_t; #define CE constexpr #define IL inline #define NORM \ if (n >= mod) n-= mod; \ return n #define PLUS(U, M) \ CE IL U plus(U l, U r) const { return l+= r, l < (M) ? l : l - (M); } #define DIFF(U, C, M) \ CE IL U diff(U l, U r) const { return l-= r, l >> C ? l + (M) : l; } #define SGN(U) \ static CE IL U set(U n) { return n; } \ static CE IL U get(U n) { return n; } \ static CE IL U norm(U n) { return n; } template struct MP_Mo { u_t mod; CE MP_Mo(): mod(0), iv(0), r2(0) {} CE MP_Mo(u_t m): mod(m), iv(inv(m)), r2(-du_t(mod) % mod) {} CE IL u_t mul(u_t l, u_t r) const { return reduce(du_t(l) * r); } PLUS(u_t, mod << 1) DIFF(u_t, A, mod << 1) CE IL u_t set(u_t n) const { return mul(n, r2); } CE IL u_t get(u_t n) const { n= reduce(n); NORM; } CE IL u_t norm(u_t n) const { NORM; } private: u_t iv, r2; static CE u_t inv(u_t n, int e= 6, u_t x= 1) { return e ? inv(n, e - 1, x * (2 - x * n)) : x; } CE IL u_t reduce(const du_t &w) const { return u_t(w >> B) + mod - ((du_t(u_t(w) * iv) * mod) >> B); } }; struct MP_Na { u32 mod; CE MP_Na(): mod(0){}; CE MP_Na(u32 m): mod(m) {} CE IL u32 mul(u32 l, u32 r) const { return u64(l) * r % mod; } PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) }; struct MP_Br { // mod < 2^31 u32 mod; CE MP_Br(): mod(0), s(0), x(0) {} CE MP_Br(u32 m): mod(m), s(95 - __builtin_clz(m - 1)), x(((u128(1) << s) + m - 1) / m) {} CE IL u32 mul(u32 l, u32 r) const { return rem(u64(l) * r); } PLUS(u32, mod) DIFF(u32, 31, mod) SGN(u32) private: u8 s; u64 x; CE IL u64 quo(u64 n) const { return (u128(x) * n) >> s; } CE IL u32 rem(u64 n) const { return n - quo(n) * mod; } }; struct MP_Br2 { // 2^20 < mod <= 2^41 u64 mod; CE MP_Br2(): mod(0), x(0) {} CE MP_Br2(u64 m): mod(m), x((u128(1) << 84) / m) {} CE IL u64 mul(u64 l, u64 r) const { return rem(u128(l) * r); } PLUS(u64, mod << 1) DIFF(u64, 63, mod << 1) static CE IL u64 set(u64 n) { return n; } CE IL u64 get(u64 n) const { NORM; } CE IL u64 norm(u64 n) const { NORM; } private: u64 x; CE IL u128 quo(const u128 &n) const { return (n * x) >> 84; } CE IL u64 rem(const u128 &n) const { return n - quo(n) * mod; } }; struct MP_D2B1 { u8 s; u64 mod, d, v; CE MP_D2B1(): s(0), mod(0), d(0), v(0) {} CE MP_D2B1(u64 m): s(__builtin_clzll(m)), mod(m), d(m << s), v(u128(-1) / d) {} CE IL u64 mul(u64 l, u64 r) const { return rem((u128(l) * r) << s) >> s; } PLUS(u64, mod) DIFF(u64, 63, mod) SGN(u64) private: CE IL u64 rem(const u128 &u) const { u128 q= (u >> 64) * v + u; u64 r= u64(u) - (q >> 64) * d - d; if (r > u64(q)) r+= d; if (r >= d) r-= d; return r; } }; template CE u_t pow(u_t x, u64 k, const MP &md) { for (u_t ret= md.set(1);; x= md.mul(x, x)) if (k & 1 ? ret= md.mul(ret, x) : 0; !(k>>= 1)) return ret; } #undef NORM #undef PLUS #undef DIFF #undef SGN #undef CE } namespace math_internal { struct m_b {}; struct s_b: m_b {}; } template constexpr bool is_modint_v= std::is_base_of_v; template constexpr bool is_staticmodint_v= std::is_base_of_v; namespace math_internal { #define CE constexpr template struct SB: s_b { protected: static CE MP md= MP(MOD); }; template struct MInt: public B { using Uint= U; static CE inline auto mod() { return B::md.mod; } CE MInt(): x(0) {} template && !is_same_v>> CE MInt(T v): x(B::md.set(v.val() % B::md.mod)) {} CE MInt(__int128_t n): x(B::md.set((n < 0 ? ((n= (-n) % B::md.mod) ? B::md.mod - n : n) : n % B::md.mod))) {} CE MInt operator-() const { return MInt() - *this; } #define FUNC(name, op) \ CE MInt name const { \ MInt ret; \ return ret.x= op, ret; \ } FUNC(operator+(const MInt & r), B::md.plus(x, r.x)) FUNC(operator-(const MInt & r), B::md.diff(x, r.x)) FUNC(operator*(const MInt & r), B::md.mul(x, r.x)) FUNC(pow(u64 k), math_internal::pow(x, k, B::md)) #undef FUNC CE MInt operator/(const MInt& r) const { return *this * r.inv(); } CE MInt& operator+=(const MInt& r) { return *this= *this + r; } CE MInt& operator-=(const MInt& r) { return *this= *this - r; } CE MInt& operator*=(const MInt& r) { return *this= *this * r; } CE MInt& operator/=(const MInt& r) { return *this= *this / r; } CE bool operator==(const MInt& r) const { return B::md.norm(x) == B::md.norm(r.x); } CE bool operator!=(const MInt& r) const { return !(*this == r); } CE bool operator<(const MInt& r) const { return B::md.norm(x) < B::md.norm(r.x); } CE inline MInt inv() const { return mod_inv(val(), B::md.mod); } CE inline Uint val() const { return B::md.get(x); } friend ostream& operator<<(ostream& os, const MInt& r) { return os << r.val(); } friend istream& operator>>(istream& is, MInt& r) { i64 v; return is >> v, r= MInt(v), is; } private: Uint x; }; template using ModInt= conditional_t < (MOD < (1 << 30)) & MOD, MInt, MOD>>, conditional_t < (MOD < (1ull << 62)) & MOD, MInt, MOD>>, conditional_t>, conditional_t>, conditional_t>, MInt>>>>>>; #undef CE } using math_internal::ModInt; template class UnionFind { std::vector par; std::vector val; public: UnionFind(int n): par(n, -1), val(n) {} int leader(int u) { if (par[u] < 0) return u; int r= leader(par[u]); if constexpr (std::is_same_v) val[u]= val[u] ^ val[par[u]]; else val[u]+= val[par[u]]; return par[u]= r; } // p(v) - p(u) = w bool unite(int u, int v, weight_t w) { int u_= leader(u), v_= leader(v); if constexpr (std::is_same_v) w^= val[u] ^ val[v]; else w+= val[u] - val[v]; if (u_ == v_) return w == weight_t(); if (par[u_] > par[v_]) std::swap(u_, v_), w= -w; return par[u_]+= par[v_], par[v_]= u_, val[v_]= w, true; } bool connected(int u, int v) { return leader(u) == leader(v); } int size(int u) { return -par[leader(u)]; } weight_t potential(int u) { return leader(u), val[u]; } // p(v) - p(u) weight_t diff(int u, int v) { if constexpr (std::is_same_v) return potential(u) ^ potential(v); else return potential(v) - potential(u); } }; template <> class UnionFind { std::vector par; public: UnionFind(int n): par(n, -1) {} int leader(int u) { return par[u] < 0 ? u : par[u]= leader(par[u]); } bool unite(int u, int v) { if ((u= leader(u)) == (v= leader(v))) return false; if (par[u] > par[v]) std::swap(u, v); return par[u]+= par[v], par[v]= u, true; } bool connected(int u, int v) { return leader(u) == leader(v); } int size(int u) { return -par[leader(u)]; } }; template class UnionFind { std::vector par; std::vector val; std::vector> his; public: UnionFind(int n): par(n, -1), val(n) {} int leader(int u) const { return par[u] < 0 ? u : leader(par[u]); } // p(v) - p(u) = w bool unite(int u, int v, T w) { int u_= leader(u), v_= leader(v); if constexpr (std::is_same_v) w^= val[u] ^ val[v]; else w+= val[u] - val[v]; if (u_ == v_) return w == T(); if (par[u_] > par[v_]) std::swap(u_, v_), w= -w; return his.emplace_back(v_, par[v_], val[v_]), par[u_]+= par[v_], par[v_]= u_, val[v_]= w, true; } bool connected(int u, int v) const { return leader(u) == leader(v); } int size(int u) const { return -par[leader(u)]; } T potential(int u) { if constexpr (std::is_same_v) return par[u] < 0 ? val[u] : val[u] ^ potential(par[u]); else return par[u] < 0 ? val[u] : val[u] + potential(par[u]); } // p(v) - p(u) T diff(int u, int v) { if constexpr (std::is_same_v) return potential(v) ^ potential(u); else return potential(v) - potential(u); } int time() const { return his.size(); } void undo() { if (his.empty()) return; auto [u, s, v]= his.back(); his.pop_back(), par[par[u]]-= s, par[u]= s, val[u]= v; } void rollback(int t) { for (assert(t <= time()); time() > t;) undo(); } }; template <> class UnionFind { std::vector par; std::vector> his; public: UnionFind(int n): par(n, -1) {} int leader(int u) const { return par[u] < 0 ? u : leader(par[u]); } bool unite(int u, int v) { if ((u= leader(u)) == (v= leader(v))) return false; if (par[u] > par[v]) std::swap(u, v); return his.emplace_back(v, par[v]), par[u]+= par[v], par[v]= u, true; } bool connected(int u, int v) const { return leader(u) == leader(v); } int size(int u) const { return -par[leader(u)]; } int time() const { return his.size(); } void undo() { if (his.empty()) return; auto [u, s]= his.back(); his.pop_back(), par[par[u]]-= s, par[u]= s; } void rollback(int t) { for (assert(t <= time()); time() > t;) undo(); } }; using namespace std; signed main() { cin.tie(0); ios::sync_with_stdio(0); using Mint= ModInt<998244353>; int N, Q; cin >> N >> Q; UnionFind uf(N); bool isok= true; for (int i= 0; i < Q; ++i) { int A, B, C; cin >> A >> B >> C, --A, --B; isok&= uf.unite(A, B, C); } if (!isok) return cout << 0 << '\n', 0; int cnt= 0; for (int i= 0; i < N; ++i) cnt+= uf.leader(i) == i; cout << Mint(2).pow(cnt) << '\n'; return 0; }