#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 998244353
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl

using namespace std;

template<typename S,typename T>auto&operator<<(ostream&o,pair<S,T>p){return o<<"{"<<p.fi<<","<<p.se<<"}";}
template<typename T>auto&operator<<(ostream&o,set<T>s){for(auto&e:s)o<<e<<" ";return o;}
template<typename S,typename T,typename U>
auto&operator<<(ostream&o,priority_queue<S,T,U>q){while(!q.empty())o<<q.top()<<" ",q.pop();return o;}
template<typename K,typename T>auto&operator<<(ostream&o,map<K,T>m){for(auto&e:m)o<<e<<" ";return o;}
template<typename T>auto&operator<<(ostream&o,vector<T>v){for(auto&e:v)o<<e<<" ";return o;}
void ashow(){cout<<endl;}template<typename T,typename...A>void ashow(T t,A...a){cout<<t<<" ";ashow(a...);}

typedef pair<int,int> P;
typedef pair<P,int> PP;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<P> vp;
typedef vector<double> vd;
typedef vector<string> vs;

const int MAX_N = 100005;

template <uint mod>
class ModInt {
private:
    uint v;
    static uint norm(const uint& x){ return x < mod ? x : x - mod; }
	static ModInt make(const uint& x){ ModInt m; return m.v = x, m; }
    static ModInt inv(const ModInt& x){ return make(inverse(x.v, mod)); }
    static uint inverse(int a, int m){
        int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
        while(*v){
            t = *u / *v;
            swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]);
        }
        return (u[1] % m + m) % m;
    }

public:
    ModInt() : v{0}{}
    ModInt(const long long val) : v{norm(val % mod + mod)} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
	explicit operator bool() const noexcept { return v != 0; }
    bool operator!() const noexcept { return !static_cast<bool>(*this); }
    ModInt& operator=(const ModInt& n){ return v = n(), (*this); }
    ModInt& operator=(const long long val){ return v = norm(val % mod + mod), (*this); }
    ModInt operator+() const { return *this; }
    ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
    ModInt operator+(const ModInt& val) const { return make(norm(v + val())); }
    ModInt operator-(const ModInt& val) const { return make(norm(v + mod - val())); }
    ModInt operator*(const ModInt& val) const { return make((long long)v * val() % mod); }
    ModInt operator/(const ModInt& val) const { return *this * inv(val); }
    ModInt& operator+=(const ModInt& val){ return *this = *this + val; }
    ModInt& operator-=(const ModInt& val){ return *this = *this - val; }
    ModInt& operator*=(const ModInt& val){ return *this = *this * val; }
    ModInt& operator/=(const ModInt& val){ return *this = *this / val; }
    ModInt operator+(const long long val) const { return ModInt{v + val}; }
    ModInt operator-(const long long val) const { return ModInt{v - val}; }
    ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; }
    ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; }
    ModInt& operator+=(const long long val){ return *this = *this + val; }
    ModInt& operator-=(const long long val){ return *this = *this - val; }
    ModInt& operator*=(const long long val){ return *this = *this * val; }
    ModInt& operator/=(const long long val){ return *this = *this / val; }
    bool operator==(const ModInt& val) const { return v == val.v; }
    bool operator!=(const ModInt& val) const { return !(*this == val); }
    bool operator==(const long long val) const { return v == norm(val % mod + mod); }
    bool operator!=(const long long val) const { return !(*this == val); }
    uint operator()() const { return v; }
    friend ModInt operator+(const long long val, const ModInt& n) { return n + val; }
    friend ModInt operator-(const long long val, const ModInt& n) { return ModInt{val - n()}; }
    friend ModInt operator*(const long long val, const ModInt& n) { return n * val; }
    friend ModInt operator/(const long long val, const ModInt& n) { return ModInt{val} / n; }
    friend bool operator==(const long long val, const ModInt& n) { return n == val; }
    friend bool operator!=(const long long val, const ModInt& n) { return !(val == n); }
    friend istream& operator>>(istream& is, ModInt& n){
        uint v;
        return is >> v, n = v, is;
    }
    friend ostream& operator<<(ostream& os, const ModInt& n){ return (os << n()); }
    friend ModInt mod_pow(ModInt x, long long n){
        ModInt ans = 1;
        while(n){
            if(n & 1) ans *= x;
            x *= x, n >>= 1;
        }
        return ans;
    }
};

using mod = ModInt<MOD>;

