#include using namespace std; int main() { const long mod = 1000000007; int k, m, n; cin >> k >> m >> n; vector> next(k * k); vector>> dp(n - 1, vector>(k, vector(k))); for (int i = 0; i < m; i++) { int p, q, r; cin >> p >> q >> r; p--; q--; r--; next.at(p * k + q).push_back(r); if (p == 0) dp[0][p][q] = 1; } for (int i = 1; i < n - 1; i++) { for (int p = 0; p < k; p++) { for (int q = 0; q < k; q++) { for (auto r : next.at(p * k + q)) { dp[i][q][r] += dp[i - 1][p][q]; dp[i][q][r] %= mod; } } } } long ans = 0; for (int i = 0; i < k; i++) { ans += dp[n - 2][i][0]; ans %= mod; } cout << ans << endl; }