//#define _GLIBCXX_DEBUG #include #define rep(i, n) for(int i=0; i; using vs = vector; using vi = vector; using vvi = vector; template using PQ = priority_queue; template using PQG = priority_queue, greater >; const int INF = 100010001; const ll LINF = (ll)INF*INF*10; template inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);} template inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);} template istream &operator>>(istream &is, pair &p) { return is >> p.first >> p.second;} template ostream &operator<<(ostream &os, const pair &p) { return os << p.first << ' ' << p.second;} const int mod = 1000000007; //const int mod = 998244353; struct mint { int64_t x; mint(int64_t x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(int64_t t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } //for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) {return mint(*this) /= a;} }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} const int N = 216; //head int k, m; ll n; int p[N], q[N], r[N]; using V = vector; using M = vector; M mul(const M& a, const M b) { int n = a.size(); M res(n, V(n)); rep(i, n) rep(j, n) rep(k, n) { res[i][j] += a[i][k] * b[k][j]; } return res; } V mul(const M& a, const V& b) { int n = a.size(); V res(n); rep(i, n) rep(j, n) { res[i] += a[i][j] * b[j]; } return res; } M pow(const M& a, ll p) { int n = a.size(); if(!p) { M res(n, V(n)); rep(i, n) res[i][i] = 1; return res; } M res = pow(a, p>>1); res = mul(res, res); if(p&1) res = mul(a, res); return res; } inline int num(int i, int j, int k_) { return i*k*k + j*k + k_; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> k >> m >> n; int uu = k*k*k; rep(i, m) cin >> p[i] >> q[i] >> r[i]; V cc(uu); M mat(uu, V(uu)); rep(i, m) { p[i]--; q[i]--; r[i]--; if(!p[i]) { cc[num(0, q[i], r[i])] = 1; } rep(j, k) { mat[num(p[i], q[i], r[i])][num(j, p[i], q[i])] = 1; } } mat = pow(mat, n-3); cc = mul(mat, cc); mint ans; rep(iu, k) rep(ju, k) { ans += cc[num(iu, ju, 0)]; } cout << ans << endl; }