#include using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; template using vc = vector; template using vvc = vc>; using pi = pair; using vi = vc; using vvi = vvc; #define rep(i,a,b) for (int i = a; i < b; i++) #define irep(i,a,b) for (int i = a; i > b; i--) #define print(n) cout << n << endl #define rup(a,b) (a+b-1)/b #define input(A,N) rep(i,0,N) cin>>A[i]; vvc mattimes(vvc A,vvc B,ll mod = 1){ vvc now(A.size(),vc(A.size(),0)); rep(i,0,A.size()){ rep(j,0,A.size()){ rep(k,0,A.size()){ now[i][j] += (A[i][k]*B[k][j])%mod; now[i][j] %= mod; } } } return now; } vc mattimes(vvc A,vc B,ll mod = 1){ vc now(B.size(),0); rep(i,0,B.size()){ rep(j,0,B.size()){ now[i] += (A[i][j]*B[j])%mod; now[i] %= mod; } } return now; } int main(){ cout << fixed << setprecision(15); int K,M; ll N; cin>>K>>M>>N; int P[M],Q[M],R[M]; rep(i,0,M) {cin>>P[i]>>Q[i]>>R[i];P[i]--;Q[i]--;R[i]--;} vvc mat(K*K,vc(K*K,0)); rep(i,0,M){ int p,q; p = P[i]*K + Q[i]; q = Q[i]*K + R[i]; mat[q][p] = 1; } ll mod = ll(pow(10,9)) + 7; vvc A(K*K,vc(K*K,0)); rep(i,0,K*K) A[i][i] = 1; int now = 0; while (pow(2,now)<=N-2){ if (N-2>>now & 1){ A = mattimes(A,mat,mod); } mat = mattimes(mat,mat,mod); now ++ ; } vc ans(K*K,0); rep(i,0,K){ ans[i] = 1; } ans = mattimes(A,ans,mod); ull count = 0; rep(i,0,K){ count += ans[i*K]; count %= mod; } print(count); //system("pause"); return 0; }