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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace ReadWriteTemplate
{
    public static class Solver
    {
        private static void SolveCase()
        {
            /*int n = 5;
            Build(0, n, new int[n, n]);*/

            int n = ReadInt();
            int m = ReadInt();
            var points = new List<Point>();
            var dict = new Dictionary<Point, int>();
            var layers = new long[n];
            var diag = Enumerable.Repeat(-1, n).ToArray();
            var up = Enumerable.Repeat(-1, n).ToArray();
            var up2 = Enumerable.Repeat(-1, n).ToArray();
            var down = Enumerable.Repeat(-1, n).ToArray();
            var down2 = Enumerable.Repeat(-1, n).ToArray();
            for (int i = 0; i < m; i++)
            {
                var point = new Point(ReadInt() - 1, ReadInt() - 1, ReadInt());
                points.Add(point);
                if (point.X == point.Y)
                {
                    diag[point.X] = point.Value;
                }
                else if (point.X == point.Y + 1)
                {
                    up[point.X] = point.Value;
                }
                else if (point.X + 1 == point.Y)
                {
                    down[point.Y] = point.Value;
                }
                else if (point.X == point.Y + 2)
                {
                    up2[point.X] = point.Value;
                }
                else if (point.X + 2 == point.Y)
                {
                    down2[point.Y] = point.Value;
                }
                else
                {
                    dict.Add(point, point.Value);
                    layers[Math.Max(point.X, point.Y)]++;
                }
            }

            foreach (var p in points)
            {
                if (Math.Abs(p.X - p.Y) > 2)
                {
                    var p2 = new Point(p.Y, p.X, 0);
                    if (dict.ContainsKey(p2))
                    {
                        if (dict[p2] != p.Value)
                        {
                            Writer.WriteLine(0);
                            return;
                        }

                        layers[Math.Max(p.X, p.Y)]--;
                    }
                }
            }

            long[][] a = new long[2][];
            for (int i = 0; i < 2; i++)
            {
                a[i] = new long[n];
                if (diag[0] != 1 - i)
                {
                    a[i][0] = 1;
                }
            }

            const long MOD = 998244353;

            var pow2 = new long[n];
            pow2[0] = 1;
            for (int i = 1; i < n; i++)
            {
                pow2[i] = pow2[i - 1] << 1;
                if (pow2[i] >= MOD)
                {
                    pow2[i] -= MOD;
                }
            }

            for (int i = 1; i < n; i++)
            {
                for (int dv = 0; dv < 2; dv++)
                {
                    if (diag[i] == 1 - dv)
                    {
                        continue;
                    }

                    for (int pdv = 0; pdv < 2; pdv++)
                    {
                        for (int upv = 0; upv < 2; upv++)
                        {
                            if (up[i] == 1 - upv)
                            {
                                continue;
                            }

                            int downv = (dv + pdv + upv) & 1;

                            if (down[i] == 1 - downv)
                            {
                                continue;
                            }

                            if (i > 1)
                            {
                                for (int upv2 = 0; upv2 < 2; upv2++)
                                {
                                    if (up2[i] == 1 - upv2)
                                    {
                                        continue;
                                    }

                                    int downv2 = (pdv + upv2) & 1;

                                    if (down2[i] == 1 - downv2)
                                    {
                                        continue;
                                    }

                                    long p = i > 2 ? pow2[i - layers[i] - 2] : 1;
                                    a[dv][i] = (a[dv][i] + a[pdv][i - 1] * p) % MOD;
                                }
                            }
                            else
                            {
                                a[dv][i] = (a[dv][i] + a[pdv][i - 1]) % MOD;
                            }
                        }
                    }
                }
            }

            long ans = (a[0][n - 1] + a[1][n - 1]) % MOD;

            // WriteArray(a[0]);
            // WriteArray(a[1]);

            Writer.WriteLine(ans);
        }

        public class Point
        {
            public Point(int x, int y, int value)
            {
                X = x;
                Y = y;
                Value = value;
            }

            public int X;

            public int Y;

            public int Value;

            protected bool Equals(Point other)
            {
                return X == other.X && Y == other.Y;
            }

            public override bool Equals(object obj)
            {
                if (ReferenceEquals(null, obj)) return false;
                if (ReferenceEquals(this, obj)) return true;
                if (obj.GetType() != this.GetType()) return false;
                return Equals((Point) obj);
            }

            public override int GetHashCode()
            {
                unchecked
                {
                    return (X * 397) ^ Y;
                }
            }
        }

        private static void Build(int t, int n, int[,] a)
        {
            if (t >= n * n)
            {
                for (int i = 0; i < n; i++)
                {
                    for (int j = i + 1; j < n; j++)
                    {
                        int c = 0;
                        for (int ii = i; ii <= j; ii++)
                        {
                            for (int jj = i; jj <= j; jj++)
                            {
                                c += a[ii, jj];
                            }
                        }
                        if (c % 2 != 0)
                        {
                            return;
                        }
                    }
                }

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        Writer.Write(a[i, j]);
                    }
                    Writer.WriteLine();
                }
                Writer.WriteLine();

                return;
            }

            for (int i = 0; i < 2; i++)
            {
                a[t / n, t % n] = i;
                Build(t + 1, n, a);
            }
        }

        public static void Solve()
        {
            // var sw = Stopwatch.StartNew();

            SolveCase();

            /*int T = ReadInt();
            for (int i = 0; i < T; i++)
            {
                // Writer.WriteLine("Case {0}:", i + 1);
                // Writer.Write("Case #{0}: ", i + 1);
                SolveCase();
            }*/

            // sw.Stop();
            // Console.WriteLine(sw.ElapsedMilliseconds);
        }

        public static void Main()
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

