#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef pair P; typedef long long ll; typedef long double ld; const int inf=1e9+7; const ll longinf=1LL<<60; #define REP(i,m,n) for(ll i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) #define F first #define S second constexpr char ln = '\n'; const int mx=200010; const ll mod=1e9+7; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} 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 { mint res(*this); return res+=a; } mint operator-(const mint a) const { mint res(*this); return res-=a; } mint operator*(const mint a) const { mint res(*this); return res*=a; } mint pow(ll 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) const { mint res(*this); return res/=a; } }; // matrix pow mod void matpom(ll*mat,int size,ll n,int m){ ll*temp=(ll*)malloc(size*size*sizeof(ll)); ll*ans=(ll*)malloc(size*size*sizeof(ll)); rep(i,size)rep(j,size)ans[i*size+j] = i==j; while(n){ if(n%2){ rep(i,size*size)temp[i]=0; rep(i,size)rep(j,size)rep(k,size)temp[i*size+j]=(temp[i*size+j]+ans[i*size+k]*mat[k*size+j])%m; rep(i,size*size)ans[i]=temp[i]; } rep(i,size*size)temp[i]=0; rep(i,size)rep(j,size)rep(k,size)temp[i*size+j]=(temp[i*size+j]+mat[i*size+k]*mat[k*size+j])%m; rep(i,size*size)mat[i]=temp[i]; n/=2; } rep(i,size*size)mat[i]=ans[i]; free(temp); free(ans); } int main(){ ll n,m,k; cin >> k >> m >> n; ll v[36][36]; rep(i,36)rep(j,36){ v[i][j] = 0; } rep(i,m){ int a,b,c; cin >> a >> b >> c; a--; b--; c--; v[a+b*6][b+c*6] = 1; } matpom((ll*)v,36,n-2,mod); mint ans = 0; rep(i,6)rep(j,6){ ans += v[0+i*6][j+0*6]; } cout << ans.x << ln; return 0; }