MOD = 10 ** 9 + 7 def matpow(A, B, w): l = len(A) while w: if w & 1: C = [0] * l for i in range(l): for j in range(l): C[i] += A[i][j] * B[j] C[i] %= MOD B = C C = [[0] * l for _ in range(l)] for i in range(l): for j in range(l): for k in range(l): C[i][j] += A[i][k] * A[k][j] C[i][j] %= MOD A = C w >>= 1 return B k, m, n = map(int, input().split()) k2 = k * k A = [[0] * k2 for _ in range(k2)] f = lambda i, j:i * k + j B = [0] * k2 for i in range(k): B[f(0, i)] = 1 for _ in range(m): p, q, r = map(int, input().split()) p -= 1 q -= 1 r -= 1 A[f(q, r)][f(p, q)] += 1 B = matpow(A, B, n - 2) ans = 0 for i in range(k): ans += B[f(i, 0)] print(ans % MOD)