#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
const LL mod=998244353;
const LL LINF=1LL<<62;
const LL INF=1<<17;
map<P,int> ma;
map<P,int> st;

LL fpow(LL a, LL n){
    LL x = 1;
    while(n > 0){
        if(n&1){
            x=x*a%mod;
        }
        a=a*a%mod;
        n >>= 1;
    }
    return x;
}


int main(){
    LL n,m;cin >> n >> m;
    vector<LL> t(n,0);
    LL k=n*(n-1)-2*(n-1)-2*(n-2);
    for (int i = 0; i < m; i++) {
        int a,b,c;
        cin >> a >> b >> c;
        a--,b--;
        st[{a,b}]=c+2;
        if(a>b) swap(a,b);
        if(ma[{a,b}]){
            if(ma[{a,b}]!=c+2){
                if(abs(a-b)>=3){
                    puts("0");
                    return 0;
                }
                else{
                    if((a+b)%2==0){
                        if(t[(a+b)/2]==2){
                            puts("0");
                            return 0;
                        }
                        t[(a+b)/2]=3;
                    }
                }
            }
            else{
                if(abs(a-b)<3&&(a+b)%2==0){
                    if(t[(a+b)/2]==3){
                        puts("0");
                        return 0;
                    }
                    t[(a+b)/2]=2;
                }
            }
        }
        if(a==b){
            if(t[a]!=c+2&&t[a]){
                puts("0");
                return 0;
            }
            t[a]=c+2;
        }
        if(abs(a-b)>2&&ma[{a,b}]==0) k-=2;
        ma[{a,b}]=c+2;
    }
    LL dp[n][2];
    memset(dp,0,sizeof(dp));
    if(t[0]!=3) dp[0][0]=1;
    if(t[0]!=2) dp[0][1]=1;
    for (int i = 0; i < n-1; i++) {
        if(st[{i,i+1}]==0&&st[{i+1,i}]==0){
            if(i==n-2){
                dp[i+1][0]=(2*dp[i][0]+2*dp[i][1])%mod;
                dp[i+1][1]=(2*dp[i][0]+2*dp[i][1])%mod;
            }
            else{    
                if(st[{i,i+2}]==0&&st[{i+2,i}]==0){
                    dp[i+1][0]=(4*dp[i][0]+4*dp[i][1])%mod;
                    dp[i+1][1]=(4*dp[i][0]+4*dp[i][1])%mod;
                }
                else{
                    dp[i+1][0]=(2*dp[i][0]+2*dp[i][1])%mod;
                    dp[i+1][1]=(2*dp[i][0]+2*dp[i][1])%mod;
                }
            }
        }
        else if(st[{i,i+1}]==0||st[{i+1,i}]==0){
            if(i==n-2){
                dp[i+1][0]=(dp[i][0]+dp[i][1])%mod;
                dp[i+1][1]=(dp[i][0]+dp[i][1])%mod;
            }
            else{
                if(st[{i,i+2}]==0&&st[{i+2,i}]==0){
                    dp[i+1][0]=(2*dp[i][0]+2*dp[i][1])%mod;
                    dp[i+1][1]=(2*dp[i][0]+2*dp[i][1])%mod;
                }
                else{
                    dp[i+1][0]=(dp[i][0]+dp[i][1])%mod;
                    dp[i+1][1]=(dp[i][0]+dp[i][1])%mod;
                }
            }
        }
        else{
            if(i==n-2){
                dp[i+1][0]=dp[i][(st[{i,i+1}]+st[{i+1,i}])%2];
                dp[i+1][1]=dp[i][(1+st[{i,i+1}]+st[{i+1,i}])%2];
            }
            else{
                if(st[{i,i+2}]==0&&st[{i+2,i}]==0){
                    dp[i+1][0]=2*dp[i][(st[{i,i+1}]+st[{i+1,i}])%2]%mod;
                    dp[i+1][1]=2*dp[i][(1+st[{i,i+1}]+st[{i+1,i}])%2]%mod;
                }
                else{
                    dp[i+1][0]=dp[i][(st[{i,i+1}]+st[{i+1,i}])%2];
                    dp[i+1][1]=dp[i][(1+st[{i,i+1}]+st[{i+1,i}])%2];
                }
            }
        }
        if(t[i+1]==3) dp[i+1][0]=0;
        else if(t[i+1]==2) dp[i+1][1]=0;
    }
    cout << (dp[n-1][0]+dp[n-1][1])%mod*fpow(2,k/2)%mod << endl;
    return 0;
}


Measured memory_bytes: 33792