PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000057. Các tập con đặc biệt (Special Subsets)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các tập con đặc biệt (Special Subsets)

Special Subsets

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Let S={1,2,…,n}S=\{1,2,\ldots,n\} and let ff be a function from SS to itself, specified by the values fi=f(i)f_i=f(i).

A subset TT of SS is special if it is nonempty and satisfies two conditions. First, for every a∈Ta\in T, the value f(a)f(a) must also belong to TT. Second, for any two distinct elements a,b∈Ta,b\in T, their images f(a)f(a) and f(b)f(b) must be different. Thus the restriction of ff to TT never maps two different selected elements to the same value.

Count all special subsets and print the count modulo 998244353998244353. Two subsets are different if their sets of elements differ.

Input

The first line contains nn; the next contains f1,…,fnf_1,\ldots,f_n.

Output

Print the number of valid subsets modulo 998244353998244353.

Subtasks

  • Subtask 1 (20 points): 1≤n≤181\le n\le18; 1≤fi≤n1\le f_i\le n; report the count modulo 998244353998244353.
  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; 1≤fi≤n1\le f_i\le n; report the count modulo 998244353998244353.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le2\cdot10^5; 1≤fi≤n1\le f_i\le n; report the count modulo 998244353998244353.

Examples

Input

3
1 2 3

Output

7

Explanation

There are three self-loops. Any nonempty selection of these three cycles works, giving 23−1=72^3-1=7.