#if !__INCLUDE_LEVEL__ #include __FILE__ template struct Fp { long long val; constexpr Fp(long long v = 0) noexcept : val(v % MOD) { if (val < 0) val += MOD; } constexpr int getmod() { return MOD; } constexpr Fp operator - () const noexcept { return val ? MOD - val : 0; } constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; } constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; } constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; } constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; } constexpr Fp& operator += (const Fp& r) noexcept { val += r.val; if (val >= MOD) val -= MOD; return *this; } constexpr Fp& operator -= (const Fp& r) noexcept { val -= r.val; if (val < 0) val += MOD; return *this; } constexpr Fp& operator *= (const Fp& r) noexcept { val = val * r.val % MOD; return *this; } constexpr Fp& operator /= (const Fp& r) noexcept { long long a = r.val, b = MOD, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } constexpr bool operator == (const Fp& r) const noexcept { return this->val == r.val; } constexpr bool operator != (const Fp& r) const noexcept { return this->val != r.val; } friend constexpr ostream& operator << (ostream &os, const Fp& x) noexcept { return os << x.val; } static constexpr Fp modpow(const Fp &a, long long n) noexcept { if (n == 0) return 1; auto t = modpow(a, n / 2); t = t * t; if (n & 1) t = t * a; return t; } }; // const int MOD = 1000000007; const int MOD = 998244353; using mint = Fp; struct Solver { void solve() { int n,q;cin>>n>>q; dsu d(n); V> graph(n); rep(i,q){ int a,b,c;cin>>a>>b>>c; a--;b--; graph[a].emplace_back(b,c); d.merge(a,b); } auto v=d.groups(); mint ans=1; rep(i,sz(v)){ map mp; deque dq; dq.emplace_back(v[i][0]); mp[v[i][0]] = 0; while(sz(dq)){ auto now=dq.front(); dq.pop_front(); for(auto [next,g]:graph[now]){ if(mp.count(next)){ if(mp[now]==0 && mp[next]==g)continue; if(mp[now]==1 && mp[next]!=g)continue; else ans=0; } else{ if(mp[now]==0)mp[next]=g; else mp[next]=1-g; dq.emplace_back(next); } } } ans*=2; } cout< #include using namespace atcoder; // #include // #include // using namespace __gnu_pbds; using namespace std; #define int ll using ll=long long; using ld = long double; using uint = unsigned; using ull = unsigned long long; using P=pair; using TP=tuple; const int INF = 1001001001; const long long INFL = 3e18; const int MAX = 2e6+5; #define FOR1(a) for (ll _ = 0; _ < ll(a); ++_) #define FOR2(i, a) for (ll i = 0; i < ll(a); ++i) #define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i) #define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c)) #define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i) #define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i) #define FOR4_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= (c)) #define overload4(a, b, c, d, e, ...) e #define rep(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__) #define rrep(...) overload4(__VA_ARGS__, FOR4_R, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__) #define fore(i,a) for(auto &i:a) #define all(x) x.begin(),x.end() #define allr(x) x.rbegin(),x.rend() #define sz(x) ((int)(x).size()) #define bp(x) (__builtin_popcountll((long long)(x))) #define elif else if #define mpa make_pair #define bit(n) (1LL<<(n)) #define LB(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define UB(c, x) distance((c).begin(), upper_bound(all(c), (x))) template using V = vector; template using max_heap = priority_queue; template using min_heap = priority_queue, greater<>>; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (bstring join(const T&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<ostream& operator<<(ostream&o,const vector&v){if(sz(v))o<ostream& operator<<(ostream&o,const pair&v){return o<Tx dup(Tx x, Ty y){return (x+y-1)/y;} templatell suma(const vector&a){ll res(0);for(auto&&x:a)res+=x;return res;} templatell suma(const V>&a){ll res(0);for(auto&&x:a)res+=suma(x);return res;} templatevoid uni(T& a){sort(all(a));a.erase(unique(all(a)),a.end());} templatevoid prepend(vector&a,const T&x){a.insert(a.begin(),x);} templateT vgcd(T m, T n) {return std::gcd(m, n);} templateT vgcd(T a, Args... args) {return vgcd(a, vgcd(args...));} void print(){ putchar(' '); } void print(bool a){ printf("%d", a); } // void print(int a){ printf("%d", a); } void print(unsigned a){ printf("%u", a); } void print(long a){ printf("%ld", a); } void print(long long a){ printf("%lld", a); } void print(unsigned long long a){ printf("%llu", a); } void print(char a){ printf("%c", a); } void print(char *a){ printf("%s", a); } void print(const char *a){ printf("%s", a); } void print(double a){ printf("%.15f", a); } void print(long double a){ printf("%.15Lf", a); } void print(const string& a){ for(auto&& i : a) print(i); } template void print(const complex& a){ if(a.real() >= 0) print('+'); print(a.real()); if(a.imag() >= 0) print('+'); print(a.imag()); print('i'); } template void print(const vector&); template void print(const array&); template void print(const pair& p); template void print(const T (&)[size]); template void print(const vector& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } } template void print(const deque& a){ if(a.empty()) return; print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } } template void print(const array& a){ print(a[0]); for(auto i = a.begin(); ++i != a.end(); ){ putchar(' '); print(*i); } } template void print(const pair& p){ print(p.first); putchar(' '); print(p.second); } template void print(const T (&a)[size]){ print(a[0]); for(auto i = a; ++i != end(a); ){ putchar(' '); print(*i); } } template void print(const T& a){ cout << a; } int out(){ putchar('\n'); return 0; } template int out(const T& t){ print(t); putchar('\n'); return 0; } template int out(const Head& head, const Tail&... tail){ print(head); putchar(' '); out(tail...); return 0; } ll binary_search(function check, ll ok, ll ng) { assert(check(ok)); while (abs(ok - ng) > 1) { auto x = (ng + ok) / 2; if (check(x)) ok = x; else ng = x; } return ok; } #endif