QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#288850#7864. Random Tree Parkingucup-team087#AC ✓121ms48492kbC++2025.3kb2023-12-23 13:52:542024-11-20 09:57:08

Judging History

你现在查看的是最新测评结果

  • [2024-11-20 09:57:08]
  • 管理员手动重测本题所有得分≥97分的提交记录
  • 测评结果:AC
  • 用时:121ms
  • 内存:48492kb
  • [2024-11-20 07:56:39]
  • 自动重测本题所有获得100分的提交记录
  • 测评结果:97
  • 用时:120ms
  • 内存:48472kb
  • [2024-11-20 07:55:31]
  • hack成功,自动添加数据
  • (/hack/1204)
  • [2023-12-23 13:52:54]
  • 评测
  • 测评结果:100
  • 用时:105ms
  • 内存:48612kb
  • [2023-12-23 13:52:54]
  • 提交

answer

#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif

#include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define pb push_back
#define eb emplace_back
#define a first
#define b second
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif

template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.a<<","<<p.b<<"}";
}

template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}

#define mp make_pair
#define mt make_tuple
#define one(x) memset(x,-1,sizeof(x))
#define zero(x) memset(x,0,sizeof(x))
#ifdef LOCAL
void dmpr(ostream&os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
	os<<t<<" ";
	dmpr(os,args...);
}
#define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
#else
#define dmp2(...) void(0)
#endif

using uint=unsigned;
using ull=unsigned long long;

template<class t,size_t n>
ostream& operator<<(ostream&os,const array<t,n>&a){
	return os<<vc<t>(all(a));
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"{";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<"}";
}

ll read(){
	ll i;
	cin>>i;
	return i;
}

vi readvi(int n,int off=0){
	vi v(n);
	rep(i,n)v[i]=read()+off;
	return v;
}

pi readpi(int off=0){
	int a,b;cin>>a>>b;
	return pi(a+off,b+off);
}

template<class t>
void print_single(t x,int suc=1){
	cout<<x;
	if(suc==1)
		cout<<"\n";
	if(suc==2)
		cout<<" ";
}

template<class t,class u>
void print_single(const pair<t,u>&p,int suc=1){
	print_single(p.a,2);
	print_single(p.b,suc);
}

template<class T>
void print_single(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print_single(v[i],i==int(v.size())-1?suc:2);
}

template<class T>
void print_offset(const vector<T>&v,ll off,int suc=1){
	rep(i,v.size())
		print_single(v[i]+off,i==int(v.size())-1?suc:2);
}

template<class T,size_t N>
void print_single(const array<T,N>&v,int suc=1){
	rep(i,N)
		print_single(v[i],i==int(N)-1?suc:2);
}

template<class T>
void print(const T&t){
	print_single(t);
}

template<class T,class ...Args>
void print(const T&t,const Args&...args){
	print_single(t,2);
	print(args...);
}

string readString(){
	string s;
	cin>>s;
	return s;
}

template<class T>
T sq(const T& t){
	return t*t;
}

void YES(bool ex=true){
	cout<<"YES\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void NO(bool ex=true){
	cout<<"NO\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void Yes(bool ex=true){
	cout<<"Yes\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void No(bool ex=true){
	cout<<"No\n";
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
//#define CAPITAL
/*
void yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<"\n";
	#else
	cout<<"Yes"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void no(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<"\n";
	#else
	cout<<"No"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}*/
void possible(bool ex=true){
	#ifdef CAPITAL
	cout<<"POSSIBLE"<<"\n";
	#else
	cout<<"Possible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}
void impossible(bool ex=true){
	#ifdef CAPITAL
	cout<<"IMPOSSIBLE"<<"\n";
	#else
	cout<<"Impossible"<<"\n";
	#endif
	if(ex)exit(0);
	#ifdef LOCAL
	cout.flush();
	#endif
}

constexpr ll ten(int n){
	return n==0?1:ten(n-1)*10;
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

int topbit(signed t){
	return t==0?-1:31-__builtin_clz(t);
}
int topbit(ll t){
	return t==0?-1:63-__builtin_clzll(t);
}
int topbit(ull t){
	return t==0?-1:63-__builtin_clzll(t);
}
int botbit(signed a){
	return a==0?32:__builtin_ctz(a);
}
int botbit(ll a){
	return a==0?64:__builtin_ctzll(a);
}
int botbit(ull a){
	return a==0?64:__builtin_ctzll(a);
}
int popcount(signed t){
	return __builtin_popcount(t);
}
int popcount(ll t){
	return __builtin_popcountll(t);
}
int popcount(ull t){
	return __builtin_popcountll(t);
}
int bitparity(ll t){
	return __builtin_parityll(t);
}
bool ispow2(int i){
	return i&&(i&-i)==i;
}
ll mask(int i){
	return (ll(1)<<i)-1;
}
ull umask(int i){
	return (ull(1)<<i)-1;
}
ll minp2(ll n){
	if(n<=1)return 1;
	else return ll(1)<<(topbit(n-1)+1);
}

bool inc(int a,int b,int c){
	return a<=b&&b<=c;
}

template<class t> void mkuni(vc<t>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.ed);
}

ll rand_int(ll l, ll r) { //[l, r]
	//#ifdef LOCAL
	static mt19937_64 gen;
	/*#else
	static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
	#endif*/
	return uniform_int_distribution<ll>(l, r)(gen);
}

ll rand_int(ll k){ //[0,k)
	return rand_int(0,k-1);
}

template<class t>
void myshuffle(vc<t>&a){
	rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
}

template<class t,class u>
int lwb(const vc<t>&v,const u&a){
	return lower_bound(all(v),a)-v.bg;
}
template<class t,class u>
bool bis(const vc<t>&v,const u&a){
	return binary_search(all(v),a);
}

vvc<int> readGraph(int n,int m){
	vvc<int> g(n);
	rep(i,m){
		int a,b;
		cin>>a>>b;
		//sc.read(a,b);
		a--;b--;
		g[a].pb(b);
		g[b].pb(a);
	}
	return g;
}

vvc<int> readTree(int n){
	return readGraph(n,n-1);
}

template<class t>
vc<t> presum(const vc<t>&a){
	vc<t> s(si(a)+1);
	rep(i,si(a))s[i+1]=s[i]+a[i];
	return s;
}
vc<ll> presum(const vi&a){
	vc<ll> s(si(a)+1);
	rep(i,si(a))s[i+1]=s[i]+a[i];
	return s;
}
//BIT で数列を管理するときに使う (CF850C)
template<class t>
vc<t> predif(vc<t> a){
	gnr(i,1,si(a))a[i]-=a[i-1];
	return a;
}
template<class t>
vvc<ll> imos(const vvc<t>&a){
	int n=si(a),m=si(a[0]);
	vvc<ll> b(n+1,vc<ll>(m+1));
	rep(i,n)rep(j,m)
		b[i+1][j+1]=b[i+1][j]+b[i][j+1]-b[i][j]+a[i][j];
	return b;
}

//verify してないや
void transvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void transvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[j][i]=a[i][j];
	}
	a.swap(b);
	transvvc(n,m,args...);
}
//CF854E
void rotvvc(int&n,int&m){
	swap(n,m);
}
template<class t,class... Args>
void rotvvc(int&n,int&m,vvc<t>&a,Args&...args){
	assert(si(a)==n);
	vvc<t> b(m,vi(n));
	rep(i,n){
		assert(si(a[i])==m);
		rep(j,m)b[m-1-j][i]=a[i][j];
	}
	a.swap(b);
	rotvvc(n,m,args...);
}

