n,m = map(int,input().split()) n += 1 g = [[] 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)) 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) if ok[-1]==0: print(0) exit() ind = [0]*n for u,v in edges: if ok[u]: ind[v] += 1 MOD = 10**9+7 q = [i for i in range(n) if ok[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 dpnum[v]==0: print("INF") else: print(dpval[-1])