typedef long long ll; typedef long double ld; #include using namespace std; const ll MOD = 1e9+7; vector> edges(40,vector(40,0)); ll k; vector> rui(ll x){ if(x==1) return edges; auto x1 = edges; auto x2 = edges; if(x%2==1){ x2 = rui(x-1); }else{ x1 = x2 = rui(x/2); } vector> ans(k*k,vector(k*k,0)); for (ll i = 0; i < k*k; i++) { for (ll j = 0; j < k*k; j++) { for (ll ii = 0; ii < k*k; ii++) { ans[i][j] += x1[i][ii]*x2[ii][j]%MOD; ans[i][j] %= MOD; } } } return ans; } int main() { ll m,n; std::cin >> k>>m>>n; for (int i = 0; i < m; i++) { ll p,q,r; std::cin >> p>>q>>r; p--;q--;r--; edges[k*p+q][k*q+r] = 1; } auto x3 = rui(n-2); ll ans = 0; for (int i = 0; i < k; i++) { for (int j = 0; j < k; j++) { ans += x3[k*i+j][k*j+1]%MOD; ans %= MOD; } } std::cout << ans << std::endl; }