TL;DR Permutation pattern is a permutation with 0 as a wildcard. You are given a permutation pattern. For each index, find its expected cycle size if a random permutation conforming to the pattern is chosen and output it modulo 998,244,353.
And now five times as long, three times as formal
A permutation is an array p of length n, such that ∀_i=j:p_i=p_j,∀_i:1≤p_i≤n
The product of permutations p and q which have the same length, denoted as p⋅q is the permutation r of the same length as both p and q, such that ∀_i:r_i=p_q_i
The power pk where p is a permutation and k is a positive integer is
Cycle size of index i is the minimal positive integer k, such that (pk)_i=i. It can be shown that such number always exists.
Permutation pattern is an array of length n such that ∀_i=j:a_i=0ora_i=a_j,∀_i:0≤a_i≤n.
We say permutation p conforms to the pattern t if ∀_i:p_i=t_iort_i=0.
Let ans_i be the expected cycle size of index i in a random permutation conforming to the pattern given in input. You are to find ans_i modulo 998,244,353.
Taking a potentially non-integer number X modulo M is the following procedure:
Jury guarantees that X is equal to some irreducible fraction QP where Q has an inverse modulo M. In that case X modulo M=A, where A is an integer between 0 and M−1 inclusive and P−QA is divisible by M. It can be shown that A is unique.
The first line contains one integer n (1≤n≤106), the length of the permutation pattern.
The second line contains n space separated integers t_i (0≤t_i≤n).
It is guaranteed that t is a permutation pattern.
Output n integers. i-th of them must be equal to ans_i modulo 998,244,353.
In the second example both ans_i are equal to 23 which equals 499122178 modulo 998244353.