Score : 600 points
问题描述
给定 (1,…,N) 的排列:p=(p1,…,pN) 和 q=(q1,…,qN)。
求满足以下条件的排列 r=(r1,…,rN) 的个数,对 (109+7) 取模:
对于所有 i(1≤i≤N),有 ri=pi 且 ri=qi。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
Given are permutations of (1,…,N): p=(p1,…,pN) and q=(q1,…,qN).
Find the number, modulo (109+7), of permutations r=(r1,…,rN) of (1,…,N) such that ri=pi and ri=qi for every i (1≤i≤N).
Constraints
- 1≤N≤3000
- 1≤pi,qi≤N
- pi=pj(i=j)
- qi=qj(i=j)
- All values in input are integers.
Input is given from Standard Input in the following format:
N
p1 … pN
q1 … qN
Output
Print the answer.
4
1 2 3 4
2 1 4 3
Sample Output 1
4
There are four valid permutations: (3,4,1,2), (3,4,2,1), (4,3,1,2), and (4,3,2,1).
3
1 2 3
2 1 3
Sample Output 2
0
The answer may be 0.
20
2 3 15 19 10 7 5 6 14 13 20 4 18 9 17 8 12 11 16 1
8 12 4 13 19 3 10 16 11 9 1 2 17 6 5 18 7 14 20 15
Sample Output 3
803776944
Be sure to print the count modulo (109+7).
update @ 2024/3/10 09:32:07