#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf 1000000007
#define mod 998244353
#define rep(i,n) for(int i=0;i<(int)(n);++i)


long long calc(long long  k,long long m){
	if(m==0)return 1;
	if(m==1)return k%mod;
	long long s = calc(k,m/2);
	if(m%2==0){
		return (s*s)%mod;
	}else{
		long long ans;
		ans = (s*s)%mod;
		return (k*ans)%mod;
	}
}
ll p[100][2];

int bitcalc(int i,int j,int k){
    return (i+1)*9 + (j+1)*3+(k+1);
}

void init(){
    int tmp = 0;
    for(int i=-1;i<=1;i++){
        for(int j=-1;j<=1;j++){
            for(int k=-1;k<=1;k++){
                tmp = bitcalc(i,j,k);
                int c1=0,c2=0,c3=0;
                if(i==-1){
                    c1++;
                }
                if(i==0){
                    c2++;
                }
                if(i==1){
                    c3++;
                }
                if(j==-1){
                    c1++;
                }
                if(j==0){
                    c2++;
                }
                if(j==1){
                    c3++;
                }
                if(k==-1){
                    c1++;
                }
                if(k==0){
                    c2++;
                }
                if(k==1){
                    c3++;
                }
                if(c1==0){
                    if(c3%2==0){
                        p[tmp][0] = 1;
                        p[tmp][1] = 0;
                    }else{
                        p[tmp][0] = 0;
                        p[tmp][1] = 1;
                    }
                }else if(c1==1){
                    p[tmp][0] = 1;
                    p[tmp][1] = 1;
                }else if(c1==2){
                    p[tmp][0] = 2;
                    p[tmp][1] = 2;
                }else if(c1==3){
                    p[tmp][0] = 4;
                    p[tmp][1] = 4;
                }
            }
        }
    }
}

ll s[100010][3][3];
ll dp[100010][2];
int main(){
    ll n,m;
    cin >> n >> m;
    map<pair<int,int>,int> mp;
    for(int i=0;i<m;i++){
        int a,b,c;
        cin >> a >> b >> c;
        mp[MP(a,b)] = c;
    }
    ll cnt = 0;
    ll cnt2 = 0;
    bool flag = 0;
    for(int i=0;i<=n;i++){
        for(int j=0;j<3;j++){
            for(int k=0;k<3;k++){
                s[i][j][k] = -1;
            }
        }
    }
    for(auto x:mp){
        auto y = x.first;
        pair<int,int> z = MP(y.second,y.first);
        if(abs(y.first-y.second)<=2){
            if(abs(y.first-y.second)==0){
                s[y.first][0][0] = x.second;
                if(y.first>=2)s[y.first-1][1][1] = x.second;
                if(y.first>=3)s[y.first-2][2][2] = x.second;
            }else if(abs(y.first-y.second)==1){
                if(y.first>y.second){
                    if(y.first>=2)s[y.first-1][0][1] = x.second;
                    if(y.first>=3)s[y.first-2][1][2] = x.second;
                }else{
                    if(y.second>=2)s[y.second-1][1][0] = x.second;
                    if(y.second>=3)s[y.second-2][2][1] = x.second;
                }
            }else{
                if(y.first>y.second){
                    s[y.second][0][2] = x.second;
                }else{
                    s[y.first][2][0] = x.second;
                }
            }
        }else if(mp.find(z)!=mp.end()){
            if(mp[z]!=x.second){
                flag = 1;
            }
            cnt++;
        }else{
            cnt2++;
        }
    }
    if(flag){
        cout << 0 << endl;
        return 0;
    }
    init();
    if(n==2){
        int bit = bitcalc(s[1][0][0],s[1][0][1],s[1][1][0]);
        //cerr << bit << endl;
        if(s[1][1][1]==-1)cout << p[bit][0]+p[bit][1] << endl;
        if(s[1][1][1]==0)cout << p[bit][0] << endl;
        if(s[1][1][1]==1)cout << p[bit][1] << endl;
        
        return 0;
    }
    //cerr << cnt << " " << cnt2 << endl;
    ll sm = n*n;
    sm -= 4+5*(n-2);
    sm -= cnt;
    sm /= 2;
    sm -= cnt2;
    //cerr << sm << endl;
    sm = calc(2LL,sm);
    //cout << sm << endl;
    dp[0][0] = 1;
    dp[0][1] = 1;
    ll ans =0;
    for(int i=1;i<=n-1;i++){
        if(i==1){
            int bit = bitcalc(s[i][0][0],s[i][0][1],s[i][1][0]);
            dp[i][0] = p[bit][0];
            dp[i][1] = p[bit][1];
        }else{
            int bit = bitcalc(0,s[i][0][1],s[i][1][0]);
            dp[i][0] += (dp[i-1][0]*p[bit][0])%mod;
            dp[i][0] %= mod;
            dp[i][1] += (dp[i-1][0]*p[bit][1])%mod;
            dp[i][1] %= mod;
            bit = bitcalc(1,s[i][0][1],s[i][1][0]);
            dp[i][0] += (dp[i-1][1]*p[bit][0])%mod;
            dp[i][0] %= mod;
            dp[i][1] += (dp[i-1][1]*p[bit][1])%mod;
            dp[i][1] %= mod;
        }
        if(s[i][0][2]==1&&s[i][2][0]==0){
            dp[i][0] = 0;
        }
        if(s[i][0][2]==0&&s[i][2][0]==1){
            dp[i][0] = 0;
        }
        if(s[i][0][2]==0&&s[i][2][0]==0){
            dp[i][1] = 0;
        }
        if(s[i][0][2]==1&&s[i][2][0]==1){
            dp[i][1] = 0;
        }
        if(i!=n-1&&s[i][0][2]==-1&&s[i][2][0]==-1){
            dp[i][0] *=2;
            dp[i][0] %=mod;
            dp[i][1] *=2;
            dp[i][1] %=mod;
        }
        if(s[i][1][1]!=-1){
            if(s[i][1][1]==1){
                dp[i][0] = 0;
            }else{
                dp[i][1] = 0;
            }
        }
        //cerr << i << " " << dp[i][0] << " " << dp[i][1] << endl;
    }
    cout << (sm*((dp[n-1][0]+dp[n-1][1])%mod))%mod << endl;
    return 0;
}

Measured memory_bytes: 11136