#include using namespace std; const long long int MOD = 1e9 + 7; int p[216], q[216], r[216]; long long int k, m, n; vector > mul(vector > a, vector> b) { int n = a.size(); int m = a[0].size(); int k = b[0].size(); vector >c(n, vector(k, 0)); for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { for (int k = 0; k < m; k++) { c[i][j] += ((a[i][k]) * (b[k][j])); c[i][j] %= MOD; } } } return c; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> k >> m >> n; vector > a(m, vector(m, 0)); vector > b(m, vector(m, 0)); for (int i = 0; i < m; i++) { cin >> p[i] >> q[i] >> r[i]; } for (int i = 0; i < m; i++) { for (int j = 0; j <= m; j++) { if (q[i] == p[j] && r[i] == q[j]) { //cout << i << ' ' << j << '\n'; a[i][j] += 1; } } b[i][i] = 1; } n -= 3; while (n > 0) { if (n % 2 == 1) { b = mul(a, b); } a = mul(a, a); n /= 2; } long long int res = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { if (p[i] == 1 && r[j] == 1) res += b[i][j]; res %= MOD; } } cout << res << '\n'; return 0; }