""" ほぼ同じじゃね? 行列累乗か 1つ目の音*6 + 2つ目の音でキーにする """ #行列A,Bの積(不可能だとエラー,modに0以下の値を指定するとmodを取らなくなる) def matrix_mul(A,B,mod): ans = [ [0] * len(B[0]) for i in range(len(A)) ] for ai in range(len(A)): for bj in range(len(B[0])): now = 0 for same in range(len(A[0])): now += A[ai][same] * B[same][bj] if mod > 0: ans[ai][bj] = now % mod else: ans[ai][bj] = now return ans #行列Aのx乗(当然正方行列じゃないとだめ) def matrix_pow(A,x,mod): B = [[A[i][j] for j in range(len(A[0]))] for i in range(len(A))] ans = [[0] * len(A[0]) for i in range(len(A))] for i in range(len(A)): ans[i][i] = 1 while x > 0: if x % 2 == 1: ans = matrix_mul(ans,B,mod) B = matrix_mul(B,B,mod) x//=2 return ans mod = 10**9+7 K,M,N = map(int,input().split()) fi = [ [0] * 36 ] for i in range(K): fi[0][i] = 1 H = [[0] * 36 for i in range(36)] for i in range(M): P,Q,R = map(int,input().split()) P -= 1 Q -= 1 R -= 1 be = P*6+Q ne = Q*6+R H[be][ne] = 1 ans = matrix_mul(fi,matrix_pow(H,N-2,mod),mod) #print (ans) pr = 0 for i in range(36): if i % 6 == 0: pr += ans[0][i] print (pr % mod)