#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; (i) < (int) (n); (i) ++)
using namespace std;

#define mp make_pair
const long long mod = 998244353;

long long mod_pow(long long x, long long a) {
        long long res = 1;
        while (a) {
                if (a & 1) res = res * x % mod;
                x = x * x % mod;
                a >>= 1;
        }
        return res;
}

int main() {
        int n, m;
        scanf("%d%d", &n, &m);
        map<pair<int, int>, int> val;
        rep(i, m) {
                int a, b, c;
                scanf("%d%d%d", &a, &b, &c);
                a --, b --;
                val[make_pair(a, b)] = c;
        }
        if (n == 2) {
                int ans = 0;
                for (int s = 0; s < (1 << 4); s ++) {
                        vector<vector<int>> w(2, vector<int> (2));
                        bool ng = false;
                        for (int t = 0; t < 4; t ++) {
                                int y = t / 2;
                                int x = t % 2;
                                int v = (s >> t) & 1;
                                if (val.count(mp(y, x)) && val[mp(y, x)] != v) { 
                                        ng = true;
                                        break;
                                }
                        }
                        if (ng) continue;
                        if (__builtin_popcount(s) & 1) continue;
                        ans ++;
                }
                printf("%d\n", ans);
                return 0;
        }
        vector<long long> cur(2);
        for (int s = 0; s < (1 << 9); s ++) {
                vector<vector<int>> w(3, vector<int> (3));
                bool ng = false;
                for (int t = 0; t < 9; t ++) {
                        int y = t / 3;
                        int x = t % 3;
                        int v = (s >> t) & 1;
                        if (val.count(mp(y, x)) && val[mp(y, x)] != v) { 
                                ng = true;
                                break;
                        }
                }
                if (ng) continue;
                if (__builtin_popcount(s) & 1) continue;
                if ((((s >> 0) & 1) + ((s >> 1) & 1) + ((s >> 3) & 1) + ((s >> 4) & 1)) & 1) continue;
                if ((((s >> 4) & 1) + ((s >> 5) & 1) + ((s >> 7) & 1) + ((s >> 8) & 1)) & 1) continue;
                cur[(s >> 8) & 1] ++;
        }

        for (int i = 3; i < n; i ++) {
                vector<long long> next(2);
                // prev = 0
                {
                        long long add = cur[0];
                        if (val.count(mp(i, i - 2)) || val.count(mp(i - 2, i))) {
                                if (val.count(mp(i, i - 2)) && val.count(mp(i - 2, i))) {
                                        if (val[mp(i, i - 2)] != val[mp(i - 2, i)]) {
                                                add = 0;
                                        }
                                }
                        } else {
                                (add *= 2) %= mod;
                        }
                        // next = 0
                        long long add2 = add;
                        if (!val.count(mp(i, i)) || val[mp(i, i)] == 0) {
                                if (val.count(mp(i, i - 1)) || val.count(mp(i - 1, i))) {
                                        if (val.count(mp(i, i - 1)) && val.count(mp(i - 1, i))) {
                                                if (val[mp(i, i - 1)] != val[mp(i - 1, i)]) {
                                                        add = 0;
                                                }
                                        }
                                } else {
                                        (add *= 2) %= mod;
                                }
                                (next[0] += add) %= mod;
                        }
                        // next = 1
                        if (!val.count(mp(i, i)) || val[mp(i, i)] == 1) {
                                if (val.count(mp(i, i - 1)) || val.count(mp(i - 1, i))) {
                                        if (val.count(mp(i, i - 1)) && val.count(mp(i - 1, i))) {
                                                if (val[mp(i, i - 1)] == val[mp(i - 1, i)]) {
                                                        add2 = 0;
                                                }
                                        }
                                } else {
                                        (add2 *= 2) %= mod;
                                }
                                (next[1] += add2) %= mod;
                        }
                }
                // prev = 1
                {
                        long long add = cur[1];
                        if (val.count(mp(i, i - 2)) || val.count(mp(i - 2, i))) {
                                if (val.count(mp(i, i - 2)) && val.count(mp(i - 2, i))) {
                                        if (val[mp(i, i - 2)] == val[mp(i - 2, i)]) {
                                                add = 0;
                                        }
                                }
                        } else {
                                (add *= 2) %= mod;
                        }
                        long long add2 = add;
                        // next = 0
                        if (!val.count(mp(i, i)) || val[mp(i, i)] == 0) {
                                if (val.count(mp(i, i - 1)) || val.count(mp(i - 1, i))) {
                                        if (val.count(mp(i, i - 1)) && val.count(mp(i - 1, i))) {
                                                if (val[mp(i, i - 1)] == val[mp(i - 1, i)]) {
                                                        add = 0;
                                                }
                                        }
                                } else {
                                        (add *= 2) %= mod;
                                }
                                (next[0] += add) %= mod;
                        }
                        // next = 1
                        if (!val.count(mp(i, i)) || val[mp(i, i)] == 1) {
                                if (val.count(mp(i, i - 1)) || val.count(mp(i - 1, i))) {
                                        if (val.count(mp(i, i - 1)) && val.count(mp(i - 1, i))) {
                                                if (val[mp(i, i - 1)] != val[mp(i - 1, i)]) {
                                                        add2 = 0;
                                                }
                                        }
                                } else {
                                        (add2 *= 2) %= mod;
                                }
                                (next[1] += add2) %= mod;
                        }
                }
                swap(cur[0], next[0]);
                swap(cur[1], next[1]);
        }

        long long rem = (long long) (n - 3) * (n - 2);
        bool ng = false;
        for (auto it : val) {
                int a = it.first.first;
                int b = it.first.second;
                int c = it.second;
                if (abs(a - b) <= 2) continue;
                if (val.count(mp(b, a))) {
                        if (c != val[mp(b, a)]) {
                                ng = true;
                                break;
                        } else {
                                rem -= 1;
                        }
                } else {
                        rem -= 2;
                }
        }
        if (ng) {
                printf("0\n");
                return 0;
        }
        assert(rem >= 0);
        rem /= 2;
        long long coef = mod_pow(2, rem);
        long long ans = ((cur[0] + cur[1]) * coef % mod) % mod;
        printf("%lld\n", ans);
        return 0;
}


Measured memory_bytes: 3328