#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>
#include <bitset>
#include <queue>
#include <stack>
#include <deque>
#include <complex>
#include <iomanip>
#include <stdio.h>
#include <string.h>
#include <random>
#include <functional>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

using std::map;
using std::set;
using std::bitset;
using std::vector;
using std::string;
using std::multimap;
using std::multiset;
using std::deque;
using std::queue;
using std::stack;
using std::pair;
using std::iterator;

using std::sort;
using std::stable_sort;
using std::reverse;
using std::max_element;
using std::min_element;
using std::unique;
using std::ios_base;
using std::swap;
using std::fill;

using std::setprecision;
using std::fixed;

long long min(long long a, long long b) {return a < b ? a : b;}
long long min(int a, long long b) {return a < b ? a : b;}
long long min(long long a, int b) {return a < b ? a : b;}
long long min(int a, int b) {return a < b ? a : b;}

long long max(long long a, long long b) {return a > b ? a : b;}
long long max(int a, long long b) {return a > b ? a : b;}
long long max(long long a, int b) {return a > b ? a : b;}
long long max(int a, int b) {return a > b ? a : b;}

#define int long long

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

#define F(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()

const long long INF = 1e18;
const int N = 210110;
const int MOD = 998244353;
const double eps = 1e-8;

long long inq(ll a, ll b)
{
    if(b == 0) return 1;
    ll l = inq(a, b / 2);
    if (b % 2) return l * l % MOD * a % MOD;
    return l * l % MOD;
}

int n, m;
map<pair<int, int>, pair<int, int> > mm;
int cells;
int x[N];
vector<pii> graph[N];
int ans = 0;
int c[N];
int zero, one;

void dfs(int v)
{
    if (x[v] == 1)
    {
        one = v;
    }
    if (x[v] == 0)
    {
        zero = v;
    }
    c[v] = 1;
    for (auto u : graph[v])
    {
        if (c[u.first] == 0)
        {
            dfs(u.first);
        }
    }
}

void ch(int v, int val)
{
    if (x[v] == -1) x[v] = val;
    if (x[v] != val) cout << 0, exit(0);
    if (c[v] == 2) return;
    c[v] = 2;
    for (auto u : graph[v])
    {
        int kek = u.second;
        int U = u.first;
        ch(U, val ^ kek);
    }
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for (int i = 0; i < m; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        if (a > b)
        {
            swap(a, b);
        }
        mm[{a, b}].first++;
        if (c) mm[{a, b}].second ^= 1;
    }
    cells = n * (n - 1) / 2;
    fill(x, x + n, -1);
    for (auto it : mm)
    {
        int i = it.first.first, j = it.first.second;
        if (i == j)
        {
            if (x[i] != -1 && x[i] != it.second.second) cout << 0, exit(0);
            x[i] = it.second.second;
        }
        else
        {
            if (it.second.first == 2)
            {
                if (i + 1 == j)
                {
                    graph[i].push_back({i + 1, it.second.second});
                    graph[i + 1].push_back({i, it.second.second});
                    cells--;
                }
                else
                if (i + 2 == j)
                {
                    if (x[i + 1] != -1 && x[i + 1] != it.second.second) cout << 0, exit(0);
                    x[i + 1] = it.second.second;
                    cells--;
                }
                else
                {
                    if (it.second.second != 0) cout << 0, exit(0);
                    cells--;
                }
            }
            else
            {
                cells--;
            }
        }
    }
    ans = 1;
    for (int i = 0; i < n; i++)
    {
        if (c[i] == 0)
        {
            zero = -1, one = -1;
            dfs(i);
            if (zero == -1 && one == -1)
            {
                zero = i;
                x[i] = 0;
                ans = 2 * ans % MOD;
            }
            if (zero != -1)
            {
                ch(zero, 0);
            }
            else
            {
                ch(one, 1);
            }
        }
    }
    ans = ans * inq(2, cells) % MOD;
    cout << ans;
}


Measured memory_bytes: 11520