Loading...
Squirrel and Relocation
Time: 1 s
Memory: 125 MB
You are invited to participate in the annual Squirrel Games. For the \(1\)st game, you are given the following scenario. Few squirrels live in a permutation \(p\) of size \(n\). If a squirrel is currently living in the \(i\)-th index, it can travel to and live in the \(p_i\)-th index if it wants to. For every squirrel, it is ensured that if it can travel to any other index, it will be empty. For every index, it is ensured that a certain squirrel can always travel to and live in it. 
Since we can’t read the minds of the squirrels, given the permutation \(p\), you are asked to find the number of all possible ways the permutation can contain the squirrels.

A permutation of size \(n\) is an array where each integer from \(1\) to \(n\) occurs exactly once. For example, \([3,1,2,4]\) is a permutation of size \(4\), \([2,4,3,5]\) is not a permutation (\(1\) is missing), \([1,2,2]\) is not a permutation (\(2\) is present twice).
Input
The first line contains a single integer \(t\) \((1 \leq t \leq 10^5)\) — the number of test cases.
The first line of each test case contains a single integer \(n\) \((1 \leq n \leq 10^5)\) — the size of permutation.
The second line of each test case contains \(n\) integers \(p_1, p_2, \dots, p_n\) \((1 \leq p_i \leq n)\) — the permutation \(p\).
It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
Constraint
\(1 \leq t \leq 10^5\)
\(1 \leq n \leq 10^5\)
Output
For each test case, print a single integer — the number of all possible ways the permutation can contain the squirrels. Since the answer can be very large, print it modulo \(998244353\).
Examples
Input
Output
1
4
2 1 3 4
2
Input
Output
2
3
1 2 3
4
2 3 4 1

1
4
Input
Output
1
5
2 4 5 1 3
6
Notes
In the first test case of the first sample, there are \(3\) squirrels and there are exactly \(2\) ways the permutation can contain the squirrels. The first way: the \(1\)st squirrel on the \(1\)st position, \(2\)nd and \(3\)rd squirrel on the \(3\)rd and \(4\)th position respectively. The second way: the \(1\)st squirrel on the \(2\)nd position, \(2\)nd and \(3\)rd squirrel on the \(3\)rd and \(4\)th position respectively.

In the first test case of the second sample, there are \(3\) squirrels and there is exactly \(1\) ways the permutation can contain the squirrels. The \(1\)st squirrel on the \(1\)st position, the \(2\)nd squirrel on the \(2\)nd position and the \(3\)rd squirrel on the \(3\)rd position.

In the second test case of the second sample, there is \(1\) squirrel and there is exactly \(4\) ways the permutation can contain the squirrel. First way: the squirrel on the \(1\)st position and rest of the positions are empty. Second way: the squirrel on the \(2\)nd position and rest of the positions are empty. Third way: the squirrel on the \(3\)rd position and rest of the positions are empty. Fourth way: the squirrel on the \(4\)th position and rest of the positions are empty.
Problem Info
Problem ID 99
Time Limit 1000 ms
Memory Limit 128000 KB
Moderators amirhozaifa , AmirHamza , GoodFella , sabbir_hasan_anik , uzzal_ewu
Statistics
Submit
You need to Login or Registration for submit your solution