#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};

template<int mod>
struct ModInt {
    long long x;
 
    ModInt() : x(0) {}
    ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}

    explicit operator int() const {return x;}
 
    ModInt &operator+=(const ModInt &p) {
        if((x += p.x) >= mod) x -= mod;
        return *this;
    }
    ModInt &operator-=(const ModInt &p) {
        if((x += mod - p.x) >= mod) x -= mod;
        return *this;
    }
    ModInt &operator*=(const ModInt &p) {
        x = (int)(1LL * x * p.x % mod);
        return *this;
    }
    ModInt &operator/=(const ModInt &p) {
        *this *= p.inverse();
        return *this;
    }
 
    ModInt operator-() const { return ModInt(-x); }
    ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
    ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
    ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
    ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
    bool operator==(const ModInt &p) const { return x == p.x; }
    bool operator!=(const ModInt &p) const { return x != p.x; }
 
    ModInt inverse() const{
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return ModInt(u);
    }

    ModInt power(long long p) const{
        int a = x;
        if (p==0) return 1;
        if (p==1) return ModInt(a);
        if (p%2==1) return (ModInt(a)*ModInt(a)).power(p/2)*ModInt(a);
        else return (ModInt(a)*ModInt(a)).power(p/2);
    }

    ModInt power(const ModInt p) const{
        return ((ModInt)x).power(p.x);
    }

    friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
        return os << p.x;
    }
    friend istream &operator>>(istream &is, ModInt<mod> &a) {
        long long x;
        is >> x;
        a = ModInt<mod>(x);
        return (is);
    }
};

using modint = ModInt<mod>;



int n,m;
map<P,int> S;
modint dp[100010][2];
map<int,P> ma[100010];
map<P,bool> visited;

void solve(){
    cin >> n >> m;
    rep(i,m){
        int x,y,c;cin >> x >> y >> c;x--;y--;
        S[P(x,y)]=c+1;
    }
    for(auto p:S){
        if(visited[p.first]) continue;
        int x1,y1,c1;x1=p.first.first,y1=p.first.second,c1=p.second;
        int t=max(x1,y1)-min(x1,y1);
        ma[max(x1,y1)][t]=P(c1,S[P(y1,x1)]);
        visited[P(x1,y1)]=true;
        visited[P(y1,x1)]=true;
    }
    dp[0][0]=1;dp[0][1]=1;
    if(S[P(0,0)]!=0) dp[0][2-S[P(0,0)]]=0;
    // rep(i,n){
    //     cout << i << endl;
    //     for(auto p:ma[i]){
    //         cout << p.first << " ( " << p.second.first << " , " << p.second.second << " ) " << endl;
    //     }
    // }
    rep(i,n-1){
        int pattern1=0,pattern2=0,muri=0,num=0;
        for(auto p:ma[i+1]){
            int t,c1,c2;t=p.first;c1=p.second.first;c2=p.second.second;
            if(t==0) continue;
            num+=1;
            if(t==1){
                if(c1==2 && c2==1) pattern1=2;
                if(c1==1 && c2==2) pattern1=2;
                if(c1==1 && c2==1) pattern1=1;
                if(c1==2 && c2==2) pattern1=1;
            }
            else if(t==2){
                if(c1==2 && c2==1) pattern2=2;
                if(c1==1 && c2==2) pattern2=2;
                if(c1==1 && c2==1) pattern2=1;
                if(c1==2 && c2==2) pattern2=1;
            }
            else{
                if(c1==2 && c2==1) muri=1;
                if(c1==1 && c2==2) muri=1;
            }
        }
        // cout << i+1 << endl;
        // cout << pattern1 << " " << pattern2 << " " << muri << endl;
        if(muri==1) continue;
        rep(j1,2){
            rep(j2,2){
                if(pattern1==1) if((j1+j2)%2!=0) continue;
                if(pattern1==2) if((j1+j2)%2!=1) continue;
                if(pattern2==1) if(j1%2!=0) continue;
                if(pattern2==2) if(j1%2!=1) continue;
                dp[i+1][j2]+=((modint)2).power(i-num+1)*dp[i][j1];
            }
        }
        if(S[P(i+1,i+1)]!=0) dp[i+1][2-S[P(i+1,i+1)]]=0;
    }
    // rep(i,n){
    //     rep(j,2){
    //         cout << i << " " << j << " " << dp[i][j] << endl;
    //     }
    // }
    cout << dp[n-1][0]+dp[n-1][1] << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}

Measured memory_bytes: 28416