import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) mod = 10**9+7 N, M = map(int, input().split()) deg = [0] * (N+1) go = [[] for _ in range(N+1)] back = [[] for _ in range(N+1)] for _ in range(M): a, b, c, d = map(int, input().split()) go[a].append((b, c, d)) back[b].append((a, c, d)) deg[b] += 1 topo = [] q = [] for i, d in enumerate(deg): if d == 0: q.append(i) while q: s = q.pop() topo.append(s) for t, _, _ in go[s]: deg[t] -= 1 if deg[t] == 0: q.append(t) if len(topo) < N + 1: print("INF") exit() cnt = [0] * (N + 1) cnt[N] = 1 for s in topo[::-1]: for t, _, num in back[s]: cnt[t] += num * cnt[s] cnt[t] %= mod if cnt[0] == 0: print(0) exit() ans = 0 for s in topo: for t, d, num in go[s]: ans += d * num * cnt[t] ans %= mod print(ans)