#include using namespace std; long long MOD = 1000000007; vector> matrix_multiplication(vector> &A, vector> &B){ int N = A.size(); int M = B.size(); int K = B[0].size(); vector> ans(N, vector(K, 0)); for (int i = 0; i < N; i++){ for (int j = 0; j < M; j++){ for (int k = 0; k < K; k++){ ans[i][k] += A[i][j] * B[j][k]; ans[i][k] %= MOD; } } } return ans; } vector> matrix_exponentiation(vector> A, long long b){ int N = A.size(); vector> ans(N, vector(N, 0)); for (int i = 0; i < N; i++){ ans[i][i] = 1; } while (b > 0){ if (b % 2 == 1){ ans = matrix_multiplication(ans, A); } A = matrix_multiplication(A, A); b /= 2; } return ans; } int main(){ int K, M, N; cin >> K >> M >> N; vector P(M), Q(M), R(M); for (int i = 0; i < M; i++){ cin >> P[i] >> Q[i] >> R[i]; P[i]--; Q[i]--; R[i]--; } vector> mat(K * K * K, vector(K * K * K, 0)); for (int i = 0; i < M; i++){ for (int j = 0; j < M; j++){ if (Q[i] == P[j] && R[i] == Q[j]){ mat[P[j] + Q[j] * K + R[j] * K * K][P[i] + Q[i] * K + R[i] * K * K] = 1; } } } N -= 3; mat = matrix_exponentiation(mat, N); vector vec(K * K * K, 0); for (int i = 0; i < M; i++){ if (P[i] == 0){ vec[P[i] + Q[i] * K + R[i] * K * K] = 1; } } long long ans = 0; for (int i = 0; i < K * K * K; i++){ for (int j = 0; j < K * K * K; j++){ if (i < K * K){ ans += mat[i][j] * vec[j]; } } } ans %= MOD; cout << ans << endl; }