#include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll MOD = 1000000007; template struct matrix{ int n, m; vector> dat; matrix(int n_, int m_){ n = n_; m = m_; for(int i = 0; i < n; i++){ dat.push_back(vector(m)); } } }; template bool prod(matrix a, matrix b, matrix &ans){ if(a.m != b.n) return false; for(int i = 0; i < a.n; i++){ for(int j = 0; j < a.m; j++){ ans.dat[i][j] = 0; for(int k = 0; k < b.m; k++){ ans.dat[i][j] += (a.dat[i][k]*b.dat[k][j])%MOD; ans.dat[i][j] %= MOD; } } } return true; } template void copy_mat(matrix a, matrix &b){ for(int i = 0; i < a.n; i++){ for(int j = 0; j < a.m; j++){ b.dat[i][j] = a.dat[i][j]; } } } template void pow(matrix a, ll n, matrix &ans){ matrix buf(a.n, a.n); matrix tmp(a.n, a.n); copy_mat(a, tmp); for(int i = 0; i < a.n; i++) { for(int j = 0; j < a.n; j++){ ans.dat[i][j] = 0; } ans.dat[i][i] = 1; } for(int i = 0; i <= 61; i++){ ll m = (ll)1 << i; if(m&n){ prod(tmp, ans, buf); copy_mat(buf, ans); } prod(tmp, tmp, buf); copy_mat(buf, tmp); } } ll K, M, N; int P[300], Q[300], R[300]; int idx(int i, int j){ return i*K+j; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; cin >> K >> M >> N; matrix mat(K*K, K*K), buf(K*K, K*K); for(int i = 0; i < M; i++) { cin >> P[i] >> Q[i] >> R[i]; P[i]--; Q[i]--; R[i]--; int from = idx(P[i], Q[i]); int to = idx(Q[i], R[i]); mat.dat[to][from]++; } ll ans = 0; pow(mat, N-2, buf); for(int i = 0; i < K; i++){ for(int j = 0; j < K; j++){ int from = idx(0, i); int to = idx(j, 0); ans += buf.dat[to][from]; ans %= MOD; } } cout << ans << endl; }