import numpy as np k,m,n = map(int, input().split()) mod = 10**9+7 A = [[0]*(k**2) for _ in range(k**2)] for _ in range(m): p,q,r = map(int, input().split()) s = (p-1)*k+q-1 t = (q-1)*k+r-1 A[s][t] = 1 A=np.array(A) B=np.eye(A.shape[0],A.shape[1], dtype=int) for _ in range(n-2): B = np.dot(B,A) B%=mod ans = 0 for i in range(k): for j in range(k): ans+=B[i][j*k] ans%=mod print(ans)