#line 1 "a.cpp" #include #include using namespace std; #line 3 "/home/kotatsugame/library/math/modint.cpp" #include template struct modint{ unsigned int x; constexpr modint()noexcept:x(){} template constexpr modint(T x_)noexcept:x((x_%=m)<0?x_+m:x_){} constexpr unsigned int val()const noexcept{return x;} constexpr modint&operator++()noexcept{if(++x==m)x=0;return*this;} constexpr modint&operator--()noexcept{if(x==0)x=m;--x;return*this;} constexpr modint operator++(int)noexcept{modint res=*this;++*this;return res;} constexpr modint operator--(int)noexcept{modint res=*this;--*this;return res;} constexpr modint&operator+=(const modint&a)noexcept{x+=a.x;if(x>=m)x-=m;return*this;} constexpr modint&operator-=(const modint&a)noexcept{if(x>=1)if(n&1)r*=x; return r; } constexpr modint inv()const noexcept { int s=x,t=m,x=1,u=0; while(t) { int k=s/t; s-=k*t; swap(s,t); x-=k*u; swap(x,u); } return modint(x); } friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;} friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;} friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;} friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;} friend constexpr bool operator==(const modint&a,const modint&b){return a.x==b.x;} friend constexpr bool operator!=(const modint&a,const modint&b){return a.x!=b.x;} friend ostream&operator<<(ostream&os,const modint&a){return os<>(istream&is,modint&a){long long v;is>>v;a=modint(v);return is;} }; #line 1 "/home/kotatsugame/library/graph/SCC.cpp" //Strongly Connected Components #line 3 "/home/kotatsugame/library/graph/SCC.cpp" struct SCC{ int n; vectorcomp,order; vectorused; vector >G,RG; SCC(int _n=0):n(_n),comp(_n,-1),used(_n,false),G(_n),RG(_n){} void add_edge(int from,int to) { G[from].push_back(to); RG[to].push_back(from); } void copy(const vector >&H) { for(int i=0;i=0;i--)if(comp[order[i]]==-1)rdfs(order[i],cnt++); return cnt; } int build(vector >&H) { int ret=build(); H.assign(ret,vector()); for(int i=0;i; int N,M; vector > >G[2<<17],H[2<<17]; int cnt[2<<17]; mint dp[2<<17],ep[2<<17]; bool vis[2<<17]; main() { cin>>N>>M; SCC P(N+1); for(int i=0;i>u>>v>>l>>a; H[u].push_back(make_pair(v,make_pair(l,a))); P.add_edge(u,v); } int K=P.build(); for(int i=0;i<=N;i++)cnt[P[i]]++; for(int i=0;i<=N;i++)for(int j=0;jP[N]) { cout<<0<1) { cout<<"INF"< >e:H[i]) { vis[e.first]=true; dp[e.first]+=dp[i]*e.second.second; ep[e.first]+=ep[i]+e.second.first*dp[i]; } } cout<