#SCC参考元:https://judge.yosupo.jp/submission/22487 def scc(E): n = len(E) iE = [[] for _ in range(n)] for i, e in enumerate(E): for v in e: iE[v].append(i) T = [] done = [0] * n # 0 -> 1 -> 2 ct = 0 for i0 in range(n): if done[i0]: continue Q = [~i0, i0] while Q: i = Q.pop() if i < 0: if done[~i] == 2: continue done[~i] = 2 T.append(~i) ct += 1 continue if i >= 0: if done[i]: continue done[i] = 1 for j in E[i]: if done[j]: continue Q.append(~j) Q.append(j) done = [0] * n SCC = [] for i0 in T[::-1]: if done[i0]: continue L = [] Q = [~i0, i0] while Q: i = Q.pop() if i < 0: if done[~i] == 2: continue done[~i] = 2 L.append(~i) continue if i >= 0: if done[i]: continue done[i] = 1 for j in iE[i]: if done[j]: continue Q.append(~j) Q.append(j) SCC.append(L) return SCC #================================================ import sys from collections import defaultdict input=sys.stdin.readline N,M=map(int,input().split()) Mod=10**9+7 X=[[] for _ in range(N+1)] E=[defaultdict(lambda :[0,0]) for _ in range(N+1)] for _ in range(M): u,v,w,a=map(int,input().split()) X[u].append(v) b,l=E[u][v] E[u][v]=[b+a,(l+a*w)%Mod] S=scc(X) inf=float("inf") Flag=[0]*(N+1) Flag[0]=1 DP=[[0,0] for _ in range(N+1)] DP[0]=[1,0] for U in S: if len(U)>=2: F=0 for v in U: F|=Flag[v] if F: for v in U: DP[v]=[inf,inf] for u in U: x,y=DP[u] for v in E[u]: Flag[v]|=Flag[u] if x==inf: DP[v]=[inf,inf] else: a,w=E[u][v] DP[v][0]+=x*a DP[v][1]+=y*a+x*w DP[v][0]%=Mod DP[v][1]%=Mod print(DP[N][1] if DP[N][1]