import sys input = sys.stdin.readline def mat_mul(A, B): C = [[0]*len(B[0]) for _ in range(len(A))] for i in range(len(A)): for j in range(len(B[0])): for k in range(len(A[0])): C[i][j] += A[i][k]*B[k][j] C[i][j] %= MOD return C def mat_pow(A, n): res = [[0]*len(A) for _ in range(len(A))] for i in range(len(A)): res[i][i] = 1 while n>0: if n&1: res = mat_mul(res, A) n >>= 1 A = mat_mul(A, A) return res K, M, N = map(int, input().split()) MOD = 10**9+7 A = [[0]*(K*K) for _ in range(K*K)] 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 res = mat_pow(A, N-2) ans = 0 for i in range(K): for j in range(K): ans += res[i][j*K] ans %= MOD print(ans)