//ソートして i 番目が idx[i]
//CF850C
template<class t>
vi sortidx(const vc<t>&a){
	int n=si(a);
	vi idx(n);iota(all(idx),0);
	sort(all(idx),[&](int i,int j){return a[i]<a[j];});
	return idx;
}
//vs[i]=a[idx[i]]
//例えば sortidx で得た idx を使えば単にソート列になって返ってくる
//CF850C
template<class t>
vc<t> a_idx(const vc<t>&a,const vi&idx){
	int n=si(a);
	assert(si(idx)==n);
	vc<t> vs(n);
	rep(i,n)vs[i]=a[idx[i]];
	return vs;
}
//CF850C
vi invperm(const vi&p){
	int n=si(p);
	vi q(n);
	rep(i,n)q[p[i]]=i;
	return q;
}

template<class t,class s=t>
s SUM(const vc<t>&a){
	return accumulate(all(a),s(0));
}
template<class t,size_t K,class s=t>
s SUM(const array<t,K>&a){
	return accumulate(all(a),s(0));
}

template<class t>
t MAX(const vc<t>&a){
	return *max_element(all(a));
}

template<class t>
pair<t,int> MAXi(const vc<t>&a){
	auto itr=max_element(all(a));
	return mp(*itr,itr-a.bg);
}

template<class A>
auto MIN(const A&a){
	return *min_element(all(a));
}

template<class t>
pair<t,int> MINi(const vc<t>&a){
	auto itr=min_element(all(a));
	return mp(*itr,itr-a.bg);
}

vi vid(int n){
	vi res(n);iota(all(res),0);
	return res;
}

template<class S>
void soin(S&s){
	sort(all(s));
}

template<class S,class F>
void soin(S&s,F&&f){
	sort(all(s),forward<F>(f));
}

template<class S>
S soout(S s){
	soin(s);
	return s;
}

template<class S>
void rein(S&s){
	reverse(all(s));
}

template<class S>
S reout(S s){
	rein(s);
	return s;
}

template<class t,class u>
pair<t,u>&operator+=(pair<t,u>&a,pair<t,u> b){
	a.a+=b.a;a.b+=b.b;return a;}
template<class t,class u>
pair<t,u>&operator-=(pair<t,u>&a,pair<t,u> b){
	a.a-=b.a;a.b-=b.b;return a;}
template<class t,class u>
pair<t,u> operator+(pair<t,u> a,pair<t,u> b){return mp(a.a+b.a,a.b+b.b);}
template<class t,class u>
pair<t,u> operator-(pair<t,u> a,pair<t,u> b){return mp(a.a-b.a,a.b-b.b);}

template<class t>
t gpp(vc<t>&vs){
	assert(si(vs));
	t res=move(vs.back());
	vs.pop_back();
	return res;
}

template<class t,class u>
void pb(vc<t>&a,const vc<u>&b){
	a.insert(a.ed,all(b));
}

template<class t,class...Args>
vc<t> cat(vc<t> a,Args&&...b){
	(pb(a,forward<Args>(b)),...);
	return a;
}

template<class t,class u>
vc<t>& operator+=(vc<t>&a,u x){
	for(auto&v:a)v+=x;
	return a;
}

template<class t,class u>
vc<t> operator+(vc<t> a,u x){
	return a+=x;
}

template<class t>
vc<t> operator+(const vc<t>&a,const vc<t>&b){
	vc<t> c(max(si(a),si(b)));
	rep(i,si(a))c[i]+=a[i];
	rep(i,si(b))c[i]+=b[i];
	return c;
}

template<class t,class u>
vc<t>& operator-=(vc<t>&a,u x){
	for(auto&v:a)v-=x;
	return a;
}

