This page is still under construction.

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

Intact Intervals

Time limit6sMemory limit1024 MB

Summary
Count the ways to cut a circular array into at least two contiguous arcs so that each arc's multiset equals the corresponding multiset of the target array b.
Level

Hard8 of 10

Topics
Array, Hash map, Prefix sum, Combinatorics
Solved
No attempts yet

Problem

Gustav is an astronaut on the Nordic Celestial Planetary Craft (NCPC), a large space station in orbit around Mars. Today, one of Gustav's tasks is to look over the safety routines on board.

The space station consists of nn modules arranged in a circle, so that module ii is connected to module i+1i+1 for i=1…n−1i = 1 \ldots n-1, and module nn is connected to module 11. Each module ii has a non-negative integer type a_ia\_i, representing the kind of equipment that can be found there. Different modules can have the same type. In case of emergency, the equipment must be rearranged so that each module ii instead gets type b_ib\_i, for some list b_1,b_2,⋯ ,b_nb\_1, b\_2, \cdots, b\_n. Here, the list bb is a rearrangement of the list aa.

Gustav has noticed that if some module connections are severed, causing the space station to split into separate parts, it may become impossible to perform this rearrangement of the equipment. He decides to estimate how likely it is that the safety routines can be followed, by calculating in how many ways the space station can be separated into two or more parts such that it is still possible to rearrange the equipment according to the emergency procedures.

In other words, your task is to count in how many ways the circular list aa can be partitioned into at least two non-empty contiguous intervals, in such a way that the circular list bb can be obtained by rearranging elements within each interval. Since this number can be quite big, you should find its remainder modulo 109+710^9+7.

For example, consider Sample Input 1 below. Here the list aa could be split into [1∣223∣4][1 | 2 2 3 | 4], indicating that the connection between modules 11 and 22, and the connection between modules 44 and 55, are severed. Note that the connection between module 55 and 11 remains in this split. The second possible way in which aa could be split is [12∣2∣34][1 2 | 2 | 3 4].

In Sample Input 2 below, the only possible way to split the list aa into at least two non-empty parts is to separate the two modules. But then it is impossible to rearrange the parts to create the list bb. Hence, the answer is 00.

Input

The first line of input contains a single integer nn (2≤n≤1062 \leq n \leq 10^6), the number of modules. The second line contains the nn integers a_1,…a_na\_1, \ldots a\_n (0≤a_i≤1090 \leq a\_i \leq 10^9). The third and final line contains the nn integers b_1,…,b_nb\_1, \ldots, b\_n (0≤b_i≤1090 \leq b\_i \leq 10^9).

The list bb is guaranteed to be a rearrangement of the list aa.

Output

Print one integer, the number of safe separations modulo 109+710^9+7.

Examples2

  1. Example 1

    Input
    5
    1 2 2 3 4
    4 3 2 2 1
    
    Expected output
    2
    
  2. Example 2

    Input
    2
    1 2
    2 1
    
    Expected output
    0