import sys input=lambda: sys.stdin.readline().rstrip() k,m,n=map(int,input().split()) mod=10**9+7 PQR=[[int(i)-1 for i in input().split()] for _ in range(m)] A=[[0]*(k**2) for _ in range(k**2)] for p,q,r in PQR: s,g=p*k+q,q*k+r A[g][s]+=1 def mul(a,b): c=[[0]*len(b[0]) for i in range(len(a))] for i in range(len(a)): for k in range(len(b)): for j in range(len(b[0])): c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod return c def matrix_pow(a,x): b=[[0]*len(a) for i in range(len(a))] for i in range(len(a)): b[i][i] = 1 while x>0: if x&1==1: b=mul(a,b) a=mul(a,a) x>>=1 return b AN=matrix_pow(A,n-2) Ans=[0]*(k**2) for i in range(k**2): for j in range(k): Ans[i]+=AN[i][j] Ans[i]=Ans[i]%mod ans=0 for i in range(k): ans+=Ans[i*k] print(ans)