Count the permutations

3.7

12 votes
Advanced Data Structures, Combinatorics, Data Structures, Fenwick tree, Medium, 普通
Problem

You are given two sequences and of length . Determine the number of different ways to permute the sequence  such that it is lexicographically smaller than the sequence .

A sequence  is strictly lexicographically smaller than a sequence , if there exists an index such that:

for all

A permutation of is considered different from another permutation  of , if there exists an index such that . For example:

  • If , then there is only one different permutation of 
  • If , then there are different permutations of .

Input format

  • First line: Integer 
  • Second line:  integers -
  • Third line:  integers -

Output format

Print the remainder of the final answer when divided by

 

Sample Input
3
1 2 3
2 1 3
Sample Output
2
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Only permutations and are lexicographically smaller than .

Editor Image

?