#include int ri() { int n; scanf("%d", &n); return n; } template struct ModMatrixPower { std::vector > data; std::vector > tmp; ModMatrixPower(int n) { data.resize(n, std::vector(n)); tmp.resize(n, std::vector(n)); } auto & operator [] (int x) { return data[x]; } void pow_self() { size_t n = data.size(); int r0[n][n], r1[n][n]; for (size_t i = 0; i < n; i++) for (size_t j = 0; j < n; j++) { r0[i][j] = data[i][j]; r1[j][i] = data[i][j]; } constexpr size_t BLOCK = 0x8; for (size_t i = 0; i < n; i += BLOCK) { for (size_t j = 0; j < n; j += BLOCK) { for (size_t k = i; k < std::min(i + BLOCK, n); k++) { for (size_t l = j; l < std::min(j + BLOCK, n); l++) { uint64_t sum = 0; size_t upper1 = n & 0x7FFFFFF0; for (size_t m = 0; m < upper1; m += 0x10) { sum += (uint64_t) r0[k][m] * r1[l][m]; sum += (uint64_t) r0[k][m + 1] * r1[l][m + 1]; sum += (uint64_t) r0[k][m + 2] * r1[l][m + 2]; sum += (uint64_t) r0[k][m + 3] * r1[l][m + 3]; sum += (uint64_t) r0[k][m + 4] * r1[l][m + 4]; sum += (uint64_t) r0[k][m + 5] * r1[l][m + 5]; sum += (uint64_t) r0[k][m + 6] * r1[l][m + 6]; sum += (uint64_t) r0[k][m + 7] * r1[l][m + 7]; sum += (uint64_t) r0[k][m + 8] * r1[l][m + 8]; sum += (uint64_t) r0[k][m + 9] * r1[l][m + 9]; sum += (uint64_t) r0[k][m + 10] * r1[l][m + 10]; sum += (uint64_t) r0[k][m + 11] * r1[l][m + 11]; sum += (uint64_t) r0[k][m + 12] * r1[l][m + 12]; sum += (uint64_t) r0[k][m + 13] * r1[l][m + 13]; sum += (uint64_t) r0[k][m + 14] * r1[l][m + 14]; sum += (uint64_t) r0[k][m + 15] * r1[l][m + 15]; sum %= mod; } for (size_t m = upper1; m < n; m++) sum += (uint64_t) r0[k][m] * r1[l][m]; tmp[k][l] = sum % mod; } } } } std::swap(tmp, data); } void apply(std::vector &vec) { size_t n = data.size(); assert(vec.size() == n); std::vector vec_tmp(n); for (size_t i = 0; i < n; i++) for (size_t j = 0; j < n; j++) { vec_tmp[j] += (int64_t) vec[i] * data[i][j] % mod; if (vec_tmp[j] >= mod) vec_tmp[j] -= mod; } std::swap(vec, vec_tmp); } }; #define MOD 1000000007 int main() { int k = ri(); int m = ri(); int64_t n; scanf("%" SCNd64, &n); ModMatrixPower mat(k * k); for (int i = 0; i < m; i++) { int p = ri() - 1; int q = ri() - 1; int r = ri() - 1; mat[p * k + q][q * k + r] = 1; } std::vector all(k * k); for (int i = 0; i < k; i++) all[i] = 1; n -= 2; for (; n; n >>= 1) { if (n & 1) mat.apply(all); mat.pow_self(); } int64_t res = 0; for (int i = 0; i < k; i++) res += all[i * k]; printf("%d\n", (int) (res % MOD)); return 0; }