AT_abc468_c [ABC468C] Between P and Q
Description
You are given an integer $ N $ and integer sequences $ P=(P_1,P_2,\ldots, P_N) $ and $ Q=(Q_1,Q_2,\ldots,Q_N) $ , each of which is a permutation of $ (1,2,\ldots,N) $ .
Find the number of integer sequences that are a permutation of $ (1,2,\ldots,N) $ and are lexicographically greater than $ P $ and lexicographically less than $ Q $ .
What is lexicographic order for integer sequences?For integer sequences $ S = (S_1,S_2,\ldots,S_{|S|}) $ and $ T = (T_1,T_2,\ldots,T_{|T|}) $ , we say that $ S $ is **lexicographically smaller** than $ T $ if $ 1. $ or $ 2. $ below holds. Here, $ |S|, |T| $ denote the lengths of $ S, T $ , respectively.
1. $ |S| \lt |T| $ and $ (S_1,S_2,\ldots,S_{|S|}) = (T_1,T_2,\ldots,T_{|S|}) $ .
2. There exists an integer $ 1 \leq i \leq \min\lbrace |S|, |T| \rbrace $ such that both of the following two conditions hold.
- $ (S_1,S_2,\ldots,S_{i-1}) = (T_1,T_2,\ldots,T_{i-1}) $
- $ S_i $ is (numerically) smaller than $ T_i $ .
Input Format
The input is given from Standard Input in the following format:
> $ N $ $ P_1 $ $ P_2 $ $ \ldots $ $ P_N $ $ Q_1 $ $ Q_2 $ $ \ldots $ $ Q_N $
Output Format
Output the answer.
Explanation/Hint
### Sample Explanation 1
Two sequences $ (2,1,3),(2,3,1) $ satisfy the condition. Thus, output $ 2 $ .
### Sample Explanation 2
There is no permutation of $ (1,2,3,4,5) $ satisfying the condition.
### Constraints
- $ 1\le N\le 10 $
- $ P $ and $ Q $ are integer sequences that are permutations of $ (1,2,\ldots,N) $ .
- All input values are integers.