import sys def MI(): return map(int,sys.stdin.readline().rstrip().split()) def matrix_multiplication_mod(A,B,p): l = len(A) m = len(B) n = len(B[0]) C = [[0]*n for _ in range(l)] for i in range(l): for j in range(n): C[i][j] = sum((A[i][k]*B[k][j]) % p for k in range(m)) % p return C def matrix_power_mod(A,n,p): l = len(A) C = [[0] * l for _ in range(l)] for i in range(l): C[i][i] = 1 while n > 0: if n % 2 == 1: C = matrix_multiplication_mod(C,A,p) A = matrix_multiplication_mod(A,A,p) n >>= 1 return C K,M,N = MI() mod = 10**9+7 PQR = set([tuple(MI()) for _ in range(M)]) A = [[0]*(K**2) for _ in range(K**2)] for i in range(K**2): i0,i1 = divmod(i,K) for j in range(K**2): j0,j1 = divmod(j,K) if i0 != j1: continue if (j0+1,i0+1,i1+1) in PQR: A[i][j] = 1 X = matrix_power_mod(A,N-2,mod) ans = 0 for i in range(0,K**2,K): for j in range(K): ans += X[i][j] ans %= mod print(ans)