#include <iostream>
#include <numeric>
#include <cmath>
#include <vector>
#include <map>
#include <tuple>

template <class Int>
int kthbit(Int a, int k) { return (a >> k) & 1; }

template <int MOD>
struct ModInt {
    using lint = long long;
    int val;

    // constructor
    ModInt(lint v = 0) : val(v % MOD) {
        if (val < 0) val += MOD;
    };

    // unary operator
    ModInt operator+() const { return ModInt(val); }
    ModInt operator-() const { return ModInt(MOD - val); }
    ModInt inv() const { return this->pow(MOD - 2); }

    // arithmetic
    ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
    ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
    ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
    ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }
    ModInt pow(lint n) const {
        auto x = ModInt(1);
        auto b = *this;
        while (n > 0) {
            if (n & 1) x *= b;
            n >>= 1;
            b *= b;
        }
        return x;
    }

    // compound assignment
    ModInt& operator+=(const ModInt& x) {
        if ((val += x.val) >= MOD) val -= MOD;
        return *this;
    }
    ModInt& operator-=(const ModInt& x) {
        if ((val -= x.val) < 0) val += MOD;
        return *this;
    }
    ModInt& operator*=(const ModInt& x) {
        val = lint(val) * x.val % MOD;
        return *this;
    }
    ModInt& operator/=(const ModInt& x) { return *this *= x.inv(); }

    // compare
    bool operator==(const ModInt& b) const { return val == b.val; }
    bool operator!=(const ModInt& b) const { return val != b.val; }

    // I/O
    friend std::istream& operator>>(std::istream& is, ModInt& x) noexcept { return is >> x.val; }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
};

constexpr int MOD = 998244353;
using mint = ModInt<MOD>;

using lint = long long;

const std::vector<std::vector<int>>
    sqs{{0, 1, 3, 4},
        {4, 5, 7, 8},
        {2, 4, 6}};

void solve() {
    int n;
    std::cin >> n;

    lint rem = lint(n - 2) * (n - 3) / 2;
    std::vector<std::map<int, int>> ps(n);
    {
        int m;
        std::cin >> m;
        while (m--) {
            int x, y, c;
            std::cin >> x >> y >> c;
            --x, --y;

            if (std::abs(x - y) < 3) {
                ps[x][y] = c;
            } else {
                if (x > y) std::swap(x, y);

                if (ps[x].count(y)) {
                    if (ps[x][y] != c) {
                        std::cout << 0 << std::endl;
                        return;
                    }
                } else {
                    ps[x][y] = c;
                    --rem;
                }
            }
        }
    }

    std::vector<mint> dp(1 << 4, 0);
    for (int b = 0; b < (1 << 4); ++b) {
        bool valid = true;

        int sum = 0;
        for (int i = 0; i < 2; ++i) {
            for (int j = 0; j < 2; ++j) {
                int c = kthbit(b, i * 2 + j);
                sum += c;
                if (ps[i].count(j) && ps[i][j] != c) valid = false;
            }
        }

        if (valid && sum % 2 == 0) dp[b] = 1;
    }

    std::vector<int> bs;
    for (int b = 0; b < (1 << 9); ++b) {
        bool valid = true;

        for (const auto& idxs : sqs) {
            int sum = 0;
            for (auto i : idxs) sum += kthbit(b, i);
            if (sum % 2 != 0) valid = false;
        }

        if (valid) bs.push_back(b);
    }

    std::vector<mint> ndp(1 << 4);
    for (int x = 0; x + 2 < n; ++x) {
        std::fill(ndp.begin(), ndp.end(), 0);

        for (auto b : bs) {
            bool valid = true;

            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    int c = kthbit(b, i * 3 + j);
                    int ni = i + x, nj = j + x;
                    if (ps[ni].count(nj) && ps[ni][nj] != c) valid = false;
                }
            }

            if (!valid) continue;

            int pb = 0, nb = 0;
            for (int i = 0; i < 2; ++i) {
                for (int j = 0; j < 2; ++j) {
                    pb |= (kthbit(b, i * 3 + j) << (i * 2 + j));
                    nb |= (kthbit(b, (i + 1) * 3 + (j + 1)) << (i * 2 + j));
                }
            }

            ndp[nb] += dp[pb];
        }

        std::swap(dp, ndp);
    }

    mint ans = std::accumulate(dp.begin(), dp.end(), mint(0));
    std::cout << ans * mint(2).pow(rem) << std::endl;
}

int main() {
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
