#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
#define REP(i, x, y) for(ll i = x; i < y; i++)
#define PER(i, x, y) for(ll i = x; i > y; i--)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 998244353;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-11;

ll multiply(ll x, ll y) {
    return (x % mod) * (y % mod) % mod;
}

ll modpower(ll x, ll y) {
    if (y == 0) {
        return 1;
    } else if (y == 1) {
        return x % mod;
    } else if (y % 2 == 0) {
        ll p = modpower(x, y / 2);
        return p * p % mod;
    } else {
        ll p = modpower (x, y / 2);
        return (p * p) % mod * (x % mod) % mod;
    }
}

int main() {
    ll N, M;
    cin >> N >> M;
    ll a[M], b[M], c[M];
    map<plglg, ll> mp;
    REP(i, 0, M) {
        cin >> a[i] >> b[i] >> c[i];
        a[i]--;
        b[i]--;
        mp[plglg(a[i], b[i])] = c[i];
    }
    ll num = M;
    bool ok = true;
    ll used[N];
    fill(used, used + N, false);
    REP(i, 0, N) {
        if (mp.count(plglg(i, i)) != 0) {
            used[i] = true;
        }
    }
    REP(i, 1, N - 1) {
        plglg pa = plglg(i, i);
        plglg pb = plglg(i - 1, i + 1);
        plglg pc = plglg(i + 1, i - 1);
        if (mp.count(pb) != 0 && mp.count(pc) != 0) {
            if (used[i]) {
                if ((mp[pa] + mp[pb] + mp[pc]) % 2 == 1) {
                    ok = false;
                } else {
                    num--;
                }
            } else {
                mp[pa] = (mp[pb] + mp[pc]) % 2;
                used[i] = true;
            }
        }
    }
    REP(i, 0, N - 1) {
        plglg pa = plglg(i, i);
        plglg pb = plglg(i, i + 1);
        plglg pc = plglg(i + 1, i);
        plglg pd = plglg(i + 1, i + 1);
        if (mp.count(pa) != 0 && mp.count(pb) != 0
            && mp.count(pc) != 0) {
            if (used[i + 1]) {
                if ((mp[pa] + mp[pb] + mp[pc] + mp[pd]) % 2 == 1) {
                    ok = false;
                } else {
                    num--;
                }
            } else {
                mp[pd] = (mp[pa] + mp[pb] + mp[pc]) % 2;
                used[i + 1] = true;
            }
        }
    }
    PER(i, N - 1, 0) {
        plglg pa = plglg(i, i);
        plglg pb = plglg(i, i - 1);
        plglg pc = plglg(i - 1, i);
        plglg pd = plglg(i - 1, i - 1);
        if (mp.count(pa) != 0 && mp.count(pb) != 0
            && mp.count(pc) != 0) {
            if (!used[i - 1]) {
                mp[pd] = (mp[pa] + mp[pb] + mp[pc]) % 2;
                used[i - 1] = true;
            }
        }
    }
    // REP(i, 0, N) {
    //     cout << used[i] << " ";
    // }
    // cout << endl;
    // REP(i, 0, N) {
    //     cout << mp.count(plglg(i, i)) << " ";
    // }
    // cout << endl;
    REP(i, 0, M) {
        if (a[i] < b[i]) {
            if (b[i] - a[i] >= 3) {
                if (mp.count(plglg(b[i], a[i])) != 0) {
                    if (mp[plglg(b[i], a[i])] != c[i]) {
                        ok = false;
                    } else {
                        num--;
                    }
                }
            }
            // else if (b[i] - a[i] == 2) {
            //     plglg pa = plglg(b[i], a[i]);
            //     plglg pb = plglg(a[i] + 1, b[i] - 1);
            //     if (mp.count(pa) != 0 && mp.count(pb) != 0) {
            //         if ((mp[pa] + mp[pb] + c[i]) % 2 == 1) {
            //             ok = false;
            //         } else {
            //             num--;
            //         }
            //     }
            // } else {
            //     if (mp.count(plglg(b[i], a[i])) != 0 && mp.count(plglg(a[i] + 1, b[i])) != 0
            //         && mp.count(plglg(a[i], b[i] - 1)) != 0) {
            //         if ((mp[plglg(b[i], a[i])] + mp[plglg(a[i] + 1, b[i])] + mp[plglg(a[i], b[i] - 1)]
            //              + c[i]) % 2 == 1) {
            //             ok = false;
            //         } else {
            //             num--;
            //         }
            //     }
            // }
        }
    }
    ll po = N * (N + 1) / 2;
    ll ans = modpower(2, po - num);
    if (ok) {
        cout << ans << endl;
    } else {
        cout << 0 << endl;
    }
}


Measured memory_bytes: 6912