#include<bits/stdc++.h>
using namespace std;
void *wmem;
template<class T1, class T2> void sortA_L(int N, T1 a[], T2 b[], void *mem = wmem){
  int i;
  pair<T1, T2> *arr=(pair<T1, T2> *) mem;
  for(i=0;i<N;i++){
    arr[i].first = a[i];
    arr[i].second = b[i];
  }
  sort(arr, arr+N);
  for(i=0;i<N;i++){
    a[i] = arr[i].first;
    b[i] = arr[i].second;
  }
}
struct mint{
  static unsigned R, RR, Rinv, W, md, mdninv;
  unsigned val;
  mint(){
  }
  mint(int a){
    val = mulR(a);
  }
  mint(unsigned a){
    val = mulR(a);
  }
  mint(long long a){
    val = mulR(a);
  }
  mint(unsigned long long a){
    val = mulR(a);
  }
  int get_inv(long long a, int md){
    long long e, s=md, t=a, u=1, v=0;
    while(s){
      e=t/s;
      t-=e*s;
      u-=e*v;
      swap(t,s);
      swap(u,v);
    }
    if(u<0){
      u+=md;
    }
    return u;
  }
  void setmod(unsigned m){
    int i;
    unsigned t;
    W = 32;
    md = m;
    R = (1ULL << W) % md;
    RR = (unsigned long long)R*R % md;
    switch(m){
      case 104857601:
      Rinv = 2560000;
      mdninv = 104857599;
      break;
      case 998244353:
      Rinv = 232013824;
      mdninv = 998244351;
      break;
      case 1000000007:
      Rinv = 518424770;
      mdninv = 2226617417U;
      break;
      case 1000000009:
      Rinv = 171601999;
      mdninv = 737024967;
      break;
      case 1004535809:
      Rinv = 234947584;
      mdninv = 1004535807;
      break;
      case 1007681537:
      Rinv = 236421376;
      mdninv = 1007681535;
      break;
      case 1012924417:
      Rinv = 238887936;
      mdninv = 1012924415;
      break;
      case 1045430273:
      Rinv = 254466304;
      mdninv = 1045430271;
      break;
      case 1051721729:
      Rinv = 257538304;
      mdninv = 1051721727;
      break;
      default:
      Rinv = get_inv(R, md);
      mdninv = 0;
      t = 0;
      for(i=0;i<(int)W;i++){
        if(t%2==0){
          t+=md;
          mdninv |= (1U<<i);
        }
        t /= 2;
      }
    }
  }
  unsigned mulR(unsigned a){
    return (unsigned long long)a*R%md;
  }
  unsigned mulR(int a){
    if(a < 0){
      a = a%md+md;
    }
    return mulR((unsigned)a);
  }
  unsigned mulR(unsigned long long a){
    return mulR((unsigned)(a%md));
  }
  unsigned mulR(long long a){
    a %= md;
    if(a < 0){
      a += md;
    }
    return mulR((unsigned)a);
  }
  unsigned reduce(unsigned T){
    unsigned m=T * mdninv, t=(unsigned)((T + (unsigned long long)m*md) >> W);
    if(t >= md){
      t -= md;
    }
    return t;
  }
  unsigned reduce(unsigned long long T){
    unsigned m=(unsigned)T * mdninv, t=(unsigned)((T + (unsigned long long)m*md) >> W);
    if(t >= md){
      t -= md;
    }
    return t;
  }
  unsigned get(){
    return reduce(val);
  }
  mint &operator+=(mint a){
    val += a.val;
    if(val >= md){
      val -= md;
    }
    return *this;
  }
  mint &operator-=(mint a){
    if(val < a.val){
      val = val + md - a.val;
    }
    else{
      val -= a.val;
    }
    return *this;
  }
  mint &operator*=(mint a){
    val = reduce((unsigned long long)val*a.val);
    return *this;
  }
  mint &operator/=(mint a){
    return *this *= a.inverse();
  }
  mint operator+(mint a){
    return mint(*this)+=a;
  }
  mint operator-(mint a){
    return mint(*this)-=a;
  }
  mint operator*(mint a){
    return mint(*this)*=a;
  }
  mint operator/(mint a){
    return mint(*this)/=a;
  }
  mint operator+(int a){
    return mint(*this)+=mint(a);
  }
  mint operator-(int a){
    return mint(*this)-=mint(a);
  }
  mint operator*(int a){
    return mint(*this)*=mint(a);
  }
  mint operator/(int a){
    return mint(*this)/=mint(a);
  }
  mint operator+(long long a){
    return mint(*this)+=mint(a);
  }
  mint operator-(long long a){
    return mint(*this)-=mint(a);
  }
  mint operator*(long long a){
    return mint(*this)*=mint(a);
  }
  mint operator/(long long a){
    return mint(*this)/=mint(a);
  }
  mint operator-(void){
    mint res;
    if(val){
      res.val=md-val;
    }
    else{
      res.val=0;
    }
    return res;
  }
  operator bool(void){
    return val!=0;
  }
  operator int(void){
    return get();
  }
  operator long long(void){
    return get();
  }