template<class t,class u>
vc<t>& operator-(vc<t> a,u x){
	return a-=x;
}

template<class t,class u>
void remval(vc<t>&a,const u&v){
	a.erase(remove(all(a),v),a.ed);
}

template<class VS,class u>
void fila(VS&vs,const u&a){
	fill(all(vs),a);
}

template<class t,class u>
int findid(const vc<t>&vs,const u&a){
	auto itr=find(all(vs),a);
	if(itr==vs.ed)return -1;
	else return itr-vs.bg;
}

template<class t>
void rtt(vc<t>&vs,int i){
	rotate(vs.bg,vs.bg+i,vs.ed);
}

//mint107 は verify してねえ
//#define DYNAMIC_MOD

struct modinfo{uint mod,root;
#ifdef DYNAMIC_MOD
constexpr modinfo(uint m,uint r):mod(m),root(r),im(0){set_mod(m);}
ull im;
constexpr void set_mod(uint m){
	mod=m;
	im=ull(-1)/m+1;
}
uint product(uint a,uint b)const{
	ull z=ull(a)*b;
	uint x=((unsigned __int128)z*im)>>64;
	uint v=uint(z)-x*mod;
	return v<mod?v:v+mod;
}
#endif
};
template<modinfo const&ref>
struct modular{
	static constexpr uint const &mod=ref.mod;
	static modular root(){return modular(ref.root);}
	uint v;
	//modular(initializer_list<uint>ls):v(*ls.bg){}
	modular(ll vv=0){s(vv%mod+mod);}
	modular& s(uint vv){
		v=vv<mod?vv:vv-mod;
		return *this;
	}
	modular operator-()const{return modular()-*this;}
	modular& operator+=(const modular&rhs){return s(v+rhs.v);}
	modular&operator-=(const modular&rhs){return s(v+mod-rhs.v);}
	modular&operator*=(const modular&rhs){
		#ifndef DYNAMIC_MOD
		v=ull(v)*rhs.v%mod;
		#else
		v=ref.product(v,rhs.v);
		#endif
		return *this;
	}
	modular&operator/=(const modular&rhs){return *this*=rhs.inv();}
	modular operator+(const modular&rhs)const{return modular(*this)+=rhs;}
	modular operator-(const modular&rhs)const{return modular(*this)-=rhs;}
	modular operator*(const modular&rhs)const{return modular(*this)*=rhs;}
	modular operator/(const modular&rhs)const{return modular(*this)/=rhs;}
	modular pow(ll n)const{
		if(n<0)return inv().pow(-n);
		modular res(1),x(*this);
		while(n){
			if(n&1)res*=x;
			x*=x;
			n>>=1;
		}
		return res;
	}
	modular inv()const{return pow(mod-2);}
	/*modular inv()const{
		int x,y;
		int g=extgcd<ll>(v,mod,x,y);
		assert(g==1);
		if(x<0)x+=mod;
		return modular(x);
	}*/
	friend modular operator+(ll x,const modular&y){
		return modular(x)+y;
	}
	friend modular operator-(ll x,const modular&y){
		return modular(x)-y;
	}
	friend modular operator*(ll x,const modular&y){
		return modular(x)*y;
	}
	friend modular operator/(ll x,const modular&y){
		return modular(x)/y;
	}
	friend ostream& operator<<(ostream&os,const modular&m){
		return os<<m.v;
	}
	friend istream& operator>>(istream&is,modular&m){
		ll x;is>>x;
		m=modular(x);
		return is;
	}
	bool operator<(const modular&r)const{return v<r.v;}
	bool operator==(const modular&r)const{return v==r.v;}
	bool operator!=(const modular&r)const{return v!=r.v;}
	explicit operator bool()const{
		return v;
	}
};

#define USE_GOOD_MOD

//size of input must be a power of 2
//output of forward fmt is bit-reversed
//output elements are in the range [0,mod*4)
//input of inverse fmt should be bit-reversed
template<class mint>
void inplace_fmt(const int n,mint*const f,bool inv){
	static constexpr uint mod=mint::mod;
	static constexpr uint mod2=mod*2;
	static constexpr int L=30;
	static mint g[L],ig[L],p2[L];
	if(g[0].v==0){
		rep(i,L){
			mint w=-mint::root().pow(((mod-1)>>(i+2))*3);
			g[i]=w;
			ig[i]=w.inv();
			p2[i]=mint(1<<i).inv();
		}
	}
	if(!inv){
		int b=n;
		if(b>>=1){//input:[0,mod)
			rep(i,b){
				uint x=f[i+b].v;
				f[i+b].v=f[i].v+mod-x;
				f[i].v+=x;
			}
		}
		if(b>>=1){//input:[0,mod*2)
			mint p=1;
			for(int i=0,k=0;i<n;i+=b*2){
				rng(j,i,i+b){
					uint x=(f[j+b]*p).v;
					f[j+b].v=f[j].v+mod-x;
					f[j].v+=x;
				}
				p*=g[__builtin_ctz(++k)];
			}
		}
		while(b){
			if(b>>=1){//input:[0,mod*3)
				mint p=1;
				for(int i=0,k=0;i<n;i+=b*2){
					rng(j,i,i+b){
						uint x=(f[j+b]*p).v;
						f[j+b].v=f[j].v+mod-x;
						f[j].v+=x;
					}
					p*=g[__builtin_ctz(++k)];
				}
			}
			if(b>>=1){//input:[0,mod*4)
				mint p=1;
				for(int i=0,k=0;i<n;i+=b*2){
					rng(j,i,i+b){
						uint x=(f[j+b]*p).v;
						f[j].v=(f[j].v<mod2?f[j].v:f[j].v-mod2);
						f[j+b].v=f[j].v+mod-x;
						f[j].v+=x;
					}
					p*=g[__builtin_ctz(++k)];
				}
			}
		}
	}else{
		int b=1;
		if(b<n/2){//input:[0,mod)
			mint p=1;
			for(int i=0,k=0;i<n;i+=b*2){
				rng(j,i,i+b){
					ull x=f[j].v+mod-f[j+b].v;
					f[j].v+=f[j+b].v;
					f[j+b].v=x*p.v%mod;
				}
				p*=ig[__builtin_ctz(++k)];
			}
			b<<=1;
		}
		for(;b<n/2;b<<=1){
			mint p=1;
			for(int i=0,k=0;i<n;i+=b*2){
				rng(j,i,i+b/2){//input:[0,mod*2)
					ull x=f[j].v+mod2-f[j+b].v;
					f[j].v+=f[j+b].v;
					f[j].v=(f[j].v)<mod2?f[j].v:f[j].v-mod2;
					f[j+b].v=x*p.v%mod;
				}
				rng(j,i+b/2,i+b){//input:[0,mod)
					ull x=f[j].v+mod-f[j+b].v;
					f[j].v+=f[j+b].v;
					f[j+b].v=x*p.v%mod;
				}
				p*=ig[__builtin_ctz(++k)];
			}
		}
		if(b<n){//input:[0,mod*2)
			rep(i,b){
				uint x=f[i+b].v;
				f[i+b].v=f[i].v+mod2-x;
				f[i].v+=x;
			}
		}
		mint z=p2[__lg(n)];
		rep(i,n)f[i]*=z;
	}
}

