# 行列の乗算(mod) def mat_mul(a, b): I, K, J = len(a), len(b), len(b[0]) c = [[0 for j in range(J)] for i in range(I)] for i in range(I): for k in range(K): for j in range(J): c[i][j] += a[i][k] * b[k][j] c[i][j] %= mod return c # 行列の累乗(mod) def mat_pow(a, n): b = [[0 for j in range(len(a))] for i in range(len(a))] for i in range(len(a)): b[i][i] = 1 while n > 0: if n & 1: b = mat_mul(b, a) a = mat_mul(a, a) n >>= 1 return b K,m,n = map(int,input().split()) mod = 10**9+7 dp = [[0]*(K*K) for i in range(K*K)] for i in range(m): p,q,r = map(int,input().split()) p -= 1 q -= 1 r -= 1 pq = p*K+q qr = q*K+r dp[pq][qr] = 1 A = mat_pow(dp,n-2) ans = 0 for i in range(K): for j in range(K): ans += A[i][j*K] ans %= mod print(ans) #print(A)