#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define all(a) a.begin(),a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #define debug(x) cerr << #x << ':' << x << '\n'; #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair P; typedef complex com; constexpr int inf = 1000000010; constexpr ll INF = 1000000000000000010; constexpr ld eps = 1e-12; constexpr ld pi = 3.141592653589793238; template inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } constexpr ll mod = 1000000007; vector> mat_mul(vector> a, vector> b) { int n = a.size(); vector> ans(n, vector(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { ans[i][j] += a[i][k] * b[k][j]; ans[i][j] %= mod; } } } return ans; } vector> mat_pow(vector> a, ll b) { int n = a.size(); vector> ans(n, vector(n)); for (int i = 0; i < n; i++) ans[i][i] = 1; while (b) { if (b & 1) ans = mat_mul(ans, a); a = mat_mul(a, a); b >>= 1; } return ans; } struct str { int x, y, z; }; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); ll k, m, n; cin >> k >> m >> n; vector> mat(216, vector(216)); vector cnt(216); vector a(m); rep(i, m) { int p, q, r; cin >> p >> q >> r; p--; q--; r--; a[i] = { p,q,r }; if (!p) cnt[6 * q + r]++; } rep(i, m) { rep(j, m) { if (a[i].y == a[j].x && a[i].z == a[j].y) { int numi = 36 * a[i].x + 6 * a[i].y + a[i].z; int numj = 36 * a[j].x + 6 * a[j].y + a[j].z; mat[numj][numi]++; } } } mat = mat_pow(mat, n - 3); ll ans = 0; rep(i, 216) { rep(j, 216) { if (i % 6 == 0) ans += mat[i][j] * cnt[j] % mod; } } cout << ans % mod << '\n'; }