#include <bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
const i32 inf = INT_MAX / 2;
const int mod = 998244353;
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<pii>> e(n + 1);
for (int x, y, c; m; m--) {
cin >> x >> y >> c;
e[x].emplace_back(y, c);
e[y].emplace_back(x, c);
}
set<vi> R, B;
for (int x = 1; x <= n; x++) {
for (auto [y, c1]: e[x]) {
if (y > x) {
if (c1) R.insert({x, y});
else B.insert({x, y});
}
for (auto [z, c2]: e[x]) {
if (y == z) continue;
if (y < z) continue;
if (c1 != c2) continue;
vi t = {x, y, z};
ranges::sort(t);
if (c1) R.insert(t);
else B.insert(t);
for (auto [w, c3]: e[y]) {
if (w == x or w == y or w == z) continue;
if (c1 != c3) continue;
t = {w, x, y, z};
ranges::sort(t);
if (c1) R.insert(t);
else B.insert(t);
}
for (auto [w, c3]: e[z]) {
if (w == x or w == y or w == z) continue;
if (c1 != c3) continue;
t = {w, x, y, z};
ranges::sort(t);
if (c1) R.insert(t);
else B.insert(t);
}
}
}
}
int res = n;
for (auto it: R)
if (B.count(it)) res++;
cout << res % mod;
return 0;
}