#define _USE_MATH_DEFINES #include using namespace std; template class Modular { private: long long value; constexpr static int MOD() { return static_cast(T::value); } public: constexpr Modular() : value() {}; constexpr Modular(const Modular& other) : value(other.value) {} template constexpr Modular(const U& x) { value = normalize(x); } template static long long normalize(const U& x) { long long v; if (-MOD() <= x && x < MOD()) v = static_cast(x); else v = static_cast(x % MOD()); if (v < 0) v += MOD(); return v; } constexpr static long long inverse(long long x) { x = (x % MOD() + MOD()) % MOD(); long long y = MOD(), u = 1, v = 0; while(y) { long long t = x / y; x -= t * y; swap(x, y); u -= t * v; swap(u, v); } return (u % MOD() + MOD()) % MOD(); } static long long mul(const long long& a, const long long& b) { long long res; #ifdef _WIN32 unsigned long long x = a * b; unsigned xh = (unsigned) (x >> 32), xl = (unsigned) x, d, m; asm( "divl %4; \n\t" : "=a" (d), "=d" (m) : "d" (xh), "a" (xl), "r" (MOD()) ); res = m; #else res = a * b % MOD(); #endif return res; } explicit operator long long() const noexcept { return value;} template explicit operator U() const noexcept { return static_cast(value); } constexpr Modular& operator=(const Modular& other) & noexcept { value = other.value; return *this; } template constexpr Modular& operator=(const U& other) & noexcept { return *this = Modular(other); } constexpr Modular& operator+=(const Modular& other) noexcept { if ((value += other.value) >= MOD()) value -= MOD(); return *this; } template constexpr Modular& operator+=(const U& other) noexcept { return *this += Modular(other); } constexpr Modular& operator-=(const Modular& other) noexcept { if ((value -= other.value) < 0) value += MOD(); return *this; } template constexpr Modular& operator-=(const U& other) noexcept { return *this -= Modular(other); } constexpr Modular& operator*=(const Modular& other) noexcept { this->value = mul(this->value, other.value); return *this; } template constexpr Modular& operator*=(const U& other) noexcept {return *this *= Modular(other); } constexpr Modular& operator/=(const Modular& other) noexcept { return *this *= Modular(inverse(other.value)); } template constexpr Modular& operator/=(const U& other) noexcept { return *this *= Modular(inverse(normalize(other))); } constexpr Modular& operator++() noexcept {return *this += 1; } constexpr Modular operator++(int) noexcept { Modular ret(*this); *this += 1; return ret; } constexpr Modular& operator--() noexcept {return *this -= 1; } constexpr Modular operator--(int) noexcept { Modular ret(*this); *this -= 1; return ret; } constexpr Modular operator-() const { return Modular(-value); } friend constexpr bool operator==(const Modular& lhs, const Modular& rhs) noexcept { return lhs.value == rhs.value; } template friend constexpr bool operator==(const Modular& lhs, U rhs) noexcept { return lhs == Modular(rhs); } template friend constexpr bool operator==(U lhs, const Modular& rhs) noexcept { return Modular(lhs) == rhs; } friend constexpr bool operator!=(const Modular& lhs, const Modular& rhs) noexcept { return !(lhs == rhs); } template friend constexpr bool operator!=(const Modular& lhs, U rhs) noexcept { return !(lhs == rhs); } template friend constexpr bool operator!=(U lhs, const Modular rhs) noexcept { return !(lhs == rhs); } friend constexpr bool operator<(const Modular& lhs, const Modular& rhs) noexcept { return lhs.value < rhs.value; } template friend constexpr bool operator<(const Modular &lhs, U rhs) noexcept { return lhs.value < rhs; } template friend constexpr bool operator<(U lhs, const Modular &rhs) noexcept { return lhs < rhs.value; } friend constexpr bool operator>(const Modular& lhs, const Modular& rhs) noexcept { return rhs.value < lhs.value; } template friend constexpr bool operator>(const Modular &lhs, U rhs) noexcept { return rhs.value < lhs; } template friend constexpr bool operator>(U lhs, const Modular &rhs) noexcept { return rhs < lhs.value; } friend constexpr bool operator<=(const Modular& lhs, const Modular& rhs) noexcept { return !(lhs.value > rhs.value); } template friend constexpr bool operator<=(const Modular &lhs, U rhs) noexcept { return !(lhs.value > rhs); } template friend constexpr bool operator<=(U lhs, const Modular &rhs) noexcept { return !(lhs < rhs.value); } friend constexpr bool operator>=(const Modular& lhs, const Modular& rhs) noexcept { return !(lhs.value < rhs.value); } template friend constexpr bool operator>=(const Modular &lhs, U rhs) noexcept { return !(lhs.value < rhs); } template friend constexpr bool operator>=(U lhs, const Modular &rhs) noexcept { return !(lhs < rhs.value); } friend constexpr Modular operator+(const Modular& lhs, const Modular& rhs) noexcept { return Modular(lhs) += rhs; } template friend constexpr Modular operator+(const Modular& lhs, U rhs) noexcept { return Modular(lhs) += rhs; } template friend constexpr Modular operator+(U lhs, const Modular &rhs) noexcept { return Modular(lhs) += rhs; } friend constexpr Modular operator-(const Modular& lhs, const Modular& rhs) noexcept { return Modular(lhs) -= rhs; } template friend constexpr Modular operator-(const Modular& lhs, U rhs) noexcept { return Modular(lhs) -= rhs; } template friend constexpr Modular operator-(U lhs, const Modular &rhs) noexcept { return Modular(lhs) -= rhs; } friend constexpr Modular operator*(const Modular& lhs, const Modular& rhs) noexcept { return Modular(lhs) *= rhs; } template friend constexpr Modular operator*(const Modular& lhs, U rhs) noexcept { return Modular(lhs) *= rhs; } template friend constexpr Modular operator*(U lhs, const Modular &rhs) noexcept { return Modular(lhs) *= rhs; } friend constexpr Modular operator/(const Modular& lhs, const Modular& rhs) noexcept { return Modular(lhs) /= rhs; } template friend constexpr Modular operator/(const Modular& lhs, U rhs) noexcept { return Modular(lhs) /= rhs; } template friend constexpr Modular operator/(U lhs, const Modular &rhs) noexcept { return Modular(lhs) /= rhs; } friend std::ostream& operator<<(std::ostream& stream, const Modular& number) noexcept { return stream << number.value; } friend std::istream& operator>>(std::istream& stream, Modular& number) { long long in; stream >> in; number.value = Modular::normalize(in); return stream; } constexpr int getmod() const { return MOD(); } }; template Modular power(const Modular& x, const U& y) { assert(y >= 0); Modular k = x, result = 1; U p = y; while (p > 0) { if (p & 1) result *= k; k *= k; p >>= 1; } return result; } template class BinaryCoefficients { private: vector> fact_, inv_, finv_; long long MOD = static_cast(T::value); public: constexpr BinaryCoefficients(int n = 2020200) : fact_(n, 1), inv_(n, 1), finv_(n, 1) { for (int i = 2; i < n; i++) { fact_[i] = fact_[i - 1] * i; inv_[i] = -inv_[MOD % i] * (MOD / i); finv_[i] = finv_[i - 1] * inv_[i]; } } constexpr Modular comb(int n, int k) const noexcept { if (n < k || n < 0 || k < 0) return 0; return fact_[n] * finv_[k] * finv_[n - k]; } constexpr Modular fact(int n) const noexcept { if (n < 0) return 0; return fact_[n]; } constexpr Modular inv(int n) const noexcept { if (n < 0) return 0; return inv_[n]; } constexpr Modular finv(int n) const noexcept { if (n < 0) return 0; return finv_[n]; } }; constexpr int mod = 1e9 + 7; //constexpr int mod = 998244353; using mint = Modular::type, mod>>; using bicoef = BinaryCoefficients::type, mod>>; // struct modValue { static int value; }; // int modValue::value; // int& mod = modValue::value; // using mint = Modular; // using bicoef = BinaryCoefficients; class StronglyConnectedComponents { private: int siz; int itr = 0; vector done; vector order, comp; void dfs (int cur, const vector>& g) { done[cur] = true; for (int nxt : g[cur]) { if (!done[nxt]) dfs(nxt, g); } order.push_back(cur); } void rdfs (int cur, const vector>& rg) { done[cur] = true; comp[cur] = itr; for (int nxt : rg[cur]) { if (!done[nxt]) rdfs(nxt, rg); } } void build (const vector>& g, const vector>& rg) { for (int i = 0; i < siz; ++i) if (!done[i]) dfs (i, g); reverse(order.begin(), order.end()); fill(done.begin(), done.end(), false); for (int i = 0; i < siz; ++i) if (!done[order[i]]) rdfs (order[i], rg), ++itr; } public: StronglyConnectedComponents(const vector>& g, const vector>& rg, const int& n) : siz(n), done(siz, false), comp(siz, -1) { build(g, rg); } bool has_cycle() { return *max_element(comp.begin(), comp.end()) < siz - 1; } vector get_comp() { return comp;} }; using SCC = StronglyConnectedComponents; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector> g(n + 1); vector>>> gg(n + 1); for (int i = 0; i < m; i++) { int u, v, l, a; cin >> u >> v >> l >> a; g[u].emplace(v); gg[u][v].emplace_back(l, a); } vector reachable(n + 1); vector vis(n + 1); auto dfs1 = [&] (auto&& self, int cur) -> bool { if (vis[cur]) return reachable[cur]; if (cur == n) return reachable[cur] = true; vis[cur] = true; bool res = false; for (auto nbr : g[cur]) { res |= self(self, nbr); } return reachable[cur] = res; }; dfs1(dfs1, 0); vector> gv(n + 1); vector> rgv(n + 1); for (int i = 0; i <= n; i++) { for (auto nbr : g[i]) { gv[i].push_back(nbr); rgv[nbr].push_back(i); } } SCC scc(gv, rgv, n + 1); auto comp = scc.get_comp(); assert((int) comp.size() == n + 1); map cnt; for (int i = 0; i <= n; i++) { if (reachable[i]) cnt[comp[i]]++; } for (auto p : cnt) { if (p.second > 1) { cout << "INF" << endl; return 0; } } cout << "INF" << endl; return 0; // vector vis2(n + 1); // bool hasCycle = false; // auto dfs2 = [&] (auto&& self, int cur) { // if (vis2[cur] == 1) { // hasCycle = true; // } // if (vis2[cur] != 0) return; // vis2[cur] = 1; // for (auto nbr : g[cur]) if (reachable[cur]) { // self(self, nbr); // } // vis2[cur] = 2; // }; // dfs2(dfs2, 0); // if (hasCycle) { // cout << "INF" << endl; // return 0; // } fill(vis.begin(), vis.end(), false); vector que; que.push_back(0); vector tot(n + 1); vector ways(n + 1); ways[0] = 1; for (int u = 0; u < (int) que.size(); u++) { int cur = que[u]; vis[cur] = true; for (auto nbr : g[cur]) { if (!reachable[nbr]) continue; tot[nbr] += tot[cur]; for (auto p : gg[cur][nbr]) { ways[nbr] += ways[cur] * p.second; tot[nbr] += ways[cur] * p.second * p.first; // cerr << "cur = " << cur << " nbr = " << nbr << " " << p.second << " " << p.first << endl; } que.push_back(nbr); } } cout << tot[n] << endl; return 0; }