#include #define rep(i,n) for(int i=0;i<(int)(n);i++) #define chmin(x,y) x = min((x),(y)); #define chmax(x,y) x = max((x),(y)); using namespace std; using ll = long long ; using P = pair ; using pll = pair; const int INF = 1e9; const long long LINF = 1e17; const int MOD = 1000000007; //const int MOD = 998244353; const double PI = 3.14159265358979323846; template struct ModInt{ long long x=0; constexpr ModInt(long long x=0):x((x%mod+mod)%mod){} constexpr ModInt operator+(const ModInt& r)const{return ModInt(*this)+=r;} constexpr ModInt operator-(const ModInt& r)const{return ModInt(*this)-=r;} constexpr ModInt operator*(const ModInt& r)const{return ModInt(*this)*=r;} constexpr ModInt operator/(const ModInt& r)const{return ModInt(*this)/=r;} constexpr ModInt& operator+=(const ModInt& r){ if((x+=r.x)>=mod) x-=mod; return *this;} constexpr ModInt& operator-=(const ModInt& r){ if((x-=r.x)<0) x+=mod; return *this;} constexpr ModInt& operator*=(const ModInt& r){ if((x*=r.x)>=mod) x%=mod; return *this;} constexpr ModInt& operator/=(const ModInt& r){ return *this*=r.inv();} ModInt inv() const { long long s=x,sx=1,sy=0,t=mod,tx=0,ty=1; while(s%t!=0){ long long temp=s/t,u=s-t*temp,ux=sx-temp*tx,uy=sy-temp*ty; s=t;sx=tx;sy=ty; t=u;tx=ux;ty=uy; } return ModInt(tx); } ModInt pow(long long n) const { ModInt a=1; ModInt b=*this; while(n>0){ if(n&1) a*=b; b*=b; n>>=1; } return a; } friend constexpr ostream& operator<<(ostream& os,const ModInt& a) {return os << a.x;} friend constexpr istream& operator>>(istream& is,ModInt& a) {return is >> a.x;} }; using mint = ModInt; //StronglyConnectedCompoinent struct scc_graph{ int n; vector> graph; vector> revgraph; vector postorder; vector seen; scc_graph(int n):n(n),graph(n),revgraph(n),seen(n,-1){} void add_edge(int from,int to){ graph[from].push_back(to); revgraph[to].push_back(from); } vector> scc(){ fill(seen.begin(),seen.end(),-1); for(int i=0;i=0;i--){ if(seen[postorder[i]]<0) revdfs(postorder[i],k++); } vector> scc_groups(k); for(int i=0;i=0) continue; dfs(j); } postorder.push_back(i); } void revdfs(int i,int k){ seen[i] = k; for(int j:revgraph[i]){ if(seen[j]>=0) continue; revdfs(j,k); } } }; struct edge{ ll to,l,a; }; mint dp[100005]; mint cnt[100005]; int main(){ int n,m; cin >> n >> m; vector> G(n+1); scc_graph graph(n+1); rep(i,m){ ll u,v,l,a; cin >> u >> v >> l >> a; G[u].push_back(edge{v,l,a}); graph.add_edge(u,v); } vector> topo = graph.scc(); map real_temp; rep(i,topo.size()){ for(int j:topo[i]){ real_temp[j] = i; } } vector seen(topo.size(),0); vector dfs_res(topo.size(),false); int n_temp = real_temp[n]; bool cycle = false; auto dfs = [&](auto&& dfs,int i) -> bool{ if(seen[i] == 1) return dfs_res[i]; seen[i] = 1; bool ok = false; if(i == n_temp) ok = true; for(int j:topo[i]){ for(auto e:G[j]){ int to = real_temp[e.to]; if(seen[to] == 1) continue; bool temp = dfs(dfs,to); if(temp) ok = true; } } if(ok && (int)topo[i].size() != 1){ cycle = true; } return dfs_res[i] = ok; }; dfs(dfs,0); if(cycle){ cout << "INF" << endl; return 0; } dp[0] = 0; cnt[0] = 1; rep(ii,topo.size()){ int i = topo[ii][0]; for(auto e:G[i]){ dp[e.to] += dp[i] * e.a + cnt[i] * e.a * e.l; cnt[e.to] += cnt[i] * e.a; } } cout << dp[n] << endl; return 0; }