Problem:
Score : 900 points

Problem Statement

Takahashi has an N \times N grid. The square at the i-th row and the j-th column of the grid is denoted by (i,j).
Particularly, the top-left square of the grid is (1,1), and the bottom-right square is (N,N).

An integer, 0 or 1, is written on M of the squares in the Takahashi's grid.
Three integers a_i,b_i and c_i describe the i-th of those squares with integers written on them: the integer c_i is written on the square (a_i,b_i).

Takahashi decides to write an integer, 0 or 1, on each of the remaining squares so that the condition below is satisfied.
Find the number of such ways to write integers, modulo 998244353.

For all 1\leq i < j\leq N, there are even number of 1s in the square region whose top-left square is (i,i) and whose bottom-right square is (j,j).

Constraints

2 \leq N \leq 10^5

0 \leq M \leq min(5 \times 10^4,N^2)

1 \leq a_i,b_i \leq N(1\leq i\leq M)

0 \leq c_i \leq 1(1\leq i\leq M)

If i \neq j, then (a_i,b_i) \neq (a_j,b_j).

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
:
a_M b_M c_M

Output

Print the number of possible ways to write integers, modulo 998244353.

Sample Input 1

3 3
1 1 1
3 1 0
2 3 1

Sample Output 1

8

For example, the following ways to write integers satisfy the condition:

101 111
011 111
000 011

Sample Input 2

4 5
1 3 1
2 4 0
2 3 1
4 2 1
4 4 1

Sample Output 2

32

Sample Input 3

3 5
1 3 1
3 3 0
3 1 0
2 3 1
3 2 1

Sample Output 3

0

Sample Input 4

4 8
1 1 1
1 2 0
3 2 1
1 4 0
2 1 1
1 3 0
3 4 1
4 4 1

Sample Output 4

4

Sample Input 5

100000 0

Sample Output 5

342016343

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <array>
using namespace std;

#define SZ(a) (int)(a).size()
using ll = long long;

constexpr ll mod = 998244353;

ll powmod(ll b, ll e) {
    ll r = 1;
    while (e != 0) {
        if (e & 1) {
            r = (r * b) % mod;
        }
        b = (b * b) % mod;
        e >>= 1;
    }
    return r;
}

int main() {
    cin.sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> a(n);
    for (int i = 0; i < m; i++) {
        int x, y, c;
        cin >> x >> y >> c;
        x--; y--;
        a[max(x, y)].emplace_back(min(x, y), c);
    }
    for (auto &b: a) {
        sort(begin(b), end(b));
    }
    array<ll, 2> dp = {1, 0};
    array<bool, 2> posb = {true, false};
    bool pos = true;
    ll s = 0;
    for (int i = 0; i < n; i++) {
        // cerr << s << " " << dp[0] << " " << dp[1] << endl;
        s += i;
        const auto &b = a[i];
        // for (auto p: b) {
        //     cerr << p.first << "," << p.second << " ";
        // }
        // cerr << endl;
        int k;
        for (k = 0; k < SZ(b); ) {
            int l = b[k].first;
            if (l >= i - 2) break;
            int c = 0;
            int p = 0;
            while (k < SZ(b) && b[k].first == l) {
                c++;
                p ^= b[k].second & 1;
                k++;
            }
            if (p != 0) {
                pos = false;
                break;
            }
            s--;
        }
        if (!pos) {
            break;
        }
        array<int, 3> cs = {0, 0, 0};
        array<int, 3> ps = {0, 0, 0};
        for (; k < SZ(b); ) {
            int l = b[k].first;
            int c = 0;
            int p = 0;
            while (k < SZ(b) && b[k].first == l) {
                c++;
                p ^= b[k].second & 1;
                k++;
            }
            if (l == i || c == 2) {
                cs[i - l] = c;
                ps[i - l] = p;
            }
            if (l != i) {
                s--;
            }
        }
        // cerr << ps[0] << endl;
        array<ll, 2> dp1 = {0, 0};
        array<bool, 2> posb1 = {false, false};
        for (int j = 0; j < 2; j++) {
            if (cs[0] == 1 && ps[0] != j) continue;
            for (int k = 0; k < 2; k++) {
                if (!(cs[1] == 2 && ps[1] != j ^ k || cs[2] == 2 && ps[2] != k) && posb[k]) {
                    // cerr << i << " " << j << k << endl;
                    dp1[j] += dp[k];
                    posb1[j] = true;
                }
            }
            dp1[j] %= mod;
        }
        if (dp1[0] == 0 && dp1[1] == 0) {
            pos = false;
            break;
        }
        swap(dp, dp1);
        swap(posb, posb1);
    }
    if (!pos) {
        puts("0");
        return 0;
    }
    // cerr << s << " " << dp[0] << " " << dp[1] << endl;
    ll ans = powmod(2, s) * (dp[0] + dp[1]) % mod;
    printf("%lld\n", ans);
    return 0;
}