#include using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define elif else if #define sp(x) fixed << setprecision(x) #define pb push_back #define eb emplace_back #define all(x) x.begin(), x.end() #define sz(x) (int)x.size() using ll = long long; using ld = long double; using pii = pair; using pil = pair; using pli = pair; using pll = pair; const ll MOD = 1e9+7; //const ll MOD = 998244353; const int inf = (1<<30)-1; const ll INF = (1LL<<60)-1; const ld EPS = 1e-10; template bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;}; template bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;}; using vec = vector; using mat = vector; mat mat_mul(mat A, mat B){ //A(l×m行列)、 B(m×n行列) int l = sz(A), m = sz(B), n = sz(B[0]); //C(l×n行列) mat C(l, vec(n, 0)); rep(i, l){ rep(k, m){ rep(j, n){ C[i][j] += A[i][k]*B[k][j]; C[i][j] %= MOD; } } } return C; } mat mat_pow(mat A, ll k){ //A(n次正方行列) int n = sz(A); //B(n次正方行列) mat B(n, vec(n, 0)); rep(i, n) B[i][i] = 1; while(k > 0){ if(k&1) B = mat_mul(B, A); A = mat_mul(A, A); k >>= 1; } return B; } using vec2 = vector; using mat2 = vector; //AをO(m*n^2)で行標準化し、rankを出力する int standard_mat(mat2 &A){ int m = sz(A), n = sz(A[0]); //今どの行を埋めていきたいか int now = 0, ret = 0; rep(j, n){ int pivot = now; rep2(i, now, m-1){ if(abs(A[i][j]) > abs(A[pivot][j])) pivot = i; } swap(A[now], A[pivot]); if(abs(A[now][j]) < EPS) continue; ret++; rep2(k, j+1, n-1) A[now][k] /= A[now][j]; A[now][j] = 1; rep(i, m){ if(i == now) continue; rep2(k, j+1, n-1) A[i][k] -= A[i][j]*A[now][k]; A[i][j] = 0; } now++; if(now == m) break; } return ret; } //n元連立一次方程式Ax=bをO(n^3)で解く(解がないまたは一意に定まらない場合は空の配列を出力) vec2 gausiann_elimination(mat2 A, vec2 b){ int n = sz(A); mat2 B(n, vec2(n+1)); rep(i, n){ rep(j, n) B[i][j] = A[i][j]; B[i][n] = b[i]; } standard_mat(B); vec2 x(n); rep(i, n){ if(abs(B[i][i]) < EPS) return vec2(0); x[i] = B[i][n]; } return x; } int main(){ int K, M; ll N; cin >> K >> M >> N; int e[K*K*K]; fill(e, e+K*K*K, false); rep(i, M){ int a, b, c; cin >> a >> b >> c; a--, b--, c--; e[a*K*K+b*K+c] = true; } mat A(K*K, vec(K*K, 0)); rep(i, K*K){ rep(j, K){ if(e[i*K+j]) A[i][(i%K)*K+j] = 1; } } A = mat_pow(A, N-2); mat x(K*K, vec(1, 0)); rep(i, K*K) if(i%K == 0) x[i][0] = 1; mat res = mat_mul(A, x); ll ans = 0; rep(i, K*K) if(i/K == 0) ans += res[i][0]; cout << ans%MOD << endl; }