template<class mint>
void inplace_fmt(vector<mint>&f,bool inv){
	inplace_fmt(si(f),f.data(),inv);
}

//size of input must be a power of 2
//output elements are in the range [0,mod*4)
template<class mint>
void half_fmt(const int n,mint*const f){
	static constexpr uint mod=mint::mod;
	static constexpr uint mod2=mod*2;
	static const int L=30;
	static mint g[L],h[L];
	if(g[0].v==0){
		rep(i,L){
			g[i]=-mint::root().pow(((mod-1)>>(i+2))*3);
			h[i]=mint::root().pow((mod-1)>>(i+2));
		}
	}
	int b=n;
	int lv=0;
	if(b>>=1){//input:[0,mod)
		mint p=h[lv++];
		for(int i=0,k=0;i<n;i+=b*2){
			rng(j,i,i+b){
				uint x=(f[j+b]*p).v;
				f[j+b].v=f[j].v+mod-x;
				f[j].v+=x;
			}
			p*=g[__builtin_ctz(++k)];
		}
	}
	if(b>>=1){//input:[0,mod*2)
		mint p=h[lv++];
		for(int i=0,k=0;i<n;i+=b*2){
			rng(j,i,i+b){
				uint x=(f[j+b]*p).v;
				f[j+b].v=f[j].v+mod-x;
				f[j].v+=x;
			}
			p*=g[__builtin_ctz(++k)];
		}
	}
	while(b){
		if(b>>=1){//input:[0,mod*3)
			mint p=h[lv++];
			for(int i=0,k=0;i<n;i+=b*2){
				rng(j,i,i+b){
					uint x=(f[j+b]*p).v;
					f[j+b].v=f[j].v+mod-x;
					f[j].v+=x;
				}
				p*=g[__builtin_ctz(++k)];
			}
		}
		if(b>>=1){//input:[0,mod*4)
			mint p=h[lv++];
			for(int i=0,k=0;i<n;i+=b*2){
				rng(j,i,i+b){
					uint x=(f[j+b]*p).v;
					f[j].v=(f[j].v<mod2?f[j].v:f[j].v-mod2);
					f[j+b].v=f[j].v+mod-x;
					f[j].v+=x;
				}
				p*=g[__builtin_ctz(++k)];
			}
		}
	}
}

template<class mint>
void half_fmt(vector<mint>&f){
	half_fmt(si(f),f.data());
}

#ifdef USE_GOOD_MOD

template<class mint>
vc<mint> multiply(vc<mint> x,const vc<mint>&y,bool same=false){
	int n=si(x)+si(y)-1;
	int s=1;
	while(s<n)s*=2;
	x.resize(s);inplace_fmt(x,false);
	if(!same){
		static vc<mint> z;
		z.clear();z.resize(s);
		rep(i,si(y))z[i]=y[i];
		inplace_fmt(z,false);
		rep(i,s)x[i]*=z[i];
	}else{
		rep(i,s)x[i]*=x[i];
	}
	inplace_fmt(x,true);x.resize(n);
	return x;
}
template<class mint>
vc<mint> multiply_givenlength(vc<mint> x,const vc<mint>&y,bool same=false){
	int s=si(x);
	assert(ispow2(s));
	assert(si(y));
	x.resize(s);inplace_fmt(x,false);
	if(!same){
		static vc<mint> z;
		z.clear();z.resize(s);
		rep(i,si(y))z[i]=y[i];
		inplace_fmt(z,false);
		rep(i,s)x[i]*=z[i];
	}else{
		rep(i,s)x[i]*=x[i];
	}
	inplace_fmt(x,true);
	return x;
}

#else

