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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.AbstractMap;
import java.util.TreeMap;
import java.util.Map;
import java.io.Closeable;
import java.util.Map.Entry;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
        thread.start();
        thread.join();
    }

    static class TaskAdapter implements Runnable {
        @Override
        public void run() {
            InputStream inputStream = System.in;
            OutputStream outputStream = System.out;
            FastInput in = new FastInput(inputStream);
            FastOutput out = new FastOutput(outputStream);
            TaskF solver = new TaskF();
            solver.solve(1, in, out);
            out.close();
        }
    }
    static class TaskF {
        public void solve(int testNumber, FastInput in, FastOutput out) {
            int n = in.readInt();
            int m = in.readInt();

            boolean valid = true;
            TreeMap<Long, Integer>[] maps = new TreeMap[4];
            for (int i = 0; i < 4; i++) {
                maps[i] = new TreeMap<>();
            }
            for (int i = 0; i < m; i++) {
                int a = in.readInt();
                int b = in.readInt();
                int c = in.readInt();

                Long key = DigitUtils.asLong(a, b);
                maps[Math.min(3, Math.abs(a - b))].put(key, c);
            }
            int[] diagonal = new int[n + 1];
            for (int i = 1; i <= n; i++) {
                diagonal[i] = maps[0].getOrDefault(DigitUtils.asLong(i, i), -1);
            }
            int[] bottom = new int[n + 1];
            int[] top = new int[n + 1];
            for (int i = 2; i <= n; i++) {
                bottom[i] = maps[1].getOrDefault(DigitUtils.asLong(i, i - 1), -1);
                top[i] = maps[1].getOrDefault(DigitUtils.asLong(i - 1, i), -1);
            }
            boolean[] decided = new boolean[n + 1];
            for (int i = 3; i <= n; i++) {
                Integer up = maps[2].get(DigitUtils.asLong(i - 2, i));
                Integer bot = maps[2].get(DigitUtils.asLong(i, i - 2));
                if (up != null && bot != null) {
                    if (diagonal[i - 1] == -1) {
                        diagonal[i - 1] = (up + bot) % 2;
                    } else if (diagonal[i - 1] != (up + bot) % 2) {
                        valid = false;
                    }
                }
                if (up != null || bot != null) {
                    decided[i] = true;
                }
            }


            NumberTheory.Modular mod = new NumberTheory.Modular(998244353);
            NumberTheory.Power pow = new NumberTheory.Power(mod);

            int[][] dp = new int[n + 1][2];
            if (diagonal[1] == -1) {
                dp[1][0] = dp[1][1] = 1;
            } else {
                dp[1][diagonal[1]] = 1;
            }

            for (int i = 2; i <= n; i++) {
                if (bottom[i] != -1 && top[i] != -1) {
                    dp[i][0] = dp[i - 1][DigitUtils.mod(bottom[i] + top[i], 2)];
                    dp[i][1] = dp[i - 1][DigitUtils.mod(bottom[i] + top[i] + 1, 2)];
                } else {
                    dp[i][1] = dp[i][0] = mod.plus(dp[i - 1][0], dp[i - 1][1]);
                }
                if (diagonal[i] != -1) {
                    dp[i][1 - diagonal[i]] = 0;
                }
            }

            int totalWay = mod.plus(dp[n][0], dp[n][1]);
            for (int i = 2; i <= n; i++) {
                if (top[i] == -1 && bottom[i] == -1) {
                    totalWay = mod.mul(totalWay, 2);
                }
            }
            for (int i = 3; i <= n; i++) {
                if (!decided[i]) {
                    totalWay = mod.mul(totalWay, 2);
                }
            }

            long remain = ((long) n * n - n - 2 * (n - 1) - 2 * (n - 2)) / 2;
            while (!maps[3].isEmpty()) {
                Map.Entry<Long, Integer> kv = maps[3].pollFirstEntry();
                Long otherSide = DigitUtils.asLong(DigitUtils.lowBit(kv.getKey()), DigitUtils.highBit(kv.getKey()));
                if (maps[3].containsKey(otherSide)) {
                    Integer otherSideValue = maps[3].remove(otherSide);
                    if (!kv.getValue().equals(otherSideValue)) {
                        valid = false;
                    }
                }
                remain--;
            }

            totalWay = mod.mul(totalWay, pow.pow(2, remain));

            if (valid) {
                out.println(totalWay);
            } else {
                out.println(0);
            }
        }

    }
    static class NumberTheory {
        public static class Modular {
            int m;

            public Modular(int m) {
                this.m = m;
            }

            public Modular(long m) {
                this.m = (int) m;
                if (this.m != m) {
                    throw new IllegalArgumentException();
                }
            }

            public Modular(double m) {
                this.m = (int) m;
                if (this.m != m) {
                    throw new IllegalArgumentException();
                }
            }

            public int valueOf(int x) {
                x %= m;
                if (x < 0) {
                    x += m;
                }
                return x;
            }

            public int valueOf(long x) {
                x %= m;
                if (x < 0) {
                    x += m;
                }
                return (int) x;
            }

            public int mul(int x, int y) {
                return valueOf((long) x * y);
            }

            public int plus(int x, int y) {
                return valueOf(x + y);
            }

            public String toString() {
                return "mod " + m;
            }

        }

        public static class Power {
            final NumberTheory.Modular modular;

            public Power(NumberTheory.Modular modular) {
                this.modular = modular;
            }

            public int pow(int x, long n) {
                if (n == 0) {
                    return modular.valueOf(1);
                }
                long r = pow(x, n >> 1);
                r = modular.valueOf(r * r);
                if ((n & 1) == 1) {
                    r = modular.valueOf(r * x);
                }
                return (int) r;
            }

        }

    }
    static class DigitUtils {
        private DigitUtils() {}

        public static long asLong(int high, int low) {
            return (((long) high) << 32) | low;
        }

        public static int highBit(long x) {
            return (int) (x >> 32);
        }

        public static int lowBit(long x) {
            return (int) x;
        }

        public static int mod(int x, int mod) {
            x %= mod;
            if (x < 0) {
                x += mod;
            }
            return x;
        }

    }
    static class FastOutput implements AutoCloseable, Closeable {
        private StringBuilder cache = new StringBuilder(10 << 20);
        private final Writer os;

        public FastOutput(Writer os) {
            this.os = os;
        }

        public FastOutput(OutputStream os) {
            this(new OutputStreamWriter(os));
        }

        public FastOutput println(int c) {
            cache.append(c).append('\n');
            return this;
        }

        public FastOutput flush() {
            try {
                os.append(cache);
                os.flush();
                cache.setLength(0);
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
            return this;
        }

        public void close() {
            flush();
            try {
                os.close();
            } catch (IOException e) {
                throw new UncheckedIOException(e);
            }
        }

        public String toString() {
            return cache.toString();
        }

    }
    static class FastInput {
        private final InputStream is;
        private byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastInput(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public int readInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

    }
}