#include using namespace std; using int128 = __int128_t; using int64 = long long; using int32 = int; using uint128 = __uint128_t; using uint64 = unsigned long long; using uint32 = unsigned int; #define ALL(obj) (obj).begin(),(obj).end() template using priority_queue_reverse = priority_queue,greater>; constexpr int64 MOD = 1'000'000'000LL + 7; //' constexpr int64 MOD2 = 998244353; constexpr int64 HIGHINF = 1'000'000'000'000'000'000LL; constexpr int64 LOWINF = 1'000'000'000'000'000LL; //' constexpr long double PI = 3.1415926535897932384626433L; template vector multivector(size_t N,T init){return vector(N,init);} template auto multivector(size_t N,T... t){return vector(N,multivector(t...));} template void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}} template ostream &operator<<(ostream &o, const map&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;} template ostream &operator<<(ostream &o, const set&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const multiset&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;} template ostream &operator<<(ostream &o, const vector&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template ostream &operator<<(ostream &o, const deque&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;} template ostream &operator<<(ostream &o, const pair&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;} void print(void) {cout << endl;} template void print(Head&& head) {cout << head;print();} template void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward(tail)...);} template void chmax(T& a, const T b){a=max(a,b);} template void chmin(T& a, const T b){a=min(a,b);} vector split(const string &str, const char delemiter) {vector res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;} inline constexpr int msb(int x) {return x?31-__builtin_clz(x):-1;} inline constexpr int64 ceil_div(const int64 a,const int64 b) {return (a+(b-1))/b;}// return ceil(a/b) void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;} void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;} void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;} /* * @title ModInt * @docs md/util/ModInt.md */ template class ModInt { public: long long x; constexpr ModInt():x(0) {} constexpr ModInt(long long y) : x(y>=0?(y%mod): (mod - (-y)%mod)%mod) {} ModInt &operator+=(const ModInt &p) {if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator+=(const long long y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator+=(const int y) {ModInt p(y);if((x += p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const ModInt &p) {if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const long long y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator-=(const int y) {ModInt p(y);if((x += mod - p.x) >= mod) x -= mod;return *this;} ModInt &operator*=(const ModInt &p) {x = (x * p.x % mod);return *this;} ModInt &operator*=(const long long y) {ModInt p(y);x = (x * p.x % mod);return *this;} ModInt &operator*=(const int y) {ModInt p(y);x = (x * p.x % mod);return *this;} ModInt &operator^=(const ModInt &p) {x = (x ^ p.x) % mod;return *this;} ModInt &operator^=(const long long y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;} ModInt &operator^=(const int y) {ModInt p(y);x = (x ^ p.x) % mod;return *this;} ModInt &operator/=(const ModInt &p) {*this *= p.inv();return *this;} ModInt &operator/=(const long long y) {ModInt p(y);*this *= p.inv();return *this;} ModInt &operator/=(const int y) {ModInt p(y);*this *= p.inv();return *this;} ModInt operator=(const int y) {ModInt p(y);*this = p;return *this;} ModInt operator=(const long long y) {ModInt p(y);*this = p;return *this;} ModInt operator-() const {return ModInt(-x); } ModInt operator++() {x++;if(x>=mod) x-=mod;return *this;} ModInt operator--() {x--;if(x<0) x+=mod;return *this;} ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } ModInt operator^(const ModInt &p) const { return ModInt(*this) ^= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inv() const {int a=x,b=mod,u=1,v=0,t;while(b > 0) {t = a / b;swap(a -= t * b, b);swap(u -= t * v, v);} return ModInt(u);} ModInt pow(long long n) const {ModInt ret(1), mul(x);for(;n > 0;mul *= mul,n >>= 1) if(n & 1) ret *= mul;return ret;} friend ostream &operator<<(ostream &os, const ModInt &p) {return os << p.x;} friend istream &operator>>(istream &is, ModInt &a) {long long t;is >> t;a = ModInt(t);return (is);} }; using modint = ModInt; /* * @title StronglyConnectedComponents - 強連結成分分解 * @docs md/graph/StronglyConnectedComponents.md */ class StronglyConnectedComponents{ int num,is_2sat,half,max_id,cnt; vector> edge; vector label,order,low; stack st; inline int rev(int i) { return i < half ? i + half : i - half; } inline void dfs(int& from) { low[from]=order[from]=cnt++; st.push(from); for(int& to:edge[from]) { if(order[to]==-1) { dfs(to); low[from]=min(low[from],low[to]); } else { low[from]=min(low[from],order[to]); } } if (low[from] == order[from]) { while(st.size()) { int u = st.top();st.pop(); order[u] = num; label[u] = max_id; if (u == from) break; } max_id++; } } public: StronglyConnectedComponents(const int n, bool is_2sat=0):num(n),max_id(0),cnt(0),is_2sat(is_2sat){ if(is_2sat) num<<=1; edge.resize(num); label.resize(num); order.resize(num,-1); low.resize(num); half=(num>>1); } inline int operator[](int idx) { return label[idx]; } inline void make_edge(const int from,const int to) { edge[from].push_back(to); } //xがflg_xならばyがflg_y inline void make_condition(int x, bool flg_x, int y, bool flg_y) { if (!flg_x) x = rev(x); if (!flg_y) y = rev(y); make_edge(x, y); make_edge(rev(y), rev(x)); } //is_2sat=1のときに、2satを満たすかを返却する inline bool solve(void) { for (int i = 0; i < num; i++) if (order[i] == -1) dfs(i); for (int& id:label) id = max_id-1-id; if(!is_2sat) return true; for (int i = 0; i < num; ++i) if (label[i] == label[rev(i)]) return false; return true; } vector> topological_sort(void) { vector> ret(max_id); for(int i=0;i label[rev(i)]; } void print(void) { for(auto id:label) cout << id << " "; cout << endl; } }; /** * @url * @est */ int main() { cin.tie(0);ios::sync_with_stdio(false); int N,M; cin >> N >> M; using t = tuple; StronglyConnectedComponents scc0(N+1),sccN(N+1); vector> edges0(N+1),edgesN(N+1); for(int i=0;i> u >> v >> l >> a; scc0.make_edge(u,v); edges0[u].push_back(t{v,l,a}); sccN.make_edge(v,u); edgesN[v].push_back(t{u,l,a}); } scc0.solve(); auto vv0 = scc0.topological_sort(); vector dp0(N+1,0); vector is_inf(N+1,0); int is_0 = 0; dp0[0]=1; for(auto v:vv0) { for(int e:v) if(e==0) is_0 = 1; if(!is_0) continue; for(int from:v) { if(v.size()!=1) is_inf[from]=1; for(auto ed:edges0[from]) { int to = get<0>(ed); int64 a = get<2>(ed); is_inf[to] |= is_inf[from]; if(is_inf[to]) continue; dp0[to] += dp0[from]*a; } } } corner(is_inf[N],"INF"); sccN.solve(); auto vvN = sccN.topological_sort(); vector dpN(N+1,0); dpN[N]=1; modint ans = 0; for(auto v:vvN) { for(int from:v) { for(auto ed:edgesN[from]) { int to = get<0>(ed); int64 l = get<1>(ed); int64 a = get<2>(ed); dpN[to] += dpN[from]*a; ans += dp0[to]*dpN[from]*a*l; } } } cout << ans << endl; return 0; }