#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template <uint MD> struct ModInt {
    using M = ModInt;
    uint v;
    ModInt() : v{0} {}
    ModInt(ll _v) { set_v(_v % MD + MD); }
    M& set_v(uint _v) {
        v = (_v < MD) ? _v : _v - MD;
        return *this;
    }
    explicit operator bool() const { return v != 0; }
    M operator-() const { return M(0) - *this; }
    M operator+(const M& r) const { return M().set_v(v + r.v); }
    M operator-(const M& r) const { return M().set_v(v + MD - r.v); }
    M operator*(const M& r) const { return M().set_v(ull(v) * r.v % MD); }
    M operator/(const M& r) const { return *this * r.inv(); }
    M& operator+=(const M& r) { return *this = *this + r; }
    M& operator-=(const M& r) { return *this = *this - r; }
    M& operator*=(const M& r) { return *this = *this * r; }
    M& operator/=(const M& r) { return *this = *this / r; }
    M pow(ll n) const {
        M x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    M inv() const { return (*this).pow(MD - 2); }
    friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; }
};
using Mint = ModInt<998244353>;

using P = pair<int, int>;

void imp() {
    cout << 0 << endl;
    exit(0);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    map<P, V<int>> mp;
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c; a--; b--;
        if (a > b) swap(a, b);
        mp[P(a, b)].push_back(c);
    }
    V<int> cen(n, -1), two(n-1, -1);
    Mint ans = Mint(2).pow(ll(n) * (n - 1) / 2);
    for (auto p: mp) {
        int a = p.first.first, b = p.first.second;
        assert(a <= b);
        int c = int(p.second.size()), s = 0;
        for (int d: p.second) s ^= d;
        if (a == b) {
            assert(c == 1);
            if (cen[a] != -1 && cen[a] != s) {
                imp();
            }
            cen[a] = s;
            continue;
        }
        ans /= Mint(2);
        if (c == 1) continue;
        assert(c == 2);
        if (a + 1 == b) {
            two[a] = s;
            continue;
        }
        if (a + 2 == b) {
            if (cen[a + 1] != -1 && cen[a + 1] != s) {
                imp();
            }
            cen[a+1] = s;
            continue;
        }
        if (s) imp();
    }
    /*cout << ans << endl;
    for (int d: cen) cout << d << ", ";
    cout << endl;
    for (int d: two) cout << d << ", ";
    cout << endl;*/

    Mint ans0 = 0, ans1 = 0;
    if (cen[0] != 1) ans0 = 1;
    if (cen[0] != 0) ans1 = 1;
    for (int i = 1; i < n; i++) {
        Mint nans0 = 0, nans1 = 0;
        if (cen[i] != 1) {
            if (two[i - 1] != 1) nans0 += ans0;
            if (two[i - 1] != 0) nans0 += ans1;
        }
        if (cen[i] != 0) {
            if (two[i - 1] != 0) nans1 += ans0;
            if (two[i - 1] != 1) nans1 += ans1;
        }
        ans0 = nans0; ans1 = nans1;
    }
    //cerr << ans << " " << ans0 << " " << ans1 << endl;
    cout << ans * (ans0 + ans1) << endl;
    return 0;
}


Measured memory_bytes: 6528