import numpy as np MOD = 10 ** 9 + 7 int1 = lambda x: int(x) - 1 def pow_matrix_mod(x, n, mod=MOD): if not n: return np.eye(len(x), dtype=np.object) if n % 2 == 0: return pow_matrix_mod(x @ x % mod, n // 2) % mod else: return x @ pow_matrix_mod(x @ x % mod, (n - 1) // 2) % mod K, M, N = map(int, input().split()) matr = np.zeros((36, 36), dtype=np.object) for _ in range(M): P, Q, R = map(int1, input().split()) matr[6 * P + Q, 6 * Q + R] = True A = np.zeros(36, dtype=np.object) A[:6] = 1 print((A @ pow_matrix_mod(matr, N - 2))[::6].sum() % MOD)