#include <cstdio>
#include <map>

using namespace std;

const int mod = 998244353;

long long dp[100000][2];
map <pair<int, int>, int> mp;

int count(int x, int y, int d) {
    int z1, z2;
    
    if (mp.count(make_pair(x, y))) {
        z1 = mp[make_pair(x, y)];
    } else {
        z1 = -1;
    }
    
    if (mp.count(make_pair(y, x))) {
        z2 = mp[make_pair(y, x)];
    } else {
        z2 = -1;
    }
    
    if (d == 0) {
        if (z1 == -1 && z2 == -1) {
            return 2;
        } else if (z1 == -1 || z2 == -1 || z1 == z2) {
            return 1;
        } else {
            return 0;
        }
    } else {
        if (z1 == -1 && z2 == -1) {
            return 2;
        } else if (z1 == -1 || z2 == -1 || z1 != z2) {
            return 1;
        } else {
            return 0;
        }
    }
}

long long pow_mod(long long x, long long y) {
    long long ans = 1;
    
    while (y > 0) {
        if (y & 1) ans = ans * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    
    return ans;
}

int main() {
    int n, m, i;
    long long p, ans;
    
    scanf("%d %d", &n, &m);
    
    p = (long long)(n - 3) * (n - 2) / 2;
    
    for (i = 0; i < m; i++) {
        int a, b, c;
        
        scanf("%d %d %d", &a, &b, &c);
        
        a--;
        b--;
        
        if (b < a - 2 || b > a + 2) {
            if (mp.count(make_pair(a, b))) {
                if (mp[make_pair(a, b)] != c) {
                    puts("0");
                    
                    return 0;
                }
            } else {
                mp[make_pair(a, b)] = mp[make_pair(b, a)] = c;
                
                p--;
            }
        } else {
            mp[make_pair(a, b)] = c;
        }
    }
    
    for (i = 0; i < n; i++) {
        if (!mp.count(make_pair(i, i)) || mp[make_pair(i, i)] == 0) {
            if (i == 0) {
                dp[0][0] = 1;
            } else if (i == 1) {
                dp[1][0] = dp[0][0] * count(0, 1, 0) + dp[0][1] * count(0, 1, 1);
            } else {
                dp[i][0] += dp[i - 1][0] * count(i - 1, i, 0) * count(i - 2, i, 0);
                dp[i][0] += dp[i - 1][1] * count(i - 1, i, 1) * count(i - 2, i, 1);
                dp[i][0] %= mod;
                
            }
        }
        
        if (!mp.count(make_pair(i, i)) || mp[make_pair(i, i)] == 1) {
            if (i == 0) {
                dp[0][1] = 1;
            } else if (i == 1) {
                dp[1][1] = dp[0][0] * count(0, 1, 1) + dp[0][1] * count(0, 1, 0);
            } else {
                dp[i][1] += dp[i - 1][0] * count(i - 1, i, 1) * count(i - 2, i, 0);
                dp[i][1] += dp[i - 1][1] * count(i - 1, i, 0) * count(i - 2, i, 1);
                dp[i][1] %= mod;
            }
        }
    }
    
    ans = (dp[n - 1][0] + dp[n - 1][1]) % mod * pow_mod(2, p) % mod;
    
    printf("%lld\n", ans);
    
    return 0;
}
