from time import time import sys import io, os sys.setrecursionlimit(10**9) input = sys.stdin.buffer.readline #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline mod = 10**9+7 n, m = map(int, input().split()) ind = [[] for i in range(n+1)] for i in range(m): u, v, l, a = map(int, input().split()) ind[v].append((u, l, a)) dp = [0]*(n+1) def dfs(v): tt = (time() - stime) / 2.2 if tt >= 1: print('INF') exit() for u, l, a in ind[v]: dfs(u) dp[v] += (dp[u]+l)*a dp[v] %= mod stime = time() dfs(n) #print(dp) print(dp[n])