#include using namespace std; #define int long long #define REP(i,m,n) for(int i=(m);i<(n);i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define all(a) a.begin(),a.end() #define rall(c) (c).rbegin(),(c).rend() #define mp make_pair #define endl '\n' #define vec vector #define mat vector > typedef long long ll; typedef pair pll; typedef long double ld; const ll inf=1e9+7; const ll mod=1e9+7; #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") //行列累乗 mat mul(mat &A,mat &B){ mat C(A.size(),vec(B[0].size())); rep(i,A.size()){ rep(k,B[0].size()){ rep(j,B.size()){ C[i][j]=(C[i][j]+(A[i][k]*B[k][j])%mod)%mod; } } } return C; } mat pow(mat &A,ll k){ mat B(A.size(),vec(A.size())); rep(i,A.size())B[i][i]=1; while(k>0){ if(k&1)B=mul(B,A); A=mul(A,A); k>>=1; } return B; } signed main(){ ll k,m,n;cin>>k>>m>>n; ll K=k*k*k; vector >a(K,vector(K)); vector > >b(k,vector >(k,vector (k))); rep(i,m){ ll p,q,r;cin>>p>>q>>r; p--;q--;r--; b[r][q][p]=1; } rep(p,k){ rep(q,k){ rep(r,k){ if(!b[p][q][r])continue; ll i=p*k*k+q*k+r; rep(pp,k){ rep(qq,k){ rep(rr,k){ ll j=pp*k*k+qq*k+rr; if(b[p][q][r]){ if(pp==q&&qq==r){ a[i][j]=1; } } } } } } } } mat c(K,vector(K)); c=pow(a,n-3); ll ans=0; rep(p,k){ if(p>0)continue; rep(q,k){ rep(r,k){ ll i=p*k*k+q*k+r; rep(pp,k){ rep(qq,k){ rep(rr,k){ ll j=pp*k*k+qq*k+rr; if(rr==0&&p==0){ if(b[pp][qq][rr]){ ans=(ans+c[i][j])%mod; } } } } } } } } cout<