atcoder#ARC114B. [ARC114B] Special Subsets

[ARC114B] Special Subsets

Score : 400400 points

Problem Statement

Let SS be a set composed of all integers from 11 through NN.

ff is a function from SS to SS. You are given the values f(1),f(2),,f(N)f(1), f(2), \cdots, f(N) as f1,f2,,fNf_1, f_2, \cdots, f_N.

Find the number, modulo 998244353998244353, of non-empty subsets TT of SS satisfying both of the following conditions:

  • For every aTa \in T, f(a)Tf(a) \in T.
  • For every a,bTa, b \in T, f(a)f(b)f(a) \neq f(b) if aba \neq b.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1fiN1 \leq f_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

f1f_1 f2f_2 \ldots fNf_N

Output

Print the number of non-empty subsets of SS satisfying both of the conditions, modulo 998244353998244353.

2
2 1
1

We have f(1)=2,f(2)=1f(1) = 2, f(2) = 1. Since f(1)f(2)f(1) \neq f(2), the second condition is always satisfied, but the first condition requires TT to contain 11 and 22 simultaneously.

2
1 1
1

We have f(1)=f(2)=1f(1) = f(2) = 1. The first condition requires TT to contain 11, and the second condition forbids TT to contain 22.

3
1 2 3
7

We have f(1)=1,f(2)=2,f(3)=3f(1) = 1, f(2) = 2, f(3) = 3. Both of the conditions are always satisfied, so all non-empty subsets of TT count.