mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline def matmul(A, B): C = [[0] * len(B[0]) for _ in range(len(A))] for i in range(len(A)): for k in range(len(B)): for j in range(len(B[0])): C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod return C def matpow(A, p): n = len(A) B = [[0] * n for _ in range(n)] for i in range(n): B[i][i] = 1 while p > 0: if p & 1: B = matmul(B, A) A = matmul(A, A) p >>= 1 return B K, M, N = map(int, input().split()) T = [] mat = [[0] * (K*K) for _ in range(K*K)] for _ in range(M): p, q, r = map(int, input().split()) p -= 1 q -= 1 r -= 1 pre = p + q * K post = q + r * K mat[post][pre] = 1 vec = [[0] for _ in range(K*K)] for i in range(K): vec[i*K][0] = 1 vec2 = matmul(matpow(mat, N-2), vec) ans = 0 for i in range(K): ans = (ans + vec2[i][0]) print(ans) if __name__ == '__main__': main()