Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

class SCC_BI {
public:
	static const int MV = 210000;
	int NV,time;
	vector<vector<int> > E;
	vector<int> ord,llink,inin;
	stack<int> roots,S;
	vector<int> M; //point to group
	vector<int> ART; // out
	vector<vector<int> > SC; // out
	vector<pair<int,int> > BR; // out
	
	void init(int NV=MV) { this->NV=NV; E.clear(); E.resize(NV);}
	void add_edge(int x,int y) { assert(NV);E[x].push_back(y); E[y].push_back(x); }
	void dfs(int cur,int pre) {
		int art=0,conn=0,i,se=0;
		ord[cur]=llink[cur]=++time;
		S.push(cur); inin[cur]=1; roots.push(cur);
		FOR(i,E[cur].size()) {
			int tar=E[cur][i];
			if(ord[tar]==0) {
				conn++; dfs(tar,cur);
				llink[cur]=min(llink[cur],llink[tar]);
				art += (pre!=-1 && ord[cur]<=llink[tar]);
				if(ord[cur]<llink[tar]) BR.push_back(make_pair(min(cur,tar),max(cur,tar)));
			}
			else if(tar!=pre || se) {
				llink[cur]=min(llink[cur],ord[tar]);
				while(inin[tar]&&ord[roots.top()]>ord[tar]) roots.pop();
			}
			else se++; // double edge
		}
		
		if(cur==roots.top()) {
			SC.push_back(vector<int>());
			while(1) {
				i=S.top(); S.pop(); inin[i]=0;
				SC.back().push_back(i);
				M[i]=SC.size()-1;
				if(i==cur) break;
			}
			sort(SC.back().begin(),SC.back().end());
			roots.pop();
		}
		if(art || (pre==-1&&conn>1)) ART.push_back(cur);
	}
	void scc() {
		SC.clear(),BR.clear(),ART.clear(),M.resize(NV);
		ord.clear(),llink.clear(),inin.clear(),time=0;
		ord.resize(NV);llink.resize(NV);inin.resize(NV);
		for(int i=0;i<NV;i++) if(!ord[i]) dfs(i,-1);
		sort(BR.begin(),BR.end()); sort(ART.begin(),ART.end());
	}
};

struct RollingHash {
	static const ll mo0=1000000007,mo1=1000000009;
	static ll mul0,mul1;
	static const ll add0=1000010007, add1=1003333331;
	static vector<ll> pmo[2];
	int l; vector<ll> hash_[2];
	void init(vector<int> s) {
		l=s.size(); int i,j;
		hash_[0]=hash_[1]=vector<ll>(1,0);
		if(!mul0) mul0=10009+(((ll)&mul0)>>5)%259,mul1=10007+(((ll)&mul1)>>5)%257;
		if(pmo[0].empty()) pmo[0].push_back(1),pmo[1].push_back(1);
		FOR(i,l) hash_[0].push_back((hash_[0].back()*mul0+add0+s[i])%mo0);
		FOR(i,l) hash_[1].push_back((hash_[1].back()*mul1+add1+s[i])%mo1);
	}
	pair<ll,ll> hash(int l,int r) { // s[l..r]
		if(l>r) return make_pair(0,0);
		while(pmo[0].size()<r+2)
			pmo[0].push_back(pmo[0].back()*mul0%mo0), pmo[1].push_back(pmo[1].back()*mul1%mo1);
		return make_pair((hash_[0][r+1]+(mo0-hash_[0][l]*pmo[0][r+1-l]%mo0))%mo0,
			             (hash_[1][r+1]+(mo1-hash_[1][l]*pmo[1][r+1-l]%mo1))%mo1);
	}
};
vector<ll> RollingHash::pmo[2]; ll RollingHash::mul0,RollingHash::mul1;

ll mo=1000000007;
int N,K;
vector<int> E[101010];
int inloop[101010];
SCC_BI bi;
vector<int> L;
ll H[101010],D[101010];
int phi[101010];

ll modpow(ll a, ll n) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

ll comb(int P_,int Q_) {
	if(P_<0 || Q_<0 || Q_>P_) return 0;
	ll ret=1;
	Q_=min(Q_,P_-Q_);
	for(int i=1;i<=Q_;i++) ret=ret*(P_+1-i)%mo*modpow(i,mo-2)%mo;
	return ret;
}
ll hcomb(int P_,int Q_) { return (P_==0&&Q_==0)?1:comb(P_+Q_-1,Q_);};

