#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
const ll INF = 5e15;
const ll MOD = 998244353;
vector<P> vec;
vector<pair<P, ll>> vec2;

ll get_num(ll i, ll j){
    auto ite = lower_bound(vec.begin(), vec.end(), P(i, j));
    if(ite == vec.end()) return -1;
    if(*ite != P(i, j)) return -1;
    ll idx = ite - vec.begin();
    return vec2[idx].second;
}

bool num_able(ll i, ll j, ll n){
    ll tmp = get_num(i, j);
    return tmp == -1 || n == tmp;
}

bool can_put(ll idx, ll i, ll j, ll k, ll l){
    return num_able(idx    , idx    , i) &&
           num_able(idx    , idx + 1, j) &&
           num_able(idx + 1, idx    , k) &&
           num_able(idx + 1, idx + 1, l);
}

int main(){
    vector<ll> pow2(1e5 + 10, 1);
    for(ll i = 0; i < pow2.size() - 1; i++) pow2[i + 1] = pow2[i] * 2 % MOD;
    ll N, M;
    cin >> N >> M;
    vector<vector<ll>> tate(N), yoko(N);
    vector<P> cntv(N);
    for(ll i = 0; i < M; i++){
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        tate[b].push_back(a);
        yoko[a].push_back(b);
        vec.push_back(P(a, b));
        vec2.push_back(make_pair(P(a, b), c));
    }
    sort(vec.begin(), vec.end());
    sort(vec2.begin(), vec2.end(), [](const auto &a, const auto &b){return a.first < b.first;});
    for(ll i = 0; i < N; i++){
        sort(tate[i].begin(), tate[i].end());
        sort(yoko[i].begin(), yoko[i].end());
        reverse(tate[i].begin(), tate[i].end());
        reverse(yoko[i].begin(), yoko[i].end());
    }
    for(ll i = 2; i < N; i++){
        ll ti, yi;
        for(ti = 0; ti < tate[i].size(); ti++) if(tate[i][ti] < i - 2) break;
        for(yi = 0; yi < yoko[i].size(); yi++) if(yoko[i][yi] < i - 2) break;
        ll tmp[2];
        ll tmpu = get_num(i, i - 2), tmpl = get_num(i - 2, i);
        if(tmpu == -1 && tmpl == -1){
            tmp[0] = tmp[1] = 2;
        }else if((tmpu == 1 && tmpl == 0) ||
                 (tmpu == 0 && tmpl == 1)){
            tmp[0] = 0;
            tmp[1] = 1;
        }else if(tmpu == tmpl){
            tmp[0] = 1;
            tmp[1] = 0;
        }else{
            tmp[0] = tmp[1] = 1;
        }
        ll pre = i - 2;
        while(ti < tate[i].size() || yi < yoko[i].size()){
            ll nxt = max((ti < tate[i].size() ? tate[i][ti] : -1),
                         (yi < yoko[i].size() ? yoko[i][yi] : -1));
            if(ti < tate[i].size() && tate[i][ti] == nxt) ti++;
            if(yi < yoko[i].size() && yoko[i][yi] == nxt) yi++;
            ll tn = get_num(nxt, i);
            ll yn = get_num(i, nxt);
            if(tn == -1 && yn == -1){
                tmp[0] *= 2;
                tmp[0] %= MOD;
                tmp[1] *= 2;
                tmp[1] %= MOD;
            }else if((tn == 1 && yn == 0) ||
                     (tn == 0 && yn == 1)){
                tmp[0] = tmp[1] = 0;
            }
            ll p = pow2[pre - nxt - 1];
            tmp[0] *= p;
            tmp[0] %= MOD;
            tmp[1] *= p;
            tmp[1] %= MOD;
            pre = nxt;
        }
        ll p = pow2[pre];
        cntv[i] = P(tmp[0] * p % MOD, tmp[1] * p % MOD);
    }
    //for(ll i = 2; i < N; i++) cout << cntv[i].first << ", " << cntv[i].second << endl;
    ll dp[2] = {};
    for(ll i = 0; i <= 1; i++) for(ll j = 0; j <= 1; j++) for(ll k = 0; k <= 1; k++) for(ll l = 0; l <= 1; l++) {
        if(!can_put(0, i, j, k, l)) continue;
        ll tmp = i + j + k + l;
        if(tmp & 1) continue;
        dp[l]++;
    }
    //cout << dp[0] << ", " << dp[1] << endl;
    for(ll idx = 2; idx < N; idx++){
        ll dpu[2] = {};
        for(ll i = 0; i <= 1; i++) for(ll j = 0; j <= 1; j++) for(ll k = 0; k <= 1; k++) for(ll l = 0; l <= 1; l++) {
            if(!can_put(idx - 1, i, j, k, l)) continue;
            ll tmp = i + j + k + l;
            if(tmp & 1) continue;
            tmp -= i;
            if(tmp & 1) dpu[l] = (dpu[l] + dp[i] * cntv[idx].second % MOD) % MOD;
            else dpu[l] = (dpu[l] + dp[i] * cntv[idx].first % MOD) % MOD;
        }
        dp[0] = dpu[0];
        dp[1] = dpu[1];
        //cout << dp[0] << ", " << dp[1] << endl;
    }
    cout << (dp[0] + dp[1]) % MOD << endl;
    return 0;
}


Measured memory_bytes: 13736