//59501818244292734739283969-1=5.95*10^25 までの値を正しく計算
//最終的な列の大きさが 2^24 までなら動く
//最終的な列の大きさが 2^20 以下のときは,下の 3 つの素数を使ったほうが速い(は?)
//VERIFY: yosupo
//Yukicoder No980 (same=true)
namespace arbitrary_convolution{
	constexpr modinfo base0{167772161,3};//2^25 * 5 + 1
	constexpr modinfo base1{469762049,3};//2^26 * 7 + 1
	constexpr modinfo base2{754974721,11};//2^24 * 45 + 1
	//extern constexpr modinfo base0{1045430273,3};//2^20 * 997 + 1
	//extern constexpr modinfo base1{1051721729,6};//2^20 * 1003 + 1
	//extern constexpr modinfo base2{1053818881,7};//2^20 * 1005 + 1
	using mint0=modular<base0>;
	using mint1=modular<base1>;
	using mint2=modular<base2>;
	template<class t,class mint>
	vc<t> sub(const vc<mint>&x,const vc<mint>&y,bool same=false){
		int n=si(x)+si(y)-1;
		int s=1;
		while(s<n)s*=2;
		vc<t> z(s);rep(i,si(x))z[i]=x[i].v;
		inplace_fmt(z,false);
		if(!same){
			vc<t> w(s);rep(i,si(y))w[i]=y[i].v;
			inplace_fmt(w,false);
			rep(i,s)z[i]*=w[i];
		}else{
			rep(i,s)z[i]*=z[i];
		}
		inplace_fmt(z,true);z.resize(n);
		return z;
	}
	template<class mint>
	vc<mint> multiply(const vc<mint>&x,const vc<mint>&y,bool same=false){
		auto d0=sub<mint0>(x,y,same);
		auto d1=sub<mint1>(x,y,same);
		auto d2=sub<mint2>(x,y,same);
		int n=si(d0);
		vc<mint> res(n);
		static const mint1 r01=mint1(mint0::mod).inv();
		static const mint2 r02=mint2(mint0::mod).inv();
		static const mint2 r12=mint2(mint1::mod).inv();
		static const mint2 r02r12=r02*r12;
		static const mint w1=mint(mint0::mod);
		static const mint w2=w1*mint(mint1::mod);
		rep(i,n){
			ull a=d0[i].v;
			ull b=(d1[i].v+mint1::mod-a)*r01.v%mint1::mod;
			ull c=((d2[i].v+mint2::mod-a)*r02r12.v+(mint2::mod-b)*r12.v)%mint2::mod;
			res[i].v=(a+b*w1.v+c*w2.v)%mint::mod;
		}
		return res;
	}
	template<class t,class mint>
	vc<t>&sub_givenlength(const vc<mint>&x,const vc<mint>&y,bool same=false){
		int s=si(x);
		assert(ispow2(s));
		assert(si(y)==s);
		static vc<t> z;
		z.clear();z.resize(s);
		rep(i,si(x))z[i]=x[i].v;
		inplace_fmt(z,false);
		if(!same){
			static vc<t> w;
			w.clear();w.resize(s);
			rep(i,si(y))w[i]=y[i].v;
			inplace_fmt(w,false);
			rep(i,s)z[i]*=w[i];
		}else{
			rep(i,s)z[i]*=z[i];
		}
		inplace_fmt(z,true);
		return z;
	}
	template<class mint>
	vc<mint> multiply_givenlength(vc<mint> x,const vc<mint>&y,bool same=false){
		auto&d0=sub_givenlength<mint0>(x,y,same);
		auto&d1=sub_givenlength<mint1>(x,y,same);
		auto&d2=sub_givenlength<mint2>(x,y,same);
		int n=si(d0);
		x.resize(n);
		static const mint1 r01=mint1(mint0::mod).inv();
		static const mint2 r02=mint2(mint0::mod).inv();
		static const mint2 r12=mint2(mint1::mod).inv();
		static const mint2 r02r12=r02*r12;
		static const mint w1=mint(mint0::mod);
		static const mint w2=w1*mint(mint1::mod);
		rep(i,n){
			ull a=d0[i].v;
			ull b=(d1[i].v+mint1::mod-a)*r01.v%mint1::mod;
			ull c=((d2[i].v+mint2::mod-a)*r02r12.v+(mint2::mod-b)*r12.v)%mint2::mod;
			x[i].v=(a+b*w1.v+c*w2.v)%mint::mod;
		}
		return x;
	}
}
using arbitrary_convolution::multiply;
using arbitrary_convolution::multiply_givenlength;

#endif

//UTPC2021 C
namespace integer_convolution{
	extern constexpr modinfo base0{1045430273,3};//2^20 * 997 + 1
	extern constexpr modinfo base1{1051721729,6};//2^20 * 1003 + 1
	//extern constexpr modinfo base0{469762049,3};//2^26 * 7 + 1
	//extern constexpr modinfo base1{754974721,11};//2^24 * 45 + 1
	using mint0=modular<base0>;
	using mint1=modular<base1>;
	template<class t>
	vc<t> sub(const vi&x,const vi&y,bool same=false){
		int n=si(x)+si(y)-1;
		int s=1;
		while(s<n)s*=2;
		vc<t> z(s);rep(i,si(x))z[i]=x[i];
		inplace_fmt(z,false);
		if(!same){
			vc<t> w(s);rep(i,si(y))w[i]=y[i];
			inplace_fmt(w,false);
			rep(i,s)z[i]*=w[i];
		}else{
			rep(i,s)z[i]*=z[i];
		}
		inplace_fmt(z,true);z.resize(n);
		return z;
	}
	vi multiply(const vi&x,const vi&y,bool same=false){
		auto d0=sub<mint0>(x,y,same);
		auto d1=sub<mint1>(x,y,same);
		const mint1 r=mint1(mint0::mod).inv();
		const ll v=ll(mint0::mod)*mint1::mod;
		int n=si(d0);
		vi res(n);
		rep(i,n){
			res[i]=d0[i].v+(r*(d1[i]-d0[i].v)).v*(ull)mint0::mod;
			if(res[i]>v/2)res[i]-=v;
		}
		return res;
	}
}

