#include #include using namespace std; using Graph = vector>; // 深さ優先探索 int seen[100010], seen2[100010]; bool dfs(const Graph &G, const Graph &G_, const Graph &G2, const Graph &G2_, int v, bool HonLia) { bool flag = false; //矛盾が生じたか if (HonLia == true) seen[v] = 2; else seen[v] = 1; for (auto next_v : G[v]) { if (seen[next_v] >= 1){ if (HonLia == true && seen[next_v] == 1) flag = true; if (HonLia == false && seen[next_v] == 2) flag = true; continue; } if (HonLia == true) {if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 else {if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 } for (auto next_v : G_[v]) { if (seen[next_v] >= 1){ if (HonLia == true && seen[next_v] == 2) flag = true; if (HonLia == false && seen[next_v] == 1) flag = true; continue; } if (HonLia == true) {if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 else {if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 } for (auto next_v : G2[v]) { if (seen[next_v] >= 1){ if (HonLia == true && seen[next_v] == 1) flag = true; if (HonLia == false && seen[next_v] == 2) flag = true; continue; } if (HonLia == true) {if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 else {if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 } for (auto next_v : G2_[v]) { if (seen[next_v] >= 1){ if (HonLia == true && seen[next_v] == 2) flag = true; if (HonLia == false && seen[next_v] == 1) flag = true; continue; } if (HonLia == true) {if (dfs(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 else {if (dfs(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 } return flag; } bool dfs2(const Graph &G, const Graph &G_, const Graph &G2, const Graph &G2_, int v, bool HonLia) { bool flag = false; //矛盾が生じたか if (HonLia == true) seen2[v] = 2; else seen2[v] = 1; for (auto next_v : G[v]) { if (seen2[next_v] >= 1){ if (HonLia == true && seen2[next_v] == 1) flag = true; if (HonLia == false && seen2[next_v] == 2) flag = true; continue; } if (HonLia == true) {if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 else {if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 } for (auto next_v : G_[v]) { if (seen2[next_v] >= 1){ if (HonLia == true && seen2[next_v] == 2) flag = true; if (HonLia == false && seen2[next_v] == 1) flag = true; continue; } if (HonLia == true) {if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 else {if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 } for (auto next_v : G2[v]) { if (seen2[next_v] >= 1){ if (HonLia == true && seen2[next_v] == 1) flag = true; if (HonLia == false && seen2[next_v] == 2) flag = true; continue; } if (HonLia == true) {if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 else {if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 } for (auto next_v : G2_[v]) { if (seen2[next_v] >= 1){ if (HonLia == true && seen2[next_v] == 2) flag = true; if (HonLia == false && seen2[next_v] == 1) flag = true; continue; } if (HonLia == true) {if (dfs2(G, G_, G2, G2_, next_v, false) == true) flag = true;} // 再帰的に探索 else {if (dfs2(G, G_, G2, G2_, next_v, true) == true) flag = true;} // 再帰的に探索 } return flag; } int main() { // 頂点数と辺数 int N, Q; cin >> N >> Q; // グラフ入力受取 Graph G(N), G_(N), G2(N), G2_(N); for (int i = 0; i < Q; ++i) { int a, b, c; cin >> a >> b >> c; if (a == b) cout << -1 << endl; if (c == 0) G[a - 1].push_back(b - 1); if (c == 1) G_[a - 1].push_back(b - 1); if (c == 0) G2[b - 1].push_back(a - 1); if (c == 1) G2_[b - 1].push_back(a - 1); } // 全頂点が訪問済みになるまで探索 long long ans = 1; for (int i = 0; i < N; i++) seen[i] = seen2[i] = 0; for (int v = 0; v < N; ++v) { if (seen[v] >= 1) continue; // v が探索済みだったらスルー long long count = 0; if (dfs(G, G_, G2, G2_, v, true) == false) count++; // v が未探索なら v を始点とした DFS を行う if (dfs2(G, G_, G2, G2_, v, false) == false) count++; // v が未探索なら v を始点とした DFS を行う ans = (ans * count) % 1000000007; } cout << ans << endl; }