from collections import defaultdict 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() dp = [defaultdict(int) for _ in range(N + 1)] dp[N][0] += 1 # len, cnt for s in topo[::-1][:-1]: for k, v in dp[s].items(): for t, dist, num in back[s]: dp[t][k + dist] += v * num % mod dp[t][k + dist] %= mod ans = 0 for k, v in dp[0].items(): ans += k * v ans %= mod print(ans)