#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; //行列累乗 mat mul(mat &A,mat &B){ mat C(A.size(),vec(B[0].size())); rep(i,A.size()){ rep(j,B[0].size()){ rep(k,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[p][q][r]=1; } rep(i,K){ ll r=i%k; ll q=i%(k*k)/k; ll p=i/(k*k); rep(j,K){ ll rr=j%k; ll qq=j%(k*k)/k; ll pp=j/(k*k); if(b[p][q][r]){ if(pp==q&&qq==r){ a[i][j]=1; } } } } vector >c(K,vector(K)); c=pow(a,n-3); ll ans=0; rep(i,K){ ll r=i%k; ll q=i%(k*k)/k; ll p=i/(k*k); rep(j,K){ ll rr=j%k; ll qq=j%(k*k)/k; ll pp=j/(k*k); if(rr==0&&p==0){ if(b[pp][qq][rr]){ ans=(ans+c[i][j])%mod; } } } } cout<