  mint inverse(){
    int a = val, b = md, u = 1, v = 0, t;
    mint res;
    while(b){
      t = a / b;
      a -= t * b; swap(a, b);
      u -= t * v; swap(u, v);
    }
    if(u < 0) u += md;
    res.val = (unsigned long long)u*RR % md;
    return res;
  }

  mint pw(unsigned long long b){
    mint a(*this), res;
    res.val = R;
    while(b){
      if(b&1) res *= a;
      b >>= 1;
      a *= a;
    }
    return res;
  }

  bool operator==(int a){return mulR(a)==val;}
  bool operator!=(int a){return mulR(a)!=val;}
};
unsigned mint::md, mint::W, mint::R, mint::Rinv, mint::mdninv, mint::RR;
mint operator+(int a, mint b){return mint(a)+=b;
}
mint operator-(int a, mint b){
  return mint(a)-=b;
}
mint operator*(int a, mint b){
  return mint(a)*=b;
}
mint operator/(int a, mint b){
  return mint(a)/=b;
}
mint operator+(long long a, mint b){
  return mint(a)+=b;
}
mint operator-(long long a, mint b){
  return mint(a)-=b;
}
mint operator*(long long a, mint b){
  return mint(a)*=b;
}
mint operator/(long long a, mint b){
  return mint(a)/=b;
}
void rd(int &x){
  int k, m=0;
  x=0;
  for(;;){
    k = getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
void wt_L(int x){
  char f[10];
  int m=0, s=0;
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    putchar_unlocked('-');
  }
  while(s--){
    putchar_unlocked(f[s]+'0');
  }
}
void wt_L(mint x){
  int i;
  i = (int)x;
  wt_L(i);
}
template<class S, class T> inline S min_L(S a,T b){
  return a<=b?a:b;
}
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
char memarr[96000000];
int A[50000], B[50000], C[50000], M, N, pp[110000], sz, vv[110000];
mint cnt[2][2][100010], dp[2][100010], pw2[100100];
vector<int> p[110000], v[110000];
#define MD 998244353
int main(){
  int i, j, k, nw, pt, s, t, x1, x2, x3, x4, x5, y1, y2, y3, y4, y5;
  mint res, val;
  wmem = memarr;
  {
    mint x;
    x.setmod(MD);
  }
  pw2[0] = 1;
  for(i=1;i<100100;i++){
    pw2[i] = pw2[i-1] * 2;
  }
  rd(N);
  rd(M);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=0;Lj4PdHRW<M;Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
      rd(B[Lj4PdHRW]);
      rd(C[Lj4PdHRW]);
    }
  }
  {
    int KL2GvlyY;
    for(KL2GvlyY=0;KL2GvlyY<(M-1) + 1;KL2GvlyY++){
      A[KL2GvlyY]--;
    }
  }
  {
    int Q5VJL1cS;
    for(Q5VJL1cS=0;Q5VJL1cS<(M-1) + 1;Q5VJL1cS++){
      B[Q5VJL1cS]--;
    }
  }
  for(i=0;i<M;i++){
    j =min_L(A[i], B[i]);
    k =max_L(A[i], B[i]);
    p[j].push_back(k-j);
    v[j].push_back(C[i]);
  }
  for(s=0;s<2;s++){
    for(t=0;t<2;t++){
      cnt[s][t][N-1] = 0;
    }
  }
  for(s=0;s<2;s++){
    if(p[N-1].size()==0 || v[N-1][0]==s){
      cnt[s][0][N-1] = 1;
    }
  }
  for(k=0;k<N-1;k++){
    sz = p[k].size();
    for(i=0;i<sz;i++){
      pp[i] = p[k][i];
    }
    for(i=0;i<sz;i++){
      vv[i] = v[k][i];
    }
    sortA_L(sz, pp, vv);
    for(s=0;s<2;s++){
      for(t=0;t<2;t++){
        pt = 0;
        y1 = y2 = y3 = y4 = y5 = -1;
        while(pt < sz && pp[pt]==0){
          y1 = vv[pt];
          pt++;
        }
        while(pt < sz && pp[pt]==1){
          if(y2==-1){
            y2 = vv[pt];
          }
          else{
            y3 = vv[pt];
          }
          pt++;
        }
        while(pt < sz && pp[pt]==2){
          if(y4==-1){
            y4 = vv[pt];
          }
          else{
            y5 = vv[pt];
          }
          pt++;
        }
        if(k==N-2){
          y4 = y5 = 0;
        }
        val = 0;
        for(x1=0;x1<2;x1++){
          for(x2=0;x2<2;x2++){
            for(x3=0;x3<2;x3++){
              for(x4=0;x4<2;x4++){
                for(x5=0;x5<2;x5++){
                  if(y1 != -1 && y1 != x1){
                    continue;
                  }
                  if(y2 != -1 && y2 != x2){
                    continue;
                  }
                  if(y3 != -1 && y3 != x3){
                    continue;
                  }
                  if(y4 != -1 && y4 != x4){
                    continue;
                  }
                  if(y5 != -1 && y5 != x5){
                    continue;
                  }
                  if( (s+x1)%2 ){
                    continue;
                  }
                  if( (x1+x2+x3+t)%2 ){
                    continue;
                  }
                  if( k!=N-2 && (x1+x2+x3+x4+x5)%2 ){
                    continue;
                  }
                  val += 1;
                }
              }
            }
          }
        }
        nw = 2;
        if(k != N-2){
          nw++;
        }
        while(pt < sz){
          if(pt + 1 < sz && pp[pt]==pp[pt+1]){
            if(vv[pt] != vv[pt+1]){
              val = 0;
            }
            pt++;
          }
          val *= pw2[pp[pt] - nw];
          nw = pp[pt] + 1;
          pt++;
        }
        val *= pw2[N-k-nw];
        cnt[s][t][k] = val;
      }
    }
  }
  for(s=0;s<2;s++){
    dp[s][N-1] = cnt[s][0][N-1];
  }
  for(k=N-2;k>=0;k--){
    dp[0][k] = cnt[0][0][k] * dp[0][k+1] + cnt[0][1][k] * dp[1][k+1];
    dp[1][k] = cnt[1][0][k] * dp[0][k+1] + cnt[1][1][k] * dp[1][k+1];
  }
  res = dp[0][0] + dp[1][0];
  wt_L(res);
  putchar_unlocked('\n');
  return 0;
}
// cLay varsion 20180730-1

