#include using namespace std; struct iofast_t { iofast_t() { ios::sync_with_stdio(false); cin.tie(nullptr); } } iofast; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } template > T &chmin(T &l, T r, Compare &&f = less()) { return l = min(l, r, f); } template > T &chmax(T &l, T r, Compare &&f = less()) { return l = max(l, r, f); } #include using mint = atcoder::modint1000000007; int main() { constexpr int lim = 6; constexpr int log = 60; int64_t k, m, n; cin >> k >> m >> n; auto dub = vec(0, log + 1, lim, lim, lim, lim); for (int i = 0; i < m; ++i) { int p, q, r; cin >> p >> q >> r; --p; --q; --r; dub[0][p][q][q][r] = 1; } for (int l = 1; l <= log; ++l) { for (int i = 0; i < lim; ++i) { for (int j = 0; j < lim; ++j) { for (int p = 0; p < lim; ++p) { for (int q = 0; q < lim; ++q) { for (int s = 0; s < lim; ++s) { for (int t = 0; t < lim; ++t) { dub[l][i][j][s][t] += dub[l - 1][i][j][p][q] * dub[l - 1][p][q][s][t]; } } } } } } } auto dp1 = vec(0, lim, lim); auto dp2 = vec(0, lim, lim); fill(begin(dp1[0]), end(dp1[0]), 1); n -= 2; for (int l = 0; l <= log; ++l) { if (!(n & (INT64_C(1) << l))) { continue; } for (int i = 0; i < lim; ++i) { for (int j = 0; j < lim; ++j) { dp2[i][j] = 0; } } for (int i = 0; i < lim; ++i) { for (int j = 0; j < lim; ++j) { for (int p = 0; p < lim; ++p) { for (int q = 0; q < lim; ++q) { dp2[p][q] += dp1[i][j] * dub[l][i][j][p][q]; } } } } swap(dp1, dp2); } mint ans = 0; for (int i = 0; i < lim; ++i) { ans += dp1[i][0]; } cout << ans.val() << endl; }