#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
    ost<<"{"<<p.first<<","<<p.second<<"}";
    return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
    ost<<"{";
    for(int i=0;i<v.size();i++){
        if(i)ost<<",";
        ost<<v[i];
    }
    ost<<"}";
    return ost;
}

inline int topbit(unsigned long long x){
    return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
    return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
    return __builtin_parity(x);
}

template<uint32_t mod>
struct ModInt{
    uint32_t a;
    ModInt& s(uint32_t vv){
        a=vv<mod?vv:vv-mod;
        return *this;
    }

    ModInt(int64_t x=0){s(x%mod+mod);}

    ModInt& operator+=(const ModInt &x){return s(a+x.a);}
    ModInt& operator-=(const ModInt &x){return s(a+mod-x.a);}
    ModInt& operator*=(const ModInt &x){
        a=uint64_t(a)*x.a%mod;
        return *this;
    }
    ModInt& operator/=(const ModInt &x){
        *this*=x.inv();
        return *this;
    }

    ModInt operator+(const ModInt &x)const{return ModInt(*this)+=x;}
    ModInt operator-(const ModInt &x)const{return ModInt(*this)-=x;}
    ModInt operator*(const ModInt &x)const{return ModInt(*this)*=x;}
    ModInt operator/(const ModInt &x)const{return ModInt(*this)/=x;}
    bool operator==(const ModInt &x)const{return a==x.a;}
    bool operator!=(const ModInt &x)const{return a!=x.a;}
    bool operator<(const ModInt &x)const{return a<x.a;}

    ModInt operator-()const{return ModInt()-*this;}
    ModInt pow(int64_t n)const{
        ModInt res(1),x(*this);
        while(n){
            if(n&1)res*=x;
            x*=x;
            n>>=1;
        }
        return res;
    }

    ModInt inv()const{return pow(mod-2);}
};

template<uint32_t mod>
istream& operator>>(istream& in,ModInt<mod>& a){
    return (in>>a.a);
}
template<uint32_t mod>
ostream& operator<<(ostream& out,const ModInt<mod>& a){
    return (out<<a.a);
}
using mint=ModInt<998244353>;

int N,M;

mint dp[111111][2];
map<int,int>mem[111111];
int ess[111111][5];
signed main(){
    cin>>N>>M;
    memset(ess,-1,sizeof(ess));
    rep(i,M){
        int a,b,c;
        cin>>a>>b>>c;
        a--;b--;
        if(abs(a-b)<=2){
            int t;
            if(a==b)t=0;
            else if(b==a+1)t=1;
            else if(a==b+1)t=2;
            else if(b==a+2)t=3;
            else t=4;
            ess[min(a,b)][t]=c;
        }
        else{
            if(a>b)swap(a,b);
            if(mem[a].find(b)!=mem[a].end()&&mem[a][b]!=c){
                cout<<0<<endl;
                return 0;
            }
            mem[a][b]=c;
        }
    }
    dp[0][0]=dp[0][1]=1;
    rep(i,N-2){
        rep(j,1<<5){
            bool ok=true;
            int t=parity(j&((1<<3)-1));
            rep(k,5)if(ess[i][k]!=-1&&ess[i][k]!=(j>>k&1))ok=false;
            if(parity(j))ok=false;
            if(!ok)continue;
            dp[i+1][t]+=dp[i][j&1];
        }
    }


    mint ans=0;
    rep(i,1<<3){
        bool ok=true;
        if(ess[N-1][0]!=-1&&ess[N-1][0]!=parity(i))ok=false;
        rep(k,3)if(ess[N-2][k]!=-1&&ess[N-2][k]!=(i>>k&1))ok=false;
        if(!ok)continue;

        ans+=dp[N-2][i&1];
    }



    rep(i,N-2){
        int num=N-i-3-(int)mem[i].size();

        ans*=mint(2).pow(num);
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 13824