#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 998244353;

template <typename T>
T pow(T a, ll n) {
	T ans = 1;
	T tmp = a;
	for (int i = 0; i <= 60; i++) {
		ll m = (ll)1 << i;
		if (m & n) {
		ans *= tmp;
		ans %= MOD;
		}
		tmp *= tmp;
		tmp %= MOD;
	}
	return ans;
}

struct edge{
    int to, cost;
};

ll N, M;

map<P, int> mp;
set<P> same, dif;
int col[100000];
bool used[100000];
vector<edge> G[100000];

set<int> idx1_set;
set<int> idx0_set;
void dfs(int v, int d){
    // cout << "dfs:" << v << ' ' << d << ' ' << col[v] << endl;
    if(col[v] == 1) idx1_set.insert(d%2);
    if(col[v] == 0) idx0_set.insert(d%2);
    used[v] = true;
    for(edge e : G[v]) {
        int to = e.to;
        int c = e.cost;
        if(!used[to]) dfs(to, d+c);
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> N >> M;
    ll cnt_free = (N*(N-1))/2;
    for(int i = 0; i < N; i++) col[i] = -1;
    for(int i = 0; i < M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        if(a > b) swap(a, b);
        P p = P(a, b);
        if(a == b){
            col[a] = c;
            continue;
        }
        if(mp.count(p) != 0){
            if(mp[p] == c){
                if(b-a <= 2) same.insert(p);
            }else{
                if(b-a > 2){
                    cout << 0 << endl;
                    return 0;
                }
                dif.insert(p);
            }
            mp.erase(p);
        }else{
            mp[p] = c;
            cnt_free--;
        }
    }
    // cout << cnt_free << endl;
    
    for(auto i : dif){
        int a = i.first, b = i.second;
        if(b-a == 2) {
            if(col[a+1] == 0) {
                cout << 0 << endl;
                return 0;
            }
            col[a+1] = 1;
        }
        else G[a].push_back((edge){b, 1});
    }
    for(auto i : same){
        int a = i.first, b = i.second;
        if(b-a == 2) {
            if(col[a+1] == 1) {
                cout << 0 << endl;
                return 0;
            }
            col[a+1] = 0;
        }
        else G[a].push_back((edge){b, 0});
    }

    ll ans = pow<ll>(2, cnt_free);
    for(int i = 0; i < N; i++){
        if(!used[i]){
            // cout << "======" << endl;
            idx0_set.clear(); idx1_set.clear();
            dfs(i, 0);
            if(idx1_set.size() > 1 || idx0_set.size() > 1){
                cout << 0 << endl;
                return 0;
            }else if(idx1_set.size() == 1 && idx0_set.size() == 1){
                int x = *(idx0_set.begin());
                int y = *(idx1_set.begin());
                if(x == y){
                    cout << 0 << endl;
                    return 0;
                }else{
                    // そのまま
                }
            }else if(idx1_set.size() == 1 || idx0_set.size() == 1){
                // そのまま                
            }else{
                ans *= 2;
                ans %= MOD;
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 6400