#include <bits/stdc++.h>
using namespace std;
const long long MOD=998244353;

template<uint_fast64_t Modulus> class modint{
    using u64=uint_fast64_t;
    public:
    u64 a;
    constexpr modint(const u64 x=0) noexcept:a(((x%Modulus)+Modulus)%Modulus){}
    constexpr u64 &value() noexcept{return a;}
    constexpr const u64 &value() const noexcept{return a;}
    constexpr modint &operator+=(const modint &rhs) noexcept{
        a+=rhs.a;
        if (a>=Modulus) a-=Modulus;
        return *this;
    }
    constexpr modint operator+(const modint &rhs) const noexcept{
        return modint(*this)+=rhs;
    }
    constexpr modint &operator++() noexcept{
        return ++a,*this;
    }
    constexpr modint operator++(int) noexcept{
        modint t=*this; return ++a,t;
    }
    constexpr modint &operator-=(const modint &rhs) noexcept{
        if (a<rhs.a) a+=Modulus;
        a-=rhs.a;
        return *this;
    }
    constexpr modint operator-(const modint &rhs) const noexcept{
        return modint(*this)-=rhs;
    }
    constexpr modint &operator--() noexcept{
        return --a,*this;
    }
    constexpr modint operator--(int) noexcept{
        modint t=*this; return --a,t;
    }
    constexpr modint &operator*=(const modint &rhs) noexcept{
        a=a*rhs.a%Modulus;
        return *this;
    }
    constexpr modint operator*(const modint &rhs) const noexcept{
        return modint(*this)*=rhs;
    }
    constexpr modint &operator/=(modint rhs) noexcept{
        u64 exp=Modulus-2;
        while(exp){
            if (exp&1) *this*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return *this;
    }
    constexpr modint operator/(const modint &rhs) const noexcept{
        return modint(*this)/=rhs;
    }
    constexpr modint operator-() const noexcept{
        return modint(Modulus-a);
    }
    constexpr bool operator==(const modint &rhs) const noexcept{
        return a==rhs.a;
    }
    constexpr bool operator!=(const modint &rhs) const noexcept{
        return a!=rhs.a;
    }
    constexpr bool operator!() const noexcept{return !a;}
    friend constexpr modint pow(modint rhs,long long exp) noexcept{
        modint res{1};
        while(exp){
            if (exp&1) res*=rhs;
            rhs*=rhs; exp>>=1;
        }
        return res;
    }
    template<class T> friend constexpr modint operator+(T x,modint y) noexcept{
        return modint(x)+y;
    }
    template<class T> friend constexpr modint operator-(T x,modint y) noexcept{
        return modint(x)-y;
    }
    template<class T> friend constexpr modint operator*(T x,modint y) noexcept{
        return modint(x)*y;
    }
    template<class T> friend constexpr modint operator/(T x,modint y) noexcept{
        return modint(x)/y;
    }
    friend ostream &operator<<(ostream &s,const modint &rhs) noexcept {
        return s << rhs.a;
    }
    friend istream &operator>>(istream &s,modint &rhs) noexcept {
        u64 a; rhs=modint{(s >> a,a)}; return s;
    }
};

using mint=modint<MOD>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,M; cin >> N >> M;
    vector<int> a(M),b(M),c(M),diagonal(N,-1);
    for (int i=0;i<M;++i){
        cin >> a[i] >> b[i] >> c[i];
        if (a[i]<b[i]) swap(a[i],b[i]);
        b[i]=a[i]-b[i]; --a[i];
        if (b[i]==0) diagonal[a[i]]=c[i];
    }
    map<pair<int,int>,int> m;
    vector<vector<pair<int,int>>> pos(N);
    for (int i=0;i<M;++i){
        if (2<b[i]&&m.count({a[i],b[i]})){
            if (m[{a[i],b[i]}]!=c[i]){cout << 0 << '\n'; return 0;}
            else continue;
        }
        m[{a[i],b[i]}]=c[i];
        pos[a[i]].emplace_back(b[i],c[i]);
    }
    vector<vector<mint>> dp(N+1,vector<mint>(2,0));
    dp[0][0]=1;
    for (int i=0;i<N;++i){
        int ok=0,cnt1=0,cnt2=0,n1,n2,m1,m2;
        for (auto p:pos[i]){
            if (p.first==1){
                ++cnt1;
                if (cnt1==1) n1=p.second;
                else n2=p.second;
            }
            if (p.first==2){
                ++cnt2;
                if (cnt2==1) m1=p.second;
                else m2=p.second;
            }
            if (2<p.first) ++ok;
        }
        ok=max(i-2,0)-ok; n1+=n2,m1+=m2;
        if (0<i&&!cnt1) ++ok;
        if (1<i&&!cnt2) ++ok;
        mint x=pow((mint)2,ok);
        dp[i][0]*=x,dp[i][1]*=x;
        if (cnt1==2&&cnt2==2){
            if (n1!=1&&m1!=1) dp[i+1][0]+=dp[i][0];
            if (n1!=1&&m1==1) dp[i+1][1]+=dp[i][1];
            if (n1==1&&m1!=1) dp[i+1][1]+=dp[i][0];
            if (n1==1&&m1==1) dp[i+1][0]+=dp[i][1];
        } else if (cnt1==2){
            if (n1!=1) dp[i+1][0]+=dp[i][0],dp[i+1][1]+=dp[i][1];
            if (n1==1) dp[i+1][0]+=dp[i][1],dp[i+1][1]+=dp[i][0];
        } else if (cnt2==2){
            if (m1!=1) dp[i+1][0]+=dp[i][0],dp[i+1][1]+=dp[i][0];
            if (m1==1) dp[i+1][0]+=dp[i][1],dp[i+1][1]+=dp[i][1];
        } else {
            dp[i+1][0]+=dp[i][0],dp[i+1][1]+=dp[i][0];
            dp[i+1][0]+=dp[i][1],dp[i+1][1]+=dp[i][1];
        }
        if (diagonal[i]!=-1) dp[i+1][diagonal[i]^1]=0;
    }
    cout << dp[N][0]+dp[N][1] << '\n';
}

Measured memory_bytes: 13696