/* -*- coding: utf-8 -*- * * 1112.cc: No.1112 冥界の音楽 - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_K = 6; const int MAX_KK = MAX_K * MAX_K; const int MOD = 1000000007; /* typedef */ typedef long long ll; typedef int vec[MAX_KK]; typedef vec mat[MAX_KK]; /* global variables */ mat ma, mb; vec va, vb; /* subroutines */ inline void initmat(const int n, mat a) { memset(a, 0, sizeof(mat)); } inline void unitmat(const int n, mat a) { initmat(n, a); for (int i = 0; i < n; i++) a[i][i] = 1; } inline void copymat(const int n, const mat a, mat b) { memcpy(b, a, sizeof(mat)); } inline void mulmat(const int n, const mat a, const mat b, mat c) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { c[i][j] = 0; for (int k = 0; k < n; k++) c[i][j] = (c[i][j] + (ll)a[i][k] * b[k][j] % MOD) % MOD; } } inline void powmat(const int n, const mat a, ll b, mat c) { mat s, t; copymat(n, a, s); unitmat(n, c); while (b > 0) { if (b & 1) { mulmat(n, c, s, t); copymat(n, t, c); } mulmat(n, s, s, t); copymat(n, t, s); b >>= 1; } } inline void mulmatvec(const int n, const mat a, const vec b, vec c) { for (int i = 0; i < n; i++) { c[i] = 0; for (int j = 0; j < n; j++) c[i] = (c[i] + (ll)a[i][j] * b[j] % MOD) % MOD; } } /* main */ int main() { int k, m; ll n; scanf("%d%d%lld", &k, &m, &n); int kk = k * k; for (int i = 0; i < m; i++) { int p, q, r; scanf("%d%d%d", &p, &q, &r); p--, q--, r--; int u = p * k + q, v = q * k + r; ma[v][u] = 1; } powmat(kk, ma, n - 2, mb); for (int i = 0; i < k; i++) va[i] = 1; mulmatvec(kk, mb, va, vb); int sum = 0; for (int i = 0; i < k; i++) sum = (sum + vb[i * k]) % MOD; printf("%d\n", sum); return 0; }