mod inv[MAX_N],fac[MAX_N],finv[MAX_N];

void make()
{
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i=2;i<MAX_N;i++){
		inv[i] = MOD - inv[MOD % i] * (MOD / i);
		fac[i] = fac[i-1] * i;
		finv[i] = finv[i-1] * inv[i];
	}
}

mod comb(int a, int b)
{
	if(a<b) return 0;
	return fac[a] * finv[b] * finv[a-b];
}

struct pair_hash {
    template <class T1, class T2>
    size_t operator() (const pair<T1, T2>& p) const {
        size_t lhs = hash<T1>()(p.first), rhs = hash<T2>()(p.second);
        return lhs^(rhs+0x9e3779b9+(lhs<<6)+(lhs>>2));
    }
};

mod dp[MAX_N][16];
unordered_map<P, int, pair_hash> mp;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    if(n == 2){
        int cri = 0;
        rep(i,m){
            int a,b,c;
            cin >> a >> b >> c;
            cri ^= c;
        }
        if(m == 4){
            cout << (cri == 0) << "\n";
        }else{
            cout << pow(2, 3-m) << "\n";
        }
        return 0;
    }
    ll hoge = (ll)(n-2)*(n-3)/2;
    rep(i,m){
        int a,b,c;
        cin >> a >> b >> c;
        --a, --b;
        if(abs(a-b) > 2){
            auto it = mp.find(P(b, a));
            if(it != mp.end()){
                if(it->se != c){
                    cout << "0\n";
                    return 0;
                }
            }else{
                hoge--;
            }
        }
        mp[P(a, b)] = c;
    }
    unordered_map<P, int, pair_hash>::iterator it;
    rep(i,16){
        if(__builtin_popcount(i)%2) continue;
        if(((it = mp.find(P(0, 0))) != mp.end()) && it->se != (i>>3&1)) continue;
        if(((it = mp.find(P(0, 1))) != mp.end()) && it->se != (i>>2&1)) continue;
        if(((it = mp.find(P(1, 0))) != mp.end()) && it->se != (i>>1&1)) continue;
        if(((it = mp.find(P(1, 1))) != mp.end()) && it->se != (i>>0&1)) continue;
        dp[1][i] = 1;
    }
    srep(i,1,n-1){
        rep(j,16){
            if(j&1){
                int ad = 0, ch = 0;
                if((it = mp.find(P(i-1, i+1))) != mp.end()){
                    ad++, ch = it->se;
                }
                if((it = mp.find(P(i+1, i-1))) != mp.end()){
                    ad++, ch ^= it->se;
                }
                if(ad == 2){
                    if(ch != 1){
                        continue;
                    }else{
                        ad = 1;
                    }
                }else{
                    ad = (1 << (1-ad));
                }
                rep(k,8){
                    if(__builtin_popcount(k)%2 == 0) continue;
                    if((it = mp.find(P(i, i+1))) != mp.end() && it->se != (k>>2&1)) continue;
                    if((it = mp.find(P(i+1, i))) != mp.end() && it->se != (k>>1&1)) continue;
                    if((it = mp.find(P(i+1, i+1))) != mp.end() && it->se != (k>>0&1)) continue;
                    dp[i+1][(1 << 3) + k] += dp[i][j]*ad;
                }
            }else{
                int ad = 0, ch = 0;
                if((it = mp.find(P(i-1, i+1))) != mp.end()){
                    ad++, ch = it->se;
                }
                if((it = mp.find(P(i+1, i-1))) != mp.end()){
                    ad++, ch ^= it->se;
                }
                if(ad == 2){
                    if(ch != 0){
                        continue;
                    }else{
                        ad = 1;
                    }
                }else{
                    ad = (1 << (1-ad));
                }
                rep(k,8){
                    if(__builtin_popcount(k)%2 == 1) continue;
                    if((it = mp.find(P(i, i+1))) != mp.end() && it->se != (k>>2&1)) continue;
                    if((it = mp.find(P(i+1, i))) != mp.end() && it->se != (k>>1&1)) continue;
                    if((it = mp.find(P(i+1, i+1))) != mp.end() && it->se != (k>>0&1)) continue;
                    dp[i+1][k] += dp[i][j]*ad;
                }
            }
        }
    }
    cout << accumulate(dp[n-1], dp[n-1]+16, (mod)0)*mod_pow((mod)2,hoge) << "\n";
    return 0;
}


Measured memory_bytes: 12556