import sys import numpy as np read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines MOD = 10**9 + 7 K, M, N = map(int, readline().split()) m = map(lambda x: int(x) - 1, read().split()) PQR = tuple(zip(m, m, m)) def make_mat(PQR): A = np.zeros((K * K, K * K), np.int64) for p, q, r in PQR: key1 = K * p + q key2 = K * q + r A[key2, key1] = 1 return A def mat_mul(A, B): A1, A2 = A >> 15, A & (1 << 15) - 1 B1, B2 = B >> 15, B & (1 << 15) - 1 X = np.dot(A1, B1) % MOD Y = np.dot(A2, B2) Z = (np.dot(A1 + A2, B1 + B2) - X - Y) return ((X << 30) + (Z << 15) + Y) % MOD def mat_power(A, N): P = np.eye(A.shape[0], dtype=np.int64) while N: if N & 1: P = mat_mul(P, A) A = mat_mul(A, A) N >>= 1 return P A = make_mat(PQR) B = mat_power(A, N - 2) ans = 0 for a in range(K): for b in range(K): key1 = a key2 = b * K ans += B[key2, key1] print(ans % MOD)