#include using namespace std; using ll = long long; #ifdef MY_DEBUG #define dbg(...) debug_out(__VA_ARGS__) #define dbgn(var) debug_out(#var, "=", var) #else #define dbg(...) #define dbgn(...) #endif void debug_out(); template void debug_out(Head, Tail...); template bool chmax(T&, const T&); template bool chmin(T&, const T&); template T pwr(T, ll); template T squ(T x) { return x * x; } ll fact(ll); ll comb(ll, ll); ll ctoll(char); char lltoc(ll); bool flg(ll b, ll i) { return ((b) >> (i)) & 1LL; } // flg(0b010, 1LL) -> true const ll LINF = (ll)1e18 + 7; const double EPS = 1e-9; const int MAX_DUBUG_SIZE = 10; // 重み付きUnionFind struct weighted_union_find { vector par, rank, siz; // par: parent, rank: depth, siz: size of group vector diff_weight; ll group_num; // 連結成分の個数 weighted_union_find(ll n) : par(n, -1), rank(n, 0), siz(n, 1), diff_weight(n, false), group_num(n) {} ll root(ll x) { if (par[x] == -1) { return x; } else { ll root_id = root(par[x]); diff_weight[x] = (diff_weight[x] ^ diff_weight[par[x]]); par[x] = root_id; // 経路圧縮,帰りがけに根に直接つなぐ return par[x]; } } bool is_same(ll x, ll y) { return root(x) == root(y); } bool weight(ll x) { root(x); return diff_weight[x]; } // unite by rank, x の根の下に y の根をつなぐ // w = weight(y) ^ weight(x) を満たすように // 同じ <-> w = false bool unite(ll x, ll y, bool w) { w = (w ^ weight(x)); w = (w ^ weight(y)); ll rx = root(x), ry = root(y); if (rx == ry) { return false; } if (rank[rx] < rank[ry]) { swap(rx, ry); } if (rank[rx] == rank[ry]) { rank[rx]++; } par[ry] = rx; siz[rx] += siz[ry]; siz[ry] = 0; diff_weight[ry] = w; group_num--; return true; } ll size(ll x) { return siz[root(x)]; } bool diff(ll x, ll y) { return weight(y) ^ weight(x); } }; #include #include using namespace atcoder; // const int MOD = 1e9 + 7; // using mint = modint1000000007; const int MOD = 998244353; using mint = modint998244353; // using mint = modint; // main の最初で mint::setmod(/* 任意のMOD */); ostream& operator<<(ostream& os, const mint& N) { return os << N.val(); } // // xz = y mod MOD を満たす既約分数 y/x を求める // string mint_to_ratio(mint z) { // string res = "NaN"; // if (z == 0) { // res = "0/1"; // return res; // } // const ll MAX = 1000; // for (ll x = 1; x <= MAX; x++) { // for (ll y = 1; y <= MAX; y++) { // mint tmp = x * z - y; // if (tmp == 0) { // res = to_string(y) + "/" + to_string(x); // return res; // } // } // } // return res; // } void solve([[maybe_unused]] int test) { ll n, q; cin >> n >> q; vector a(q), b(q), c(q); for (ll i = 0; i < q; i++) { cin >> a[i] >> b[i] >> c[i]; a[i]--, b[i]--; // 0-indexed } // Aren't they one line? weighted_union_find wuf(n); for (ll i = 0; i < q; i++) { if (wuf.is_same(a[i], b[i])) { if (wuf.diff(a[i], b[i]) != c[i]) { cout << 0 << endl; return; } } else { wuf.unite(a[i], b[i], c[i]); } } // for (ll i = 0; i < q; i++) { // if ((wuf.weight(a[i]) ^ wuf.weight(b[i])) != c[i]) { // cout << 0 << endl; // return; // } // } dbg(wuf.diff_weight); for (ll i = 0; i < n; i++) { dbg(wuf.weight(i)); } mint ans = mint(2).pow(wuf.group_num); cout << ans << endl; } int main() { cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12); int testcase_size = 1; // cin >> testcase_size; for (int _t = 0; _t < testcase_size; _t++) { solve(_t); } } /* -----------------------------------MY_FUNCTIONS------------------------------------ */ template ostream& operator<<(ostream& os, const pair& pp) { return os << "{" << pp.first << "," << pp.second << "}"; } template ostream& operator<<(ostream& os, const vector& V) { if (V.empty()) return os << "[]"; os << "["; for (ll i = 0; i < (ll)V.size(); i++) { os << V[i] << (i == int(V.size() - 1) ? "]" : ","); } return os; } template ostream& operator<<(ostream& os, const vector>& VV) { if (VV.empty()) return os << "[[]]"; os << "[\n"; for (auto&& V : VV) { os << V << "\n"; } os << "]"; return os; } template ostream& operator<<(ostream& os, const vector>>& VVV) { if (VVV.empty()) return os << "[[[]]]"; os << "[" << "\n"; int cnt = 0; for (auto&& VV : VVV) { os << cnt++ << VV << "\n\n"; } os << "]"; return os; } template ostream& operator<<(ostream& os, const set& SS) { if (SS.empty()) return os << "[]"; os << "["; auto ii = SS.begin(); for (; ii != SS.end(); ii++) os << *ii << (ii == prev(SS.end()) ? "]" : ","); return os; } template ostream& operator<<(ostream& os, const map& MM) { if (MM.empty()) return os << "[{:}]"; os << "["; auto ii = MM.begin(); for (; ii != MM.end(); ii++) os << "{" << ii->first << ":" << ii->second << "}" << (ii == prev(MM.end()) ? "]" : ","); return os; } void debug_out() { cerr << endl; } void debug_out_vl(vector V) { const int MAX_SIZE = min((int)V.size(), MAX_DUBUG_SIZE); cerr << "\033[33m"; if (V.empty()) { cerr << "[]" << endl; return; } cerr << "["; for (int i = 0; i < MAX_SIZE; i++) { if (V[i] == LINF) cerr << "INF"; else cerr << V[i]; if (i == (int)V.size() - 1) cerr << "]\n"; else if (i == MAX_DUBUG_SIZE - 1) cerr << ",...\n"; else cerr << ","; } return; } void debug_out_vvl(vector> VV) { cerr << "\033[33m"; if (VV.empty()) { cerr << "[[]]" << endl; return; } cerr << "[\n"; int MAX_ROW = min((int)VV.size(), MAX_DUBUG_SIZE); for (int i = 0; i < MAX_ROW; i++) { const int MAX_COLUMN = min((int)VV[i].size(), MAX_DUBUG_SIZE); if (VV[i].empty()) { cerr << "[]" << endl; continue; } cerr << "["; for (int j = 0; j < MAX_COLUMN; j++) { if (VV[i][j] == LINF) cerr << "INF"; else cerr << VV[i][j]; if (j == (int)VV[i].size() - 1) cerr << "]\n"; else if (j == MAX_DUBUG_SIZE - 1) cerr << ",...\n"; else cerr << ","; } if (i != (int)VV.size() - 1 and i == MAX_DUBUG_SIZE - 1) { cerr << ":\n:\033[m\n"; return; } } cerr << "]\033[m\n"; return; } template void debug_out(Head H, Tail... T) { if constexpr (std::is_same_v>) { debug_out_vl(H); } else if constexpr (std::is_same_v>>) { debug_out_vvl(H); } else { cerr << "\033[33m" << H << "\033[m "; } debug_out(T...); } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } return false; } template T pwr(T x, ll n) { T res = 1; for (int i = 0; i < n; i++) { res *= x; } return res; } ll fact(ll n) { ll res = 1; for (ll i = 1; i <= n; i++) res *= i; return res; } ll comb(ll n, ll r) { // comb(60, 30)までオーバーフローなし ll res = 1; for (int i = 1; i <= r; i++) { res *= n--; res /= i; } return res; } ll ctoll(char c) { if (c < '0' or '9' < c) { cerr << "\n\033[33m ctoll に '0'~'9' 以外の文字が入りました.\033[m\n" << endl; } return ll(c - '0'); } char lltoc(ll n) { if (n < 0 or 9 < n) { cerr << "\n\033[33m lltoc に 0 ~ 9 以外の数字が入りました.\033[m\n" << endl; } return char(n + '0'); }