#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

const int MOD = 998244353;

struct triple
{
    int x, y, val;
    triple(int a = 0, int b = 0, int c = 0) {x = a, y = b, val = c;}
};

int step[100005], mas[100005];
vector <triple> posit[100005];
int dp[100005][5];

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(0); cin.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        int x, y, val;
        cin >> x >> y >> val;
        posit[max(x, y)].pb(triple(x, y, val));
    }
    step[0] = 1;
    for (int i = 1; i <= n; i++) step[i] = 2 * step[i - 1] % MOD;

    dp[1][0] = 1, dp[1][1] = 1;
    if (!posit[1].empty()) dp[1][1 - posit[1][0].val] = 0;
    for (int i = 1; i <= n; i++) mas[i] = -1;

    for (int i = 2; i <= n; i++)
    {
        int open = i - 1;
        bool fl = true;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            if (x == i && y == i && val != 0) fl = false;
            if (x == i && y == i) continue;

            if (mas[min(x, y)] == -1) mas[min(x, y)] = val, open--;
            else
                if (mas[min(x, y)] != val) fl = false;
        }
        if (!fl) dp[i][0] = 0;
        else dp[i][0] = (ll) dp[i - 1][0] * (ll) step[open] % (ll) MOD;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            mas[min(x, y)] = -1;
        }

        open = i - 1;
        fl = true;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            if (x == i && y == i && val != 1) fl = false;
            if (x == i && y == i) continue;

            if (mas[min(x, y)] == -1) mas[min(x, y)] = val, open--;
            else
            {
                if (min(x, y) == i - 1)
                {
                    if (mas[min(x, y)] == val) fl = false;
                }
                else
                {
                    if (mas[min(x, y)] != val) fl = false;
                }
            }
        }
        if (!fl) dp[i][1] = 0;
        else dp[i][1] = (ll) dp[i - 1][0] * (ll) step[open] % (ll) MOD;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            mas[min(x, y)] = -1;
        }

        open = i - 1;
        fl = true;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            if (x == i && y == i && val != 0) fl = false;
            if (x == i && y == i) continue;

            if (mas[min(x, y)] == -1) mas[min(x, y)] = val, open--;
            else
            {
                if (min(x, y) == i - 1)
                    if (mas[min(x, y)] == val) fl = false;
                if (min(x, y) == i - 2)
                    if (mas[min(x, y)] == val) fl = false;
                if (min(x, y) < i - 2)
                    if (mas[min(x, y)] != val) fl = false;
            }
        }
        if (fl) dp[i][0] = (dp[i][0] + (ll) dp[i - 1][1] * (ll) step[open] % (ll) MOD) % MOD;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            mas[min(x, y)] = -1;
        }

        open = i - 1;
        fl = true;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            if (x == i && y == i && val != 1) fl = false;
            if (x == i && y == i) continue;

            if (mas[min(x, y)] == -1) mas[min(x, y)] = val, open--;
            else
            {
                if (min(x, y) == i - 1)
                    if (mas[min(x, y)] != val) fl = false;
                if (min(x, y) == i - 2)
                    if (mas[min(x, y)] == val) fl = false;
                if (min(x, y) < i - 2)
                    if (mas[min(x, y)] != val) fl = false;
            }
        }
        if (fl) dp[i][1] = (dp[i][1] + (ll) dp[i - 1][1] * (ll) step[open] % (ll) MOD) % MOD;
        for (int j = 0; j < (int) posit[i].size(); j++)
        {
            int x = posit[i][j].x, y = posit[i][j].y, val = posit[i][j].val;
            mas[min(x, y)] = -1;
        }
    }
    cout << (dp[n][0] + dp[n][1]) % MOD;
    return 0;
}


Measured memory_bytes: 6912