102146: [AtCoder]ABC214 G - Three Permutations

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Score : $600$ points

Problem Statement

Given are permutations of $(1, \dots, N)$: $p = (p_1, \dots, p_N)$ and $q = (q_1, \dots, q_N)$.

Find the number, modulo $(10^9 + 7)$, of permutations $r = (r_1, \dots, r_N)$ of $(1, \dots, N)$ such that $r_i \neq p_i$ and $r_i \neq q_i$ for every $i$ $(1 \leq i \leq N)$.

Constraints

  • $1 \leq N \leq 3000$
  • $1 \leq p_i, q_i \leq N$
  • $p_i \neq p_j \, (i \neq j)$
  • $q_i \neq q_j \, (i \neq j)$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$
$p_1$ $\ldots$ $p_N$
$q_1$ $\ldots$ $q_N$

Output

Print the answer.


Sample Input 1

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)$.


Sample Input 2

3
1 2 3
2 1 3

Sample Output 2

0

The answer may be $0$.


Sample Input 3

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 $(10^9 + 7)$.

Input

题意翻译

给出两个大小为 $n$ 的排列 $p,q$,你需要求出大小为 $n$ 的排列 $r$ 的数量,使得 $r_i\neq p_i,r_i\neq q_i$。

Output

分数:$600$分

问题描述

给定$(1, \dots, N)$的排列:$p = (p_1, \dots, p_N)$和$q = (q_1, \dots, q_N)$。

找出$(1, \dots, N)$的排列$r = (r_1, \dots, r_N)$的数量,模$(10^9 + 7)$,使得对于每个$i$ $(1 \leq i \leq N)$,有$r_i \neq p_i$且$r_i \neq q_i$。

限制条件

  • $1 \leq N \leq 3000$
  • $1 \leq p_i, q_i \leq N$
  • $p_i \neq p_j \, (i \neq j)$
  • $q_i \neq q_j \, (i \neq j)$
  • 输入中的所有值都是整数。

输入

从标准输入按照以下格式获取输入:

$N$
$p_1$ $\ldots$ $p_N$
$q_1$ $\ldots$ $q_N$

输出

打印答案。


样例输入 1

4
1 2 3 4
2 1 4 3

样例输出 1

4

有四个有效的排列:$(3, 4, 1, 2)$,$(3, 4, 2, 1)$,$(4, 3, 1, 2)$和$(4, 3, 2, 1)$。


样例输入 2

3
1 2 3
2 1 3

样例输出 2

0

答案可以是$0$。


样例输入 3

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

样例输出 3

803776944

确保打印的计数模$(10^9 + 7)$。

加入题单

算法标签: