#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 998244353;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M;
    cin >> N >> M;
    vec<map<int,int>> A(N),B(N);
    for(int i=0;i<M;i++){
        int a,b,c;
        cin >> a >> b >> c;
        a--; b--;
        A[a][b] = c;
        B[b][a] = c;

    }
    vvec<mint> dp(N,vec<mint>(2));
    
    auto valid = [&](int x,int y,int n){
        return !A[x].count(y) || A[x][y]==n;
    };
    
    if(valid(0,0,0)) dp[0][0] = 1;
    if(valid(0,0,1)) dp[0][1] = 1;
    for(int i=1;i<N;i++) for(int j=0;j<2;j++){
        for(int k=0;k<2;k++){
            if(!valid(i,i,k)) continue;
            int s = (j+k)&1;
            int c = 0;
            for(int a=0;a<2;a++) for(int b=0;b<2;b++){
                if((s+a+b)&1) continue;
                c += (valid(i-1,i,a) && valid(i,i-1,b));
            }
            if(i>=2){
                int d = 0;
                for(int a=0;a<2;a++) for(int b=0;b<2;b++){
                    if((j+a+b)&1) continue;
                    d += (valid(i-2,i,a) && valid(i,i-2,b));
                }
                c *= d;
            }
            dp[i][k] += dp[i-1][j]*c;
        }
    }

    vec<mint> pow2(N+1,1);
    for(int i=1;i<=N;i++) pow2[i] = pow2[i-1]*2;
    mint ans = dp[N-1][0]+dp[N-1][1];
    for(int i=0;i+3<N;i++){
        int now = N-i-3;
        for(auto& x:A[i]){
            int a = x.first,c = x.second;
            if(a<i+3) continue;
            now--;
            if(!valid(a,i,c)) ans = 0;
        }
        for(auto& x:B[i]){
            int b = x.first,c = x.second;
            if(b<i+3) continue;
            if(!A[i].count(b)) now--;
        }
        ans *= pow2[now];
    }
    cout << ans << "\n";
}

Measured memory_bytes: 20736