#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

ll mod_pow(ll x, ll n, ll mod){ // x ^ n % mod
    ll res = 1;
    while(n > 0){
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

// ax + by = gcd(a, b) となるような (x, y) を求める
// a と b は互いに素として ax + by = 1 となる (x, y) を求める
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x); // 再帰
    y -= a / b * x;
    return d;
}


// 負の数に対応した mod
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}


// 逆元計算 (a と m が互いに素であることが必要)
long long modinv(long long a, long long m) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m); // x % m だが、x が負かもしれないので
}

int f[110000][3][2];

int main() {
    int n, m;
    cin >> n >> m;
    
    rep(i,n)rep(j,3)rep(k,2)f[i][j][k] = 0;
    map<P,int> mp; 
    int ng = 0;
    rep(i,m){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        if(a > b) swap(a,b);
        if(a == b){
            f[b][0][c]++;
        }else if(b - a == 1){
            f[b][1][c]++;
        }else if(b - a == 2){
            f[b][2][c]++;
        }else{
            if(mp.find(P(a,b)) != mp.end()){
                if(mp[P(a,b)] != c){
                    ng = 1;
                }
            }else{
                mp[P(a,b)] = c;
            }
        }
    }

    if(ng){
        cout << 0 << endl;
        return 0;
    }

    int g[n] = {};
    for(auto p : mp){
        int x = p.first.second;
        g[x]++;
    }

    ll dp[n][2];
    const ll MOD = 998244353;
    rep(i,n)rep(j,2) dp[i][j] = 0;
    if(f[0][0][0] == 1){
        dp[0][0] = 1;
    }else if(f[0][0][1] == 1){
        dp[0][1] = 1;
    }else{
        dp[0][0] = 1;
        dp[0][1] = 1;
    }

    srep(i,1,n){
        ll tmp00 = dp[i-1][0], tmp01 = dp[i-1][0];
        ll tmp10 = dp[i-1][1], tmp11 = dp[i-1][1];

        int cnt1 = f[i][1][0] + f[i][1][1];
        if(cnt1 == 0){
            tmp00 *= 2; tmp01 *= 2;
            tmp10 *= 2; tmp11 *= 2;
        }else if(cnt1 == 1){
            ;
        }else if(cnt1 == 2){
            if(f[i][1][0]==2 || f[i][1][1]==2){
                tmp01 = 0;
                tmp10 = 0;
            }else{
                tmp00 = 0;
                tmp11 = 0;
            }
        }

        if(i >= 2){
            int cnt2 = f[i][2][0] + f[i][2][1];
            if(cnt2 == 0){
                tmp00 *= 2; tmp01 *= 2;
                tmp10 *= 2; tmp11 *= 2;
            }else if(cnt2 == 1){
                ;
            }else{
                if(f[i][2][0] == 2 || f[i][2][1] == 2){
                    tmp10 = 0; tmp11 = 0;
                }else{
                    tmp00 = 0; tmp01 = 0;
                }
            }
        }
        
        dp[i][0] = (tmp00 + tmp10) % MOD;
        dp[i][1] = (tmp01 + tmp11) % MOD;

        if(i >= 3){
            ll tmp3 = mod_pow(2,i-2,MOD);
            rep(j,g[i]){
                tmp3 = tmp3 * modinv(2,MOD) % MOD;
            }
            dp[i][0] = dp[i][0] * tmp3 % MOD;
            dp[i][1] = dp[i][1] * tmp3 % MOD;
        }
        
        if(f[i][0][0] == 1) dp[i][1] = 0;
        if(f[i][0][1] == 1) dp[i][0] = 0;
    }

    // rep(i,n) cout << dp[i][0] << ' ' << dp[i][1] << endl;

    ll ans = (dp[n-1][0] + dp[n-1][1]) % MOD;
    cout << ans << endl;
    return 0;
}




Measured memory_bytes: 11016