#include <bits/stdc++.h>
using namespace std;
const int maxn = 24, maxm = maxn * maxn * maxn, mod = 998244353;
int n, m;
tuple<int, int, int> abc[maxm];
set<vector<int>> st;
bitset<(1<<maxn) * maxn> bst;
int dp[1<<maxn][maxn];
int ans;
int cut(int mask, int l, int r) {
return (mask >> l) & ((1 << (r-l+1)) - 1);
}
void modadd(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
}
void set_true(int mask, int i) {
bst[mask * maxn + i] = true;
}
bool invalid(int mask, int i) {
return bst[mask * maxn + i];
}
bool eq(tuple<int, int, int> &t1, tuple<int, int, int> &t2) {
auto &[a1, b1, c1] = t1;
auto &[a2, b2, c2] = t2;
// if (a1 != a2 || c1 != c2) return false;
// bool pb = (b1 < a1 && b2 < a2) || (b1 > a1 && b1 < c1 && b2 > a2 && b2 < c2) || (b1 > c1 && b2 > c2);
// return pb;
return (a1 == a2 && c1 == c2);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i=0; i<m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--, c--;
abc[i] = {a, b, c};
}
for (int i=0; i<m; i++) {
auto &[a, b, c] = abc[i];
vector<int> v = {a, b, c};
sort(v.begin(), v.end());
if (st.count(v)) {
cout << "0\n";
return 0;
}
st.insert(v);
}
sort(abc, abc + m, [](tuple<int, int, int> &t1, tuple<int, int, int> &t2) {
auto &[a1, b1, c1] = t1;
auto &[a2, b2, c2] = t2;
if (a1 != a2) return a1 < a2;
if (c1 != c2) return c1 < c2;
return b1 < b2;
});
for (int l=0; l<m; ) {
int r = l;
while (r < m && eq(abc[l], abc[r])) r++;
// gaboleh ke b kalo a dan c sama
auto &[a, _, c] = abc[l];
if (a > c) swap(a, c);
for (int mask=0; mask<(1<<(n-2)); mask++) {
int true_mask = 0;
if (a > 0) true_mask |= cut(mask, 0, a - 1);
if (a + 1 <= c - 1) true_mask |= (cut(mask, a, c - 2) << (a + 1));
if (c + 1 <= n - 1) true_mask |= (cut(mask, c - 1, n - 3) << (c + 1));
true_mask &= ~(1 << b);
for (int i=l; i<r; i++) {
auto &[aa, b, cc] = abc[i];
set_true(true_mask, b);
}
true_mask |= (1 << a);
true_mask |= (1 << c);
for (int i=l; i<r; i++) {
auto &[aa, b, cc] = abc[i];
set_true(true_mask, b);
}
}
l = r;
}
for (int mask=1; mask<(1<<n); mask++) {
if ((mask&(mask-1)) == 0) {
int i = 31 - __builtin_clz(mask);
dp[mask][i] = !invalid(0, i); // cek dulu i pernah jadi b atau nggak
} else {
for (int m1=mask; m1>0; m1-=m1&-m1) {
int i = __builtin_ctz(m1);
dp[mask][i] = 0;
int pre_mask = mask & ~(1 << i);
for (int m2=pre_mask; m2>0; m2-=m2&-m2) {
int j = __builtin_ctz(m2);
if (!invalid(pre_mask, i)) modadd(dp[mask][i], dp[pre_mask][j]);
}
}
}
}
ans = 0;
for (int i=0; i<n; i++) modadd(ans, dp[(1<<n)-1][i]);
cout << ans << '\n';
}