Under construction

Still being built. Things may change

Sleeping Cows

Time limit1sMemory limit512 MB

Summary
Count the maximal matchings between cows and barns, where each cow fits in its assigned barn and no unmatched cow fits in any empty barn, modulo 1e9+7.
Level

Hard8 of 10

Topics
Dynamic programming, Sorting, Combinatorics
Solved
No attempts yet

Problem

Farmer John has NN (1≤N≤3000)(1 \le N \le 3000) cows of various sizes. He originally built each cow a personalized barn, but now some of the cows have outgrown their barns. Specifically, FJ built NN barns of sizes t_1,t_2,…,t_Nt\_1,t\_2,\ldots,t\_N, while the cows are now of sizes s_1,s_2,…,s_Ns\_1,s\_2,\ldots,s\_N (1≤s_i,t_i≤1091\le s\_i,t\_i\le 10^9).

Every night, the cows go through a ritual of finding a barn to sleep in. Cow ii can sleep in barn jj if and only if it fits inside the barn (s_i≤t_js\_i\le t\_j). Each barn can house at most one cow.

A matching of cows to barns is maximal if every cow assigned to a barn fits in that barn, and every unassigned cow is too big for every empty barn left out of the matching.

Compute the number of maximal matchings modulo 109+710^9 + 7.

Input

The first line contains NN.

The second line contains NN space-separated integers s_1,s_2,…,s_Ns\_1,s\_2,\ldots,s\_N.

The third line contains NN space-separated integers t_1,t_2,…,t_Nt\_1,t\_2,\ldots,t\_N.

Output

Print the number of maximal matchings modulo 109+710^9 + 7.

Hint

Here is a list of all nine maximal matchings. An ordered pair (i,j)(i,j) means that cow ii is assigned to barn jj.

(1, 1), (2, 2), (3, 4)
(1, 1), (2, 3), (3, 4)
(1, 1), (2, 4)
(1, 2), (2, 3), (3, 4)
(1, 2), (2, 4)
(1, 3), (2, 2), (3, 4)
(1, 3), (2, 4)
(1, 4), (2, 2)
(1, 4), (2, 3)

Examples1

  1. Example 1

    Input
    4
    1 2 3 4
    1 2 2 3
    
    Expected output
    9