This page is still under construction.

Parts of this page are still being built. What you see may change.

Arranging game levels

Time limit1sMemory limit256 MB

Summary
Count rooted tree arrangements of N levels where each level's clear score S_i and the cumulative score K_i along the root-to-level path are given, and children must have larger S than parents.
Level

Hard8 of 10

Topics
Tree, Combinatorics, Sorting, DFS
Solved
No attempts yet

Problem

Hyunwook makes games as a hobby, and today he sketched a simple design. The game has NN levels, and the player earns a fixed number of points for clearing each one. Every level has a fixed list of levels that the player may move to after clearing it, and the player picks one of them.

The design is as follows.

  1. There is one starting level.
  2. A way to reach any other level from the starting level always exists, and it is unique.
  3. Clearing level ii gives the player SiS_i points.
  4. Each level ii also has a value KiK_i, the cumulative score the designer intends the player to hold right after clearing level ii. It is the sum of the clear scores of every level on the way from the starting level to level ii, including level ii itself.
  5. If level ii has to be cleared after level jj, then Si>SjS_i > S_j must hold. The game only moves toward levels that award more points.

Hyunwook wants to know how many level arrangements satisfy these rules. Write a program that counts the arrangements of the NN levels that match all of the given SiS_i and KiK_i.

Input

The first line contains the number of levels NN (1≤N≤3×1051 \le N \le 3 \times 10^5).

The second line contains S1,S2,…,SNS_1, S_2, \dots, S_N (1≤Si≤1091 \le S_i \le 10^9), separated by spaces.

The third line contains K1,K2,…,KNK_1, K_2, \dots, K_N (1≤Ki≤1091 \le K_i \le 10^9), separated by spaces.

Output

Print the number of arrangements that satisfy the given SiS_i and KiK_i, modulo 109+710^9 + 7. Two arrangements count as the same one when every level has the same list of next levels in both, where the order inside a list does not matter.

Hint

The first example has exactly one valid arrangement. Level 1 is the starting level, and after clearing it the player chooses between level 2 and level 3. The second example has no valid arrangement.

Examples2

  1. Example 1

    Input
    3
    2 6 4
    2 8 6
    
    Expected output
    1
    
  2. Example 2

    Input
    3
    2 1 3
    2 3 4
    
    Expected output
    0