import sys import io, os input = sys.stdin.buffer.readline mod = 10**9+7 import numpy as np #A**n def mat_pow(A, n, mod): size = len(A) res = np.eye(size, dtype=np.object) while n > 0: if n & 1 == 1: res = res @ A res %= mod A = A @ A A %= mod n = n>>1 return res k, m, n = map(int, input().split()) PQR = [] for i in range(m): p, q, r = map(int, input().split()) p, q, r = p-1, q-1, r-1 PQR.append((p, q, r)) #A = [[0]*(k**2) for i in range(k**2)] A = np.zeros((k**2, k**2), dtype=np.object) for i in range(m): p, q, r = PQR[i] u = p*k+q v = q*k+r A[u][v] += 1 A = mat_pow(A, n-2, mod) ans = 0 for i in range(k): v = 0*k+i for j in range(k): u = j*k+0 ans += A[v][u] #print(A) print(ans%mod)