#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define SORT(a) sort(a.begin(), a.end())
#define REVERSE(a) reverse(a.begin(), a.end())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define int long long
#define INF 1000000000000000000
using namespace std;

#define ANS(f) if(f) cout << "YES" << endl; else cout << "NO" << endl;

typedef vector<int> vec;
typedef vector<vec> mat;
typedef pair<int, int> Pii;

template<typename T>
void readv(vector<T> &a){ REP(i, a.size()) cin >> a[i]; }
void readi(vector<int> &a){ REP(i, a.size()){cin >> a[i]; a[i]--;} }
void debug(mat m){REP(i, m.size()){ REP(j, m[i].size()){ cout << m[i][j] << ","; } cout << endl; }}

int modpow(int x, int n, int m){
    int a = 1;
    IREP(i, 64){
        a = (a * a) % m;
        if(((n >> i) & 1) == 1) a = (a * x) % m;
    }
    return a;
}

signed main(){

    int N, M; cin >> N >> M;
    vec a(M), b(M), c(M);
    int mod = 998244353;

    mat m2(2, vec(2, -1));
    vec fixed(N, 0);
    map<Pii, int> mnear, mfar;

    REP(i, M){
        cin >> a[i] >> b[i] >> c[i];
        a[i]--; b[i]--;
        if(llabs(a[i] - b[i]) <= 2) mnear[Pii(a[i], b[i])] = c[i] + 1;
        else{
            int vmin = min(a[i], b[i]), vmax = max(a[i], b[i]);
            if(mfar[Pii(vmin, vmax)] > 0){
                if(mfar[Pii(vmin, vmax)] - 1 != c[i]){
                    cout << 0;
                    return 0;
                }
            }else{
                mfar[Pii(vmin, vmax)] = c[i] + 1;
                fixed[vmax]++;
            }
        }
    }

    mat dp(N, vec(2, 0));
    REP(i, 16){
        int i0 = i;
        int tmp[2][2];
        bool flag = true;
        REP(j, 2) REP(k, 2){
            tmp[j][k] = i0 % 2;
            i0 /= 2;
            if(mnear[Pii(j, k)] > 0 && tmp[j][k] != mnear[Pii(j, k)] - 1) flag = false;
        }
        if(flag) if((tmp[0][0] + tmp[0][1] + tmp[1][0] + tmp[1][1]) % 2 == 0) dp[1][tmp[1][1]] += 1;
    }

    int o[5];
    FOR(i, 2, N){
        int tmp = modpow(2, i - 2 - fixed[i], mod);
        o[0] = mnear[Pii(i, i - 2)];
        o[1] = mnear[Pii(i, i - 1)];
        o[2] = mnear[Pii(i, i)];
        o[3] = mnear[Pii(i - 1, i)];
        o[4] = mnear[Pii(i - 2, i)];
        REP(a0, 2){
            if(o[0] > 0 && a0 != o[0] - 1) continue;
            REP(a1, 2){
                if(o[1] > 0 && a1 != o[1] - 1) continue;
                REP(a2, 2){
                    if(o[2] > 0 && a2 != o[2] - 1) continue;
                    REP(a3, 2){
                        if(o[3] > 0 && a3 != o[3] - 1) continue;
                        REP(a4, 2){
                            if(o[4] > 0 && a4 != o[4] - 1) continue;
                            if((a0 + a1 + a2 + a3 + a4) % 2 == 0){
                                int parity = (a1 + a2 + a3) % 2;
                                dp[i][a2] += tmp * dp[i - 1][parity];
                                dp[i][a2] %= mod;
                            }
                        }
                    }
                }
            }
        }
    }
    //debug(dp);
    cout << (dp[N - 1][0] + dp[N - 1][1]) % mod;
    
    return 0;
}

Measured memory_bytes: 41984