#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair P; const ll MOD=1e9+7; vector> matrixmul(int l, int m, int n, vector> a, vector> b){ vector> c(l, vector(n)); for(int i=0; i> matrixpow(int n, vector> a, ll k){ vector> ap=a, ans(n, vector(n)); for(int i=0; i>=1; } return ans; } int main() { int k, m; ll n; cin>>k>>m>>n; vector> mat(k*k, vector(k*k)); int p[300], q[300], r[300]; for(int i=0; i>p[i]>>q[i]>>r[i]; p[i]--; q[i]--; r[i]--; mat[p[i]*k+q[i]][q[i]*k+r[i]]=1; } auto matp=matrixpow(k*k, mat, n-2); ll ans=0; for(int i=0; i