#include using namespace std; typedef long long ll; typedef long double ld; #define REP(i, n) for (int i = 0; i < (n); ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, m, n) for (ll i = m; i < n; ++i) #define FORR(i, m, n) for (ll i = m; i >= n; --i) #define ALL(v) (v).begin(),(v).end() templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b= mod) x -= mod; return *this; } constexpr mint& operator-=(const mint& a){ if((x += mod-a.x) >= mod) x -= mod; return *this; } constexpr mint& operator*=(const mint& a){ (x *= a.x) %= mod; return *this; } constexpr mint operator+(const mint& a) const{ mint res(*this); return res+=a; } constexpr mint operator-(const mint& a) const{ mint res(*this); return res-=a; } constexpr mint operator*(const mint& a) const{ mint res(*this); return res*=a; } constexpr mint pow(long long x) const{ if(!x) return 1; mint a = pow(x>>1); a *= a; if(x&1) a *= *this; return a; } constexpr mint inv() const{ return pow(mod-2); } constexpr mint& operator/=(const mint& a){ return (*this) *= a.inv(); } constexpr mint operator/(const mint& a) const{ mint res(*this); return res/=a; } friend ostream& operator<<(ostream& os, const mint& m){ os << m.x; return os; } }; vector matmul(vector &dp,vector> &mt){ int m=dp.size(); vector ret(m); REP(i,m){ REP(j,m){ ret[i]+=mt[i][j]*dp[j]; } } return ret; } vector> update(vector> &mt){ int m=mt.size(); vector> ret(m,vector(m)); REP(i,m){ REP(j,m){ REP(l,m){ ret[i][j]+=mt[i][l]*mt[l][j]; } } } return ret; } void matpow(vector &dp,vector> &mt,ll k){ while(k){ if(k&1){ dp=matmul(dp,mt); } mt=update(mt); k/=2; } } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll k,m,n;cin >> k >> m >> n; vector dp(k*k); vector> a(k*k,vector(k*k)); REP(i,k){ dp[i]=1; } REP(i,m){ int p,q,r;cin >> p >> q >> r; p--,q--,r--; a[q*k+r][p*k+q]=1; } matpow(dp,a,n-2); mint ans=0; REP(i,k){ ans+=dp[i*k]; } cout << ans << endl; }