//最大で 1<<mx のサイズの fft が登場!
template<class mint>
vc<mint> large_convolution(const vc<mint>&a,const vc<mint>&b,int mx){
	int n=si(a),m=si(b);
	vc<mint> c(n+m-1);
	int len=1<<(mx-1);
	for(int i=0;i<n;i+=len){
		for(int j=0;j<n;j+=len){
			int x=min(len,n-i),y=min(len,m-j);
			auto d=multiply(vc<mint>(a.bg+i,a.bg+i+x),vc<mint>(b.bg+j,b.bg+j+y));
			rep(k,si(d))
				c[i+j+k]+=d[k];
		}
	}
	return c;
}

//input A: N 次,B ?,M
//output D: M 次多項式
//C を M 次多項式として
//[x^N] A*B*C = [x^M] D*C
//となるような D を返す
//CF796F
template<class mint>
vc<mint> transpose_advance(const vc<mint>&a,const vc<mint>&b,int m){
	int n=si(a)-1;
	auto d=multiply(a,b);
	vc<mint> res(m+1);
	if(n>=m){
		rep(i,m+1)res[i]=d[i+n-m];
	}else{
		rng(i,m-n,m+1)res[i]=d[i+n-m];
	}
	return res;
}

//Yukicoder 2166
template<class mint>
void chmult(vc<mint>&x,const vc<mint>&y,int s){
	x=multiply(move(x),y);
	x.resize(s);
}

#ifndef DYNAMIC_MOD
extern constexpr modinfo base{998244353,3};
//extern constexpr modinfo base{1000000007,0};
//extern constexpr modinfo base{2147483579,0};//2^31 未満の最大の安全素数
//modinfo base{1,0};
#ifdef USE_GOOD_MOD
static_assert(base.mod==998244353);
#endif
#else
modinfo base(1,0);
extern constexpr modinfo base107(1000000007,0);
using mint107=modular<base107>;
#endif
using mint=modular<base>;

mint parity(int i){
	return i%2==0?1:-1;
}

#ifdef LOCAL
const int vmax=10010;
#else
const int vmax=(1<<21)+10;
#endif
mint fact[vmax],finv[vmax],invs[vmax];
void initfact(){
	fact[0]=1;
	rng(i,1,vmax){
		fact[i]=fact[i-1]*i;
	}
	finv[vmax-1]=fact[vmax-1].inv();
	for(int i=vmax-2;i>=0;i--){
		finv[i]=finv[i+1]*(i+1);
	}
	for(int i=vmax-1;i>=1;i--){
		invs[i]=finv[i]*fact[i-1];
	}
}
mint choose(int n,int k){
	return inc(0,k,n)?fact[n]*finv[n-k]*finv[k]:0;
}
mint binom(int a,int b){
	return 0<=a&&0<=b?fact[a+b]*finv[a]*finv[b]:0;
}
mint catalan(int n){
	return binom(n,n)-(n-1>=0?binom(n-1,n+1):0);
}
//対角線を超えず (x,y) に至る方法の数
mint catalan(int x,int y){
	assert(y<=x);
	return binom(x,y)-binom(x+1,y-1);
}
//y=x+c を超えず (x,y) に至る方法の数
mint catalan(int x,int y,int c){
	assert(y<=x+c);
	return binom(x,y)-binom(x+c+1,y-c-1);
}

/*
const int vmax=610;
mint fact[vmax+1],binbuf[vmax+1][vmax+1];
mint choose(int n,int k){
	return 0<=k&&k<=n?binbuf[n-k][k]:0;
}
mint binom(int a,int b){
	return 0<=a&&0<=b?binbuf[a][b]:0;
}
void initfact(int n){
	fact[0]=1;
	rep(i,n)fact[i+1]=fact[i]*(i+1);
	rep(i,n+1)rep(j,n+1){
		if(i==0&&j==0){
			binbuf[i][j]=1;
		}else{
			binbuf[i][j]=0;
			if(i)binbuf[i][j]+=binbuf[i-1][j];
			if(j)binbuf[i][j]+=binbuf[i][j-1];
		}
	}
}
*/

mint p2[vmax],p2inv[vmax];
void initp2(){
	p2[0]=1;
	rep(i,vmax-1)p2[i+1]=p2[i]*2;
	p2inv[vmax-1]=p2[vmax-1].inv();
	per(i,vmax-1)p2inv[i]=p2inv[i+1]*2;
}

bool dbg=false;

