#include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; const ll MOD = 1000000007; class ModInt{ public: ll v; ModInt(ll _v = 0){ if(_v >= MOD) _v %= MOD; v = _v; } ModInt operator+(ll n){ return ModInt((v+n)%MOD); } ModInt operator-(ll n){ return ModInt((v-n+MOD)%MOD); } ModInt operator*(ll n){ if(n >= MOD) n %= MOD; return ModInt((v*n)%MOD); } ModInt operator/(ll n){ return ModInt((ModInt(n).inv()*v).v%MOD); } void operator+=(ll n){ v = (v+n)%MOD; } void operator-=(ll n){ v = (v-n+MOD)%MOD; } void operator*=(ll n){ v = (v*n+MOD)%MOD; } ModInt operator+(ModInt n){ return ModInt((v+n.v)%MOD); } ModInt operator-(ModInt n){ return ModInt((v-n.v+MOD)%MOD); } ModInt operator*(ModInt n){ return ModInt((v*n.v)%MOD); } ModInt operator/(ModInt n){ return ModInt((n.inv()*v).v%MOD); } void operator+=(ModInt n){ v = (v+n.v)%MOD; } void operator-=(ModInt n){ v = (v-n.v+MOD)%MOD; } void operator*=(ModInt n){ v = (v*n.v)%MOD; } void operator=(ModInt n){ v = n.v; } bool operator==(ModInt n){ return v == n.v; } bool operator!=(ModInt n){ return v != n.v; } void operator=(ll n){ v = n; } ModInt inv(){ if(v == 1) return ModInt(1); else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD); } }; ostream& operator<<(ostream& os, const ModInt& m){ os << m.v; return os; } istream & operator >> (istream &in, ModInt &m){ in >> m.v; return in; } class Scc{ public: int N; vector> G; vector> G_inv; vector idx; Scc(int n){ N = n; G = vector>(n, vector()); G_inv = vector>(n, vector()); used = vector(n, false); idx = vector(n); } void add_edge(int from, int to){ G[from].push_back(to); G_inv[to].push_back(from); } vector> scc(){ vector> ans; for(int i = 0; i < N; i++){ if(!used[i]) dfs1(i); } clear(); int cur = 0; for(int i = vs.size()-1; i >= 0; i--){ if(!used[vs[i]]) { dfs2(vs[i], cur); cur++; ans.push_back(buf); buf.clear(); } } return ans; } private: vector used; vector vs; vector buf; void clear(){ for(int i = 0; i < N; i++) used[i] = false; } void dfs1(int v){ used[v] = true; for(int i = 0; i < G[v].size(); i++){ if(!used[G[v][i]]) dfs1(G[v][i]); } vs.push_back(v); } void dfs2(int v, int k){ used[v] = true; idx[v] = k; for(int i = 0; i < G_inv[v].size(); i++){ if(!used[G_inv[v][i]]) dfs2(G_inv[v][i], k); } buf.push_back(v); } }; struct edge{ int to; ModInt l; int a; }; ModInt dp_sum[100001]; ModInt dp_cnt[100001]; vector g[100001]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int n, m; cin >> n >> m; Scc scc(n+1); for(int i = 0; i < m; i++){ int u, v, l, a; cin >> u >> v >> l >> a; scc.add_edge(u, v); g[u].push_back((edge){v, ModInt(l), a}); } auto v = scc.scc(); vector tsort; for(auto u : v){ if(u.size() > 1){ cout << "INF" << endl; return 0; } tsort.push_back(u[0]); } dp_cnt[0] = 1; for(int u : tsort){ for(edge e : g[u]){ dp_cnt[e.to] += dp_cnt[u]*e.a; dp_sum[e.to] += dp_sum[u]*e.a+e.l*dp_cnt[u]*e.a; } } cout << dp_sum[n] << endl; }