1 solutions

  • 0
    @ 2026-7-28 15:22:12
    /*
     *                                                     __----~~~~~~~~~~~------___
     *                                    .  .   ~~//====......          __--~ ~~
     *                    -.            \_|//     |||\\  ~~~~~~::::... /~
     *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
     *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
     *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
     *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
     *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
     *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
     *           '         ~-|      /|    |-~\~~       __--~~
     *                       |-~~-_/ |    |   ~\_   _-~            /\
     *                            /  \     \__   \/~                \__
     *                        _--~ _/ | .-~~____--~-/                  ~~==.
     *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
     *                                  -_     ~\      ~~---l__i__i__i--~~_/
     *                                  _-~-__   ~)  \--______________--~~
     *                                //.-~~~-~_--~- |-------~~~~~~~~
     *                                       //.-~~~--\
     *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     *
     *                               神兽保佑            永无BUG
     */
    
    /*
     * @Description: I want to be the weakest vegetable in the world!
     * @Author: I.Z.
     */
    #include<bits/stdc++.h>
    using namespace std;
    #define MOD         998244353
    #define speMOD      2933256077ll
    #define int         long long
    #define pii         pair<int,int>
    #define all(v)      v.begin(),v.end()
    #define pb          push_back
    #define REP(i,b,e)  for(int i=(b);i<(int)(e);++i)
    #define over(x)     {cout<<x<<endl;return;}
    #define lowbit(x)   ((x)&(-(x)))
    #define cntbit(x)   __builtin_popcount(x)
    #define rf(v)       shuffle(all(v),sd);
    mt19937 sd(std::random_device{}());
    int qpow (int a,int b,int m=MOD,int res=1){
    	a%=m;
    	while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
    	return res;
    }
    string YN(bool x,string Y="Yes",string N="No") {
    	if(x){
    		return Y;
    	} else {
    		return N;
    	}
    }
    int fac[2000005],inv[2000005];
    void init(int n){
    	fac[0]=inv[0]=1;
    	REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
    	inv[n]=qpow(fac[n],MOD-2);
    	for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
    }
    int binom(int x,int y){
    	return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
    }
    int n,ans;
    int p[2000005],fa[2000005],c[2000005],t[2000005];
    int findfa(int x){return fa[x]==x? x:fa[x]=findfa(fa[x]);}
    int calc(int x){return x==1? 1:qpow(x,x-2);}
    void Main() {
    	cin>>n;
    	init(n);
    	REP(i,0,n)cin>>p[i],--p[i],fa[i]=i;
    	REP(i,0,n)fa[findfa(i)]=findfa(p[i]);
    	REP(i,0,n)++t[findfa(i)];
    	REP(i,0,n)if(i==fa[i])++c[t[i]];
    	REP(i,0,n+1)t[i]=p[i]=fa[i]=0;
    	REP(i,1,n+1)if(c[i]){
    		for(int j=i*2;j<=n;j+=i)(t[j]+=c[i]*i)%=MOD;
    	}
    	int ans=1;
    	for(int i=n;i>=1;--i)if(c[i]){
    		if(!t[i]){
    			(ans*=calc(c[i])*qpow(i,c[i]-1)%MOD)%=MOD;
    			break;
    		}
    		if(c[i]==1){
    			(ans*=t[i])%=MOD;
    			continue;
    		}
    		int d=0;
    		p[0]=fa[0]=1;
    		REP(j,1,c[i]+1)p[j]=p[j-1]*t[i]%MOD;
    		REP(j,1,c[i]+1)fa[j]=fa[j-1]*(i*c[i]%MOD)%MOD;
    		REP(j,1,c[i]+1)(d+=binom(c[i]-1,j-1)*p[j]%MOD*fa[c[i]-j])%=MOD;
    		(ans*=d)%=MOD;
    	}
    	if(!c[1]&&!c[2])over(0)
    	if(!c[1])(ans*=c[2])%=MOD;
    	cout<<ans<<endl;
    }
    void TC() {
    	int tc=1;
    	while(tc--){
    		Main();
    		cout.flush();
    	}
    }
    signed main() {
    	freopen("omphalos.in","r",stdin);
    	freopen("omphalos.out","w",stdout);
    	return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
    }
    /*
    1. CLEAR the arrays (ESPECIALLY multitests)
    2. DELETE useless output
     */
    
    • 1

    Information

    ID
    2913
    Time
    1000ms
    Memory
    512MiB
    Difficulty
    10
    Tags
    (None)
    # Submissions
    2
    Accepted
    2
    Uploaded By