#include "bits/stdc++.h" using namespace std; using ll = long long; using P = pair; const ll INF = (1LL << 61); ll mod = (ll)1e9 + 7; ll N, M; vector>E1[100010], E2[100010], G[100010]; ll dp[100010], dp2[100010]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> M; for (int i = 0; i < M; i++) { ll u, v, l, a; cin >> u >> v >> l >> a; E1[u].push_back({ v, {l, a} }); E2[v].push_back({ u, {l, a} }); } vectorused(N+1), used2(N+1); used[0] = true; queueq; q.push(0); while (!q.empty()) { int v = q.front(); q.pop(); for (auto e : E1[v]) { if (!used[e.first]) { used[e.first] = true; q.push(e.first); } } } used2[N] = true; queueq2; q2.push(N); while (!q2.empty()) { int v = q2.front(); q2.pop(); for (auto e : E2[v]) { if (!used2[e.first]) { used2[e.first] = true; q2.push(e.first); } } } for (int i = 0; i <= N; i++) { if (used[i] && used2[i]) { for (auto e : E1[i]) { if (used[e.first] && used2[e.first]) { G[i].push_back(e); } } } } vectorans; vectorind(N + 1); for (int i = 0; i <= N; i++) { for (auto e : G[i]) { ind[e.first]++; } } queueque; for (int i = 0; i <= N; i++) { if (ind[i] == 0)que.push(i); } while (!que.empty()) { ll now = que.front(); ans.push_back(now); que.pop(); for (auto e : G[now]) { ind[e.first]--; if (ind[e.first] == 0) { que.push(e.first); } } } if (ans.size() != N + 1) { cout << "INF" << endl; return 0; } dp[0] = 1; dp2[0] = 0; for (auto v : ans) { for (auto e : G[v]) { dp[e.first] += dp[v] * e.second.second; dp[e.first] %= mod; dp2[e.first] += dp2[v] * e.second.second + (dp[v] * e.second.first % mod) * e.second.second; dp2[e.first] %= mod; } } cout << dp2[N] << endl; return 0; }