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;
                        }

                        if (p.X < p.Y)
                        {
                            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
    }
}


Measured memory_bytes: 34632