from collections import deque import sys input = sys.stdin.readline N,M = map(int,input().split()) edge = [[] for i in range(N+1)] deg = [0 for i in range(N+1)] for i in range(M): u,v,l,a = map(int,input().split()) edge[u].append((v,l,a)) deg[v] += 1 res = [] deq = deque([v for v in range(N+1) if deg[v]==0]) while deq: v = deq.popleft() res.append(v) for nv,l,a in edge[v]: deg[nv] -= 1 if not deg[nv]: deq.append(nv) if 0 not in res or N not in res: exit(print("INF")) dp = [-1 for i in range(N+1)] dp[N] = 0 way = [0 for i in range(N+1)] way[N] = 1 mod = 10**9+7 for v in res[::-1]: if v==N: continue for nv,l,a in edge[v]: if dp[nv]!=-1: if dp[v]==-1: dp[v] = 0 dp[v] += dp[nv] * a + l * a * way[nv] dp[v] %= mod way[v] += way[nv] * a way[v] %= mod print(dp[0])