def _mul(A, B, MOD): C = [[0] * len(B[0]) for i 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 pow_matrix(A, n, MOD): """A**nをダブリングによって求める。""" B = [[0] * 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 = _mul(A, B, MOD) A = _mul(A, A, MOD) n = n // 2 return B k, m, n = map(int, input().split()) info = [list(map(int, input().split())) for i in range(m)] MOD = 10 ** 9 + 7 # matrix_l * a * matrix_r matrix = [[0] * (k ** 2) for i in range(k ** 2)] for p, q, r in info: p -= 1 q -= 1 r -= 1 row = 0 col = 0 row += r * k row += q col += q * k col += p matrix[row][col] += 1 matrix = pow_matrix(matrix, n - 2, MOD) ans = [0] * (k ** 2) for i in range(k): ans[i * k] = 1 res = 0 for i in range(k): for j in range(k ** 2): res += matrix[i][j] * ans[j] res %= MOD print(res)