9615: Love Wins All

内存限制:512 MB 时间限制:2 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

It's a loving community!

There are n residents in the community, and each resident i (1in) in the community has a unique resident ai (1in) in the community whom he/she loves so much. Each two residents love different residents. A resident can love himself / herself. It is guaranteed that n is even.

One day, a bad thing happens: They need to choose 2 residents to be forbidden to get married forever.

And to prevent such a thing from happening in the future, the rest n2 residents decide to get married as n/21 couples, each couple consisting of 2 persons (of course). It makes no sense that a couple consists of resident x and resident y while neither x loves y nor y loves x, so such a thing never happens.

So, as the planner, you need to figure out how you can arrange this. You want to know the number of different marriage plans. Two marriage plans are considered different if at least one of the following conditions is satisfied:

  • In one plan, a person i is married, and in the other, he/she is not.
  • In one plan, a person i is married to j, and in the other, he/she is not married to j.

As the number of plans can be quite enormous, output it modulo 998 244 353.

输入格式

Each test contains multiple test cases. The first line contains the number of test cases T (1T104) .
Each test case consists of two lines.
The first line contains 1 integer n (4n5×105), the number of residents in the community. It's guaranteed that nnn is even.
The second line contains n integers a1,a2,,an (1ain), where ai represents the one that the resident i loves. It is guaranteed that if i=j (1i,jn), ai=aj .
It is guaranteed that n over all test cases in one test will not exceed 5×105 .

输出格式

For each test case, output 1 integer: the number of different marriage plans modulo 998 244 353.

输入样例 复制

2
4
1 3 4 2
6
3 4 5 6 2 1

输出样例 复制

3
9