void slv(){
	int n;cin>>n;
	vvc<int> t(n);
	rng(i,1,n){
		int p;cin>>p;
		p--;
		t[p].pb(i);
	}
	auto dfs=[&](auto self,int v,int d)->vc<mint>{
		vc<mint> x(d+2);
		rep(i,d+2)x[i]=finv[i];
		
		for(auto c:t[v]){
			chmult(x,self(self,c,d+1),d+2);
		}
		
		x.erase(x.bg);
		return x;
	};
	auto res=dfs(dfs,0,0);
	print(res[0]*fact[n]);
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	
	initfact();
	if(dbg){
		while(1)slv();
	}else{
		//int t;cin>>t;rep(_,t)
		slv();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 29ms
memory: 44572kb

input:

3
1 1

output:

12

result:

ok 1 number(s): "12"

Test #2:

score: 0
Accepted
time: 22ms
memory: 44624kb

input:

3
1 2

output:

16

result:

ok 1 number(s): "16"

Test #3:

score: 0
Accepted
time: 25ms
memory: 44560kb

input:

4
1 2 3

output:

125

result:

ok 1 number(s): "125"

Test #4:

score: 0
Accepted
time: 28ms
memory: 44688kb

input:

8
1 2 3 1 3 4 3

output:

1198736

result:

ok 1 number(s): "1198736"

Test #5:

score: 0
Accepted
time: 28ms
memory: 44664kb

input:

15
1 2 2 2 2 3 3 2 7 7 3 10 3 13

output:

938578089

result:

ok 1 number(s): "938578089"

Test #6:

score: 0
Accepted
time: 28ms
memory: 44572kb

input:

100
1 1 1 3 5 5 5 5 9 9 3 2 11 14 9 8 16 8 18 18 20 10 12 2 22 21 27 28 29 6 2 21 2 20 21 11 16 19 9 25 39 8 14 19 6 38 22 19 25 13 3 27 19 51 23 18 45 30 30 22 24 16 12 61 42 24 3 3 53 40 59 72 6 23 1 64 41 13 71 75 30 64 11 55 70 60 32 84 25 4 69 49 15 42 72 31 71 23 58

output:

426063005

result:

ok 1 number(s): "426063005"

Test #7:

score: 0
Accepted
time: 33ms
memory: 44560kb

input:

500
1 1 3 3 3 4 3 5 2 5 8 4 12 11 8 14 1 12 7 16 7 7 17 10 8 26 7 4 13 21 6 7 20 34 35 24 25 23 25 39 20 30 13 43 43 35 45 34 7 4 11 23 11 43 35 27 6 2 3 11 37 42 27 37 62 42 41 43 63 4 57 17 18 8 11 23 72 74 41 49 76 44 50 81 46 18 45 5 8 88 77 27 35 11 52 18 32 85 57 25 32 22 39 35 43 26 63 7 62 2...

output:

105022837

result:

ok 1 number(s): "105022837"

Test #8:

score: 0
Accepted
time: 34ms
memory: 44652kb

input:

2000
1 1 2 4 4 4 1 5 9 4 5 9 9 4 15 1 11 18 11 2 4 22 10 23 18 15 6 25 25 19 15 28 32 17 29 24 35 11 32 20 25 8 7 12 27 29 40 21 23 47 24 8 6 24 53 43 9 10 48 18 16 16 10 45 42 33 20 27 33 47 41 22 37 4 38 23 8 29 14 54 49 74 60 56 45 32 11 4 58 16 71 29 49 32 31 95 38 2 89 73 91 65 26 12 94 35 1 73...

output:

510693456

result:

ok 1 number(s): "510693456"

Test #9:

score: 0
Accepted
time: 27ms
memory: 44824kb

input:

10000
1 2 1 1 2 1 4 3 5 6 1 8 8 3 2 15 4 14 10 9 9 15 17 5 21 9 11 24 17 20 17 16 4 13 10 10 36 2 8 29 34 40 8 13 27 5 1 18 16 4 40 47 4 8 9 1 54 40 38 41 46 52 31 21 21 14 49 49 46 22 14 59 71 37 30 18 37 30 36 56 24 56 48 17 75 68 68 6 65 87 48 52 8 26 94 89 29 32 40 77 51 6 9 78 1 48 100 69 85 89...

output:

158503783

result:

ok 1 number(s): "158503783"

Test #10:

score: 0
Accepted
time: 121ms
memory: 48324kb

input:

100000
1 1 1 2 4 4 7 8 6 9 7 8 12 10 15 15 9 12 9 16 9 13 11 18 11 8 6 23 22 28 8 29 12 24 14 9 33 5 17 4 33 29 41 19 37 34 19 41 15 21 20 13 36 25 34 38 2 56 33 53 40 36 26 28 34 7 19 66 35 43 52 47 53 32 61 11 55 10 78 75 43 80 71 16 20 68 27 41 80 33 69 50 71 7 5 26 24 78 62 17 76 15 10 11 56 64 ...

output:

937583571

result:

ok 1 number(s): "937583571"

Test #11:

score: 0
Accepted
time: 111ms
memory: 48272kb

input:

100000
1 2 1 2 5 3 5 4 6 8 2 1 6 2 5 5 1 6 12 12 15 11 23 3 4 13 3 22 8 5 13 12 10 9 6 27 37 22 14 24 12 26 15 30 2 27 43 4 47 9 42 5 33 26 13 54 17 32 23 15 34 36 14 49 41 25 14 35 22 35 51 50 17 22 38 54 71 41 69 44 61 18 77 3 78 53 74 70 67 8 18 10 88 2 1 74 36 15 76 62 7 70 89 24 72 77 15 44 49 ...

output:

264669337

result:

ok 1 number(s): "264669337"

Test #12:

score: 0
Accepted
time: 110ms
memory: 48192kb

input:

100000
1 1 3 2 2 6 7 6 3 4 10 1 2 14 12 16 3 2 19 20 3 2 12 17 6 17 16 9 27 18 23 21 2 31 18 13 6 17 39 13 25 18 29 11 42 17 10 34 22 9 33 31 52 45 5 54 43 52 56 4 5 47 63 51 41 54 28 65 31 70 2 63 59 53 53 40 39 5 46 71 13 6 41 31 57 4 82 62 78 59 87 72 92 9 5 69 90 92 19 15 78 41 39 23 12 1 47 49 ...

output:

399299126

result:

ok 1 number(s): "399299126"

Test #13:

score: 0
Accepted
time: 120ms
memory: 48336kb

input:

100000
1 1 1 4 5 5 3 7 6 1 7 8 8 11 11 13 7 7 1 1 13 20 21 22 22 19 8 2 29 28 4 27 8 16 30 4 5 14 21 35 29 32 35 22 14 23 41 24 33 12 31 39 4 40 24 5 38 46 20 23 37 5 27 39 32 41 26 50 33 15 50 40 40 23 52 58 31 16 25 60 36 72 29 33 48 1 82 1 25 57 15 69 5 78 29 81 36 46 97 38 15 7 39 51 19 80 29 77...

output:

58289876

result:

ok 1 number(s): "58289876"

Test #14:

score: 0
Accepted
time: 108ms
memory: 48256kb

input:

100000
1 1 1 1 4 1 7 2 4 8 6 3 2 9 15 15 5 5 7 1 12 15 4 19 7 8 15 21 26 28 13 20 14 21 30 27 21 2 3 14 1 33 33 8 41 25 11 38 35 35 35 5 16 29 16 9 24 39 13 12 3 58 20 44 3 43 53 57 13 23 44 43 14 4 23 69 27 73 22 55 25 64 52 40 71 48 56 56 8 68 27 30 92 46 18 7 58 30 65 69 61 55 38 92 33 102 80 2 2...

output:

861492056

result:

ok 1 number(s): "861492056"

Test #15:

score: 0
Accepted
time: 112ms
memory: 48240kb

input:

100000
1 1 2 3 2 2 5 8 7 4 1 3 5 11 15 2 9 8 19 5 19 11 15 19 19 11 26 3 13 15 30 1 18 28 16 33 9 23 15 2 3 36 7 11 44 31 40 15 46 7 8 5 23 36 22 12 2 28 23 14 11 40 21 18 60 24 32 42 50 57 21 27 60 54 9 63 76 56 22 59 40 41 31 58 27 68 10 45 70 54 46 29 68 6 4 61 11 7 60 56 69 92 69 5 88 71 46 21 7...

output:

528382031

result:

ok 1 number(s): "528382031"

Test #16:

score: 0
Accepted
time: 103ms
memory: 48256kb

input:

100000
1 1 3 1 1 5 4 5 3 3 6 4 1 8 10 15 5 14 5 16 9 13 14 13 8 15 26 17 1 21 11 31 18 16 21 27 14 32 9 27 30 30 3 41 33 26 47 25 26 6 24 15 11 15 6 49 48 25 23 56 3 38 31 28 54 14 17 45 60 64 24 21 14 30 20 30 38 8 13 43 37 11 83 78 75 12 30 66 37 85 24 77 72 71 49 78 88 73 25 68 19 51 79 43 93 21 ...

output:

316789948

result:

ok 1 number(s): "316789948"

Test #17:

score: 0
Accepted
time: 114ms
memory: 48260kb

input:

100000
1 2 1 2 5 4 3 8 5 8 7 6 3 10 15 12 10 4 17 15 16 13 11 1 15 10 4 16 21 11 25 11 15 4 9 21 18 16 17 29 39 3 39 1 34 5 1 14 44 5 15 16 12 15 42 28 45 32 8 33 7 32 61 9 8 34 54 66 59 61 51 51 37 40 30 61 36 36 45 18 75 27 27 45 45 53 50 77 26 89 72 41 15 18 56 53 64 6 34 33 9 90 41 50 8 4 58 101...

output:

846732448

result:

ok 1 number(s): "846732448"

Test #18:

score: 0
Accepted
time: 94ms
memory: 48208kb

input:

100000
1 1 1 3 3 6 5 7 1 3 11 6 3 2 4 4 12 1 6 10 15 8 8 20 24 10 3 5 25 4 10 13 18 30 19 11 9 6 8 24 16 5 17 2 42 33 35 3 26 42 7 42 30 17 6 41 57 53 8 19 41 50 4 16 13 45 28 50 53 22 20 2 9 30 62 25 43 76 2 41 67 74 16 2 43 64 17 28 61 15 35 33 12 60 29 64 51 33 16 18 16 15 45 18 77 40 10 87 70 72...

output:

994347719

result:

ok 1 number(s): "994347719"

Test #19:

score: 0
Accepted
time: 109ms
memory: 48492kb

input:

100000
1 1 1 4 3 3 5 8 8 1 4 9 9 14 6 12 5 3 2 9 14 4 15 11 14 14 12 18 3 22 25 1 23 1 15 8 21 35 31 34 15 23 30 13 32 18 22 33 32 18 36 46 36 27 45 16 4 35 48 12 29 1 59 64 33 12 4 1 53 8 29 16 18 67 75 7 54 18 74 21 55 69 47 54 42 56 2 85 3 90 81 42 15 90 9 41 72 68 43 58 28 87 38 22 39 29 26 44 2...

output:

946042832

result:

ok 1 number(s): "946042832"

Test #20:

score: 0
Accepted
time: 61ms
memory: 46088kb

input:

40000
1 2 2 4 1 6 5 3 3 6 3 3 4 6 10 12 1 18 18 4 11 3 9 14 25 13 14 18 4 3 1 6 13 16 9 17 37 13 38 7 10 36 13 8 22 3 17 1 20 12 33 37 8 10 25 35 41 52 10 35 36 59 20 25 32 62 18 5 3 22 66 13 2 52 38 30 62 18 35 77 51 58 32 34 44 2 70 85 46 2 80 84 67 91 91 80 19 13 42 99 75 36 38 51 62 93 96 37 96 ...

output:

599775439

result:

ok 1 number(s): "599775439"

Extra Test:

score: 0
Extra Test Passed