#include #pragma GCC optimize("Ofast") #define _GLIBCXX_DEBUG using namespace std; using std::cout; using std::cin; using std::endl; using ll=long long; using ld=long double; ll ILL=2167167167167167167; const int INF=2100000000; const ll mod=1e9+7; #define rep(i,a) for (ll i=0;i using _pq = priority_queue, greater>; template ll LB(vector &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();} template ll UB(vector &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();} template bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;} template bool chmax(T &a,const T &b){if(a void So(vector &v) {sort(v.begin(),v.end());} template void Sore(vector &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});} void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";} template void vec_out(vector &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout< fact; std::vector rev; std::vector fact_rev; combination(int max,long long mod):upper(max),MOD(mod),fact(max+1),rev(max+1),fact_rev(max+1){ for(long long i=0;i<=max;i++){ if(i<2){ fact[i]=1; fact_rev[i]=1; rev[i]=1; continue; } fact[i]=(fact[i-1]*i)%mod; rev[i]=mod-((mod/i)*rev[mod%i])%mod; fact_rev[i]=(fact_rev[i-1]*rev[i])%mod; } } long long Comb(int x,int y){ assert(upper>=x); if (x>t; rep(i,t) solve(); } void solve(){ ll N,M; cin>>N>>M; combination table(N+1,mod); map m; ll ans=1; vector p(N),seen(N); rep(i,N) cin>>p[i],p[i]--; rep(i,N){ if(seen[i]) continue; int ind=i,C=0; while(seen[ind]==0){ seen[ind]=1; C++; ind=p[ind]; } m[C]++; } for(auto x:m){ vector dp(x.second+1); dp[0]=1; for(int i=1;i<=x.second;i++){ if(M%i!=0) continue; int tmp=(M/i)%x.first; if(__gcd(tmp,(int)x.first)!=1) continue; vector n_dp(x.second+1); ll potato=1; for(int j=0;j*i<=x.second;j++){ for(int k=0;k+j*i<=x.second;k++){ int ind=k+i*j; ll val=(potato*table.fact_rev[j])%mod; val=(val*dp[k])%mod; n_dp[ind]=(n_dp[ind]+val)%mod; } potato=(potato*table.rev[i])%mod; } swap(n_dp,dp); } ans=(ans*dp[x.second])%mod; ans=(ans*table.fact[x.second])%mod; } cout<