void dfs2(int cur,int pre) {
	ll momo[2]={1000000007,1000000009};
	vector<ll> prim = {100291,100297,100313,100333,100343,100357,100361,100363,100379,100391};
	vector<pair<ll,ll>> V;
	
	FORR(r,E[cur]) if(r!=pre) {
		dfs2(r,cur);
		V.push_back({H[r],D[r]});
	}
	sort(ALL(V));
	
	int id=0,id2=1;
	ll a=1,b=1;
	FORR(r,V) {
		ll h=r.first>>32, l=r.first-(h<<32);
		(a+=h*prim[(id++)%prim.size()])%=momo[0];
		(b+=l*prim[(id2++)%prim.size()])%=momo[1];
	}
	H[cur]=(a<<32)+b;
	
	D[cur]=K;
	for(int i=0,num=1;i<V.size();i+=num+1) {
		num=1;
		while(i+num<V.size() && V[i+num-1]==V[i+num]) num++;
		D[cur] = D[cur] * hcomb(V[i].second,num) % mo;
	}
	
	return;
}

void dfs(int cur) {
	inloop[cur]=2;
	L.push_back(cur);
	int i;
	FOR(i,E[cur].size()) if(inloop[E[cur][i]]) {
		if(inloop[E[cur][i]]==1) dfs(E[cur][i]);
		E[cur].erase(E[cur].begin()+i);
		i--;
	}
	dfs2(cur,-1);
}



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>K;
	bi.init(N);
	FOR(i,N) {
		cin>>x;
		E[i].push_back(x-1);
		E[x-1].push_back(i);
		bi.add_edge(i,x-1);
	}
	bi.scc();
	FOR(i,bi.SC.size()) FORR(r,bi.SC[i]) inloop[r]=bi.SC[i].size()>1;
	FOR(i,N) if(inloop[i]) {
		dfs(i);
		break;
	}
	int maxper=1;
	for(i=2;i<=L.size();i++) if(L.size()%i==0) {
		int ng=0;
		FOR(j,L.size()-i) if(H[j]!=H[j+i] || D[j]!=D[j+i]) ng=1;
		if(ng==0) maxper=max(maxper,L.size()/i);
	}
	for(i=1;i<=L.size();i++) phi[__gcd(i,maxper)]++;
	
	// symmetric;
	vector<int> nor,rev;
	FOR(i,2*L.size()) nor.push_back(H[i%L.size()]),rev.push_back(H[L.size()-1-i%L.size()]);
	RollingHash rhn,rhr;
	rhn.init(nor);
	rhr.init(rev);
	
	int found=-1;
	FOR(i,L.size()) {
		if(L.size()%2==0 && H[i]!=H[i+L.size()/2]) continue;
		if(rhn.hash(i,i+L.size()/2)==rhr.hash(L.size()-1+i,L.size()-1+i+L.size()/2)) {
			found=i;
			break;
		}
	}
	/*
	_P("%d\n",found);
	FOR(i,L.size()) _P("%d : %lld %lld\n",i,H[i],D[i]);
	*/
	ll ret=0;
	if(found==-1) {
		for(i=1;i<=L.size();i++) if(phi[i]) {
			ll t=phi[i];
			FOR(j,i) t=t*D[j]%mo;
			ret += t;
		}
		ret = ret%mo*modpow(L.size(),mo-2)%mo;
		
	}
	else {
		assert(0);
	}
	cout<<ret<<endl;
	
	

}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}


Compiler output:
a.cc: In function 'void solve()':
a.cc:183:37: error: no matching function for call to 'max(int&, std::vector<int>::size_type)'
  183 |                 if(ng==0) maxper=max(maxper,L.size()/i);
      |                                  ~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:183:37: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'std::vector<int>::size_type' {aka 'long unsigned int'})
  183 |                 if(ng==0) maxper=max(maxper,L.size()/i);
      |                                  ~~~^~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:183:37: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  183 |                 if(ng==0) maxper=max(maxper,L.size()/i);
      |                                  ~~~^~~~~~~~~~~~~~~~~~~