Count the permutations

3.7

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

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

A sequence (X1,X2,,XK) is strictly lexicographically smaller than a sequence (Y1,Y2,,YK), if there exists an index t (1tK) such that:

1. Xt<Yt

2. Xr=Yr for all 1r<t

A permutation X of A is considered different from another permutation Y of A, if there exists an index i (1iN) such that XiYi. For example:

  • If A=(1,1,1), then there is only one different permutation of A
  • If A=(1,1,2,2), then there are 6 different permutations of A.

Input format

  • First line: Integer N (1N105)
  • Second line: N integers - A1,A2,,AN (1Ai2105)
  • Third line: N integers - B1,B2,,BN (1Bi2105)

Output format

Print the remainder of the final answer when divided by 109+7.

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Only permutations (1,2,3) and (1,3,2) are lexicographically smaller than (2,1,3).

Editor Image

?