#if DEBUG
            // Reader = Console.In; Writer = Console.Out;
            Reader = File.OpenText("input.txt"); Writer = File.CreateText("output.txt");
#else
            Reader = Console.In; Writer = Console.Out;
#endif

            // Solve();
            Thread thread = new Thread(Solve, 64 * 1024 * 1024);
            thread.CurrentCulture = CultureInfo.InvariantCulture;
            thread.Start();
            thread.Join();

            Reader.Close();
            Writer.Close();
        }

        public static IOrderedEnumerable<TSource> OrderByWithShuffle<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            return source.Shuffle().OrderBy(keySelector);
        }

        public static T[] Shuffle<T>(this IEnumerable<T> source)
        {
            T[] result = source.ToArray();
            Random rnd = new Random();
            for (int i = result.Length - 1; i >= 1; i--)
            {
                int k = rnd.Next(i + 1);
                T tmp = result[k];
                result[k] = result[i];
                result[i] = tmp;
            }
            return result;
        }

        #region Read/Write

        private static TextReader Reader;

        private static TextWriter Writer;

        private static Queue<string> CurrentLineTokens = new Queue<string>();

        private static string[] ReadAndSplitLine()
        {
            return Reader.ReadLine().Split(new[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
        }

        public static string ReadToken()
        {
            while (CurrentLineTokens.Count == 0)
                CurrentLineTokens = new Queue<string>(ReadAndSplitLine());
            return CurrentLineTokens.Dequeue();
        }

        public static string ReadLine()
        {
            return Reader.ReadLine();
        }

        public static int ReadInt()
        {
            return int.Parse(ReadToken());
        }

        public static long ReadLong()
        {
            return long.Parse(ReadToken());
        }

        public static double ReadDouble()
        {
            return double.Parse(ReadToken(), CultureInfo.InvariantCulture);
        }

        public static int[] ReadIntArray()
        {
            return ReadAndSplitLine().Select(int.Parse).ToArray();
        }

        public static long[] ReadLongArray()
        {
            return ReadAndSplitLine().Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray();
        }

        public static int[][] ReadIntMatrix(int numberOfRows)
        {
            int[][] matrix = new int[numberOfRows][];
            for (int i = 0; i < numberOfRows; i++)
                matrix[i] = ReadIntArray();
            return matrix;
        }

        public static string[] ReadLines(int quantity)
        {
            string[] lines = new string[quantity];
            for (int i = 0; i < quantity; i++)
                lines[i] = Reader.ReadLine().Trim();
            return lines;
        }

        public static void WriteArray<T>(IEnumerable<T> array)
        {
            Writer.WriteLine(string.Join(" ", array));
        }

        #endregion
    }
}