#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=998244353 ;


ll powmod(ll k){
    ll ret=1;
    ll n=2LL;
    while(k>0){
        if(k&1)ret=ret*n%mod;
        n=n*n%mod;
        k/=2;
    }
    return ret;
}

int cnt[101010];
bool even[101010],odd[101010],e[101010],o[101010],eve[101010],od[101010],e3[101010],o3[101010];
int main(){
    int n,m;
    cin>>n>>m;
    vector<pair<pint,int>> v;
    rep(i,m){
        int a,b,c;
        cin>>a>>b>>c;
        --a;--b;
        if(a>b)swap(a,b);
        v.push_back({{a,b},c});
    }
    sort(v.begin(),v.end());
    v.push_back({{inf,inf},inf});
    v.push_back({{inf,inf},inf});
    rep(i,n){
        cnt[i]=i;
        even[i]=odd[i]=true;
        e[i]=o[i]=true;
        e3[i]=o3[i]=true;
        eve[i]=od[i]=true;
    }
    rep(i,m){
        if(v[i].first==v[i+1].first){
            cnt[v[i].first.second]--;
            if(v[i].first.second==v[i].first.first+1){
                if(v[i].second==v[i+1].second)o[v[i].first.second]=false;
                else e[v[i].first.second]=false;
                
            }
            else if(v[i].first.second==v[i].first.first+2){
                if(v[i].second==v[i+1].second)o3[v[i].first.second]=false;
                else e3[v[i].first.second]=false;
            }
            else {
                if(v[i].second==v[i+1].second)odd[v[i].first.second]=false;
                else even[v[i].first.second]=false;
            }
            i++;
        }
        else {
            if(v[i].first.first==v[i].first.second){
                if(v[i].second)eve[v[i].first.second]=false;
                else od[v[i].first.second]=false;
            }
            else {
                cnt[v[i].first.second]--;
            }
        }
    }
    ll ans0=eve[0],ans1=od[0];
    rep(i,n-1){
        ll ret0=0;
        if(eve[i+1]){
            if(e[i+1]&&e3[i+1]&&even[i+1])ret0+=ans0*powmod(cnt[i+1])%mod;
            if(o[i+1]&&o3[i+1]&&even[i+1])ret0+=ans1*powmod(cnt[i+1])%mod;
        }
        ll ret1=0;
        if(od[i+1]){
            if(e[i+1]&&o3[i+1]&&even[i+1])ret1+=ans1*powmod(cnt[i+1])%mod;
            if(o[i+1]&&e3[i+1]&&even[i+1])ret1+=ans0*powmod(cnt[i+1])%mod;
        }
        ans0=ret0%mod;
        ans1=ret1%mod;
    }
    cout<<(ans0+ans1)%mod;
    return 0;
}
