n,m = map(int,input().split()) n += 1 g = [[] for _ in range(n)] rg = [[] for _ in range(n)] edges = [] for _ in range(m): u,v,l,a = map(int,input().split()) g[u].append((v,l,a)) edges.append((u,v)) rg[v].append(u) ok = [0]*n ok[0] = 1 q = [0] while q: u = q.pop() for v,l,a in g[u]: if ok[v]==0: ok[v] = 1 q.append(v) ok2 = [0]*n ok2[n-1] = 1 q = [n-1] while q: u = q.pop() for v in rg[u]: if ok2[v]==0: ok2[v] = 1 q.append(v) ind = [0]*n for u,v in edges: if ok[u] and ok2[v]: ind[v] += 1 MOD = 10**9+7 q = [i for i in range(n) if ok[i] and ok2[i] and ind[i]==0] dpnum = [0]*n dpval = [0]*n dpnum[0] = 1 while q: u = q.pop() for v,l,a in g[u]: dpnum[v] += a*dpnum[u] dpnum[v] %= MOD dpval[v] += (dpval[u] + dpnum[u]*l)%MOD*a dpval[v] %= MOD ind[v] -= 1 if ind[v]==0: q.append(v) if any(vi>0 for vi in ind): print("INF") else: print(dpval[-1])