// --- original code ---
// #define MD 998244353
// 
// int N, M;
// int A[50000], B[50000], C[50000];
// 
// vector<int> p[110000], v[110000];
// int pp[110000], vv[110000], sz;
// 
// mint cnt[2][2][100010];
// mint pw2[100100];
// mint dp[2][100010];
// 
// {
//   int i, j, k;
//   int s, t, pt, nw;
//   int x1, x2, x3, x4, x5;
//   int y1, y2, y3, y4, y5;
//   mint val;
//   mint res;
// 
//   pw2[0] = 1;
//   rep(i,1,100100) pw2[i] = pw2[i-1] * 2;
// 
//   rd(N,M,(A,B,C)(M));
//   A[0..M-1]--;
//   B[0..M-1]--;
// 
//   rep(i,M){
//     j = min(A[i],B[i]);
//     k = max(A[i],B[i]);
//     p[j].push_back(k-j);
//     v[j].push_back(C[i]);
//   }
// 
//   rep(s,2) rep(t,2) cnt[s][t][N-1] = 0;
//   rep(s,2){
//     if(p[N-1].size()==0 || v[N-1][0]==s) cnt[s][0][N-1] = 1;
//   }
// 
//   rep(k,N-1){
//     sz = p[k].size();
// 
//     rep(i,sz) pp[i] = p[k][i];
//     rep(i,sz) vv[i] = v[k][i];
//     sortA(sz, pp, vv);
// 
// //    wt("------------");
// //    wt(k);
// //    wt(pp(sz));
// //    wt(vv(sz));
// 
//     rep(s,2) rep(t,2){
//       pt = 0;
//       y1 = y2 = y3 = y4 = y5 = -1;
//       while(pt < sz && pp[pt]==0){
//         y1 = vv[pt];
//         pt++;
//       }
//       while(pt < sz && pp[pt]==1){
//         if(y2==-1) y2 = vv[pt]; else y3 = vv[pt];
//         pt++;
//       }
//       while(pt < sz && pp[pt]==2){
//         if(y4==-1) y4 = vv[pt]; else y5 = vv[pt];
//         pt++;
//       }
//       if(k==N-2) y4 = y5 = 0;
// 
//       val = 0;
//       rep(x1,2) rep(x2,2) rep(x3,2) rep(x4,2) rep(x5,2){
//         if(y1 != -1 && y1 != x1) continue;
//         if(y2 != -1 && y2 != x2) continue;
//         if(y3 != -1 && y3 != x3) continue;
//         if(y4 != -1 && y4 != x4) continue;
//         if(y5 != -1 && y5 != x5) continue;
//         if( (s+x1)%2 ) continue;
//         if( (x1+x2+x3+t)%2 ) continue;
//         if( k!=N-2 && (x1+x2+x3+x4+x5)%2 ) continue;
//         val += 1;
//       }
// 
//       nw = 2;
//       if(k != N-2) nw++;
//       while(pt < sz){
//         if(pt + 1 < sz && pp[pt]==pp[pt+1]){
//           if(vv[pt] != vv[pt+1]){
//             val = 0;
//           }
//           pt++;
//         }
//         val *= pw2[pp[pt] - nw];
//         nw = pp[pt] + 1;
//         pt++;
//       }
// 
//       val *= pw2[N-k-nw];
//       cnt[s][t][k] = val;
// //      wt(s,t,k,val);
//     }
//   }
// 
//   rep(s,2) dp[s][N-1] = cnt[s][0][N-1];
//   for(k=N-2;k>=0;k--){
//     dp[0][k] = cnt[0][0][k] * dp[0][k+1] + cnt[0][1][k] * dp[1][k+1];
//     dp[1][k] = cnt[1][0][k] * dp[0][k+1] + cnt[1][1][k] * dp[1][k+1];
//   }
//   res = dp[0][0] + dp[1][0];
//   wt(res);
// }


Measured memory_bytes: 13568