#include <iostream>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const ll MOD = 998244353;

ll modpow(ll x, ll n){
    ll res = 1, r = x;
    while(n){
        if(n % 2) res = res * r % MOD;
        r = r * r % MOD;
        n /= 2;
    }
    return res;
}

int main()
{
    ll n;
    int m;
    cin >> n >> m;
    int a[50005], b[50005], c[50005];
    int d[100002];
    fill(d, d + n, -1);
    int e[100002];
    fill(e, e + n, -1);
    for(int i = 0; i < m; i++){
        cin >> a[i] >> b[i] >> c[i];
        a[i]--; b[i]--;
        if(a[i] == b[i]) d[a[i]] = c[i];
    }
    map<P, int> mp;
    ll k = n * (n - 1) / 2;
    for(int i = 0; i < m; i++){
        if(a[i] == b[i]) continue;
        int j = mp[P(b[i], a[i])] - 1;
        if(j >= 0){
            if(abs(a[i] - b[i]) > 2){
                if(c[i] != c[j]){
                    cout << 0 << endl;
                    return 0;
                }
            }
            if(abs(a[i] - b[i]) == 2){
                if(d[(a[i] + b[i]) / 2] == -1) d[(a[i] + b[i]) / 2] = (c[i] + c[j]) % 2;
                else if(d[(a[i] + b[i]) / 2] != (c[i] + c[j]) % 2){
                    cout << 0 << endl;
                    return 0;
                }
            }
            if(abs(a[i] - b[i]) == 1){
                e[max(a[i], b[i])] = (c[i] + c[j]) % 2;
            }
        }
        else{
            mp[P(a[i], b[i])] = i + 1;
            k--;
        }
    }
    queue<int> que;
    for(int i = 0; i < n; i++){
        if(d[i] != -1) que.push(i);
    }
    while(que.size()){
        int i = que.front();
        que.pop();
        if(i > 0 && e[i] != -1){
            if(d[i - 1] == -1){
                d[i - 1] = (d[i] + e[i]) % 2;
                que.push(i - 1);
            }
            else if(d[i - 1] != (d[i] + e[i]) % 2){
                cout << 0 << endl;
                return 0;
            }
        }
        if(i < n - 1 && e[i + 1] != -1){
            if(d[i + 1] == -1){
                d[i + 1] = (d[i] + e[i + 1]) % 2;
                que.push(i + 1);
            }
            else if(d[i + 1] != (d[i] + e[i + 1]) % 2){
                cout << 0 << endl;
                return 0;
            }
        }
    }
    for(int i = 1; i < n; i++){
        if(e[i] != -1 && d[i] == -1) k--;
    }
    for(int i = 0; i < n; i++){
        if(d[i] == -1) k++;
    }
    cout << modpow(2, k) << endl;
}
