import sys input = sys.stdin.buffer.readline sys.setrecursionlimit(10 ** 7) mod = 10**9+7 def mat_mul(A, B): res = [[0] * len(B[0]) for _ in range(len(A))] for i in range(len(A)): for k in range(len(A[0])): for j in range(len(B[0])): res[i][j] += A[i][k] * B[k][j] res[i][j] %= mod return res def mat_pow(A, n): size = len(A) res = [[0] * size for _ in range(size)] for i in range(size): res[i][i] = 1 while n: if n & 1: res = mat_mul(res, A) A = mat_mul(A, A) n >>= 1 return res K, M, N = map(int, input().split()) A = [[0]*(K*K) for _ in range(K*K)] for _ in range(M): a, b, c = map(int, input().split()) a -= 1 b -= 1 c -= 1 s = a*K+b t = b*K+c A[t][s] += 1 A = mat_pow(A, N-2) ans = 0 for r in range(0, K*K, K): for c in range(K): ans += A[r][c] ans %= mod print(ans)