#include #include #include #include #include #include #include #include #include #include #include using namespace std; //ABC 223 D Restricted Permutaion template using pq = priority_queue, greater>; vector topological_sort(vector>> &E){ long long N = E.size(), C; vector deg(N), res; pq que; for (int i=0; i> N >> M; vector>> E(N); vector top, deg(N), dp(N); for (int i=0; i> A >> C >> B; A--; B--; E[A].push_back({B, C}); deg[B]++; } top = topological_sort(E); dp[N-1] = 1; for (int i=N-1; i>=0; i--){ for (auto [x, a] : E[top[i]]){ dp[top[i]] += a * dp[x]; } } for (int i=0; i