Under construction

Still being built. Things may change

Interleaving Sequences

Time limit9sMemory limit1024 MB

Summary
Given sequences A and B, count for each x the pairs of intervals A' and B' whose minimum interleaving stability equals x.
Level

Hard9 of 10

Topics
Array, Two pointers, Combinatorics
Solved
No attempts yet

Problem

An interval of a sequence CC is any nonempty contiguous subsequence of CC. A sequence of length kk has k(k+1)/2k(k+1)/2 intervals, because each interval is determined by its start and end positions.

The stability of a sequence [c1,c2,…,ck][c_1, c_2, \ldots, c_k] is the largest integer ss such that there is an index ii (1≤i≤k−s+11 \le i \le k-s+1) with ci<ci+1<⋯<ci+s−1c_i < c_{i+1} < \cdots < c_{i+s-1} or ci>ci+1>⋯>ci+s−1c_i > c_{i+1} > \cdots > c_{i+s-1}. For example, the stability of [8,6,1,3,5,7,4,2][8, 6, 1, 3, 5, 7, 4, 2] is 4, because the strictly monotone interval [1,3,5,7][1, 3, 5, 7] exists and no longer one does.

An interleaving of two sequences AA and BB is any sequence of length ∣A∣+∣B∣|A|+|B| that contains a subsequence (not necessarily contiguous) equal to AA, where the elements outside that subsequence form BB in order. For example, the interleavings of [1,2,3][1, 2, 3] and [4,5][4, 5] include [1,4,2,5,3][1, 4, 2, 5, 3], [4,5,1,2,3][4, 5, 1, 2, 3], and [4,1,5,2,3][4, 1, 5, 2, 3]. Neither [1,2,3,4,3][1, 2, 3, 4, 3] nor [1,2,3,5,4][1, 2, 3, 5, 4] is one.

Let f(A,B)f(A, B) be the minimum possible stability of an interleaving of AA and BB.

Given two sequences AA and BB of lengths nn and mm, instead of f(A,B)f(A, B), count for each integer xx from 1 to n+mn+m the number of pairs (A′,B′)(A', B') such that A′A' is an interval of AA, B′B' is an interval of BB, and f(A′,B′)=xf(A', B') = x. These counts can be very large, so print them modulo 109+710^9+7.

Input

The first line contains two integers nn and mm (1≤n,m≤300 0001 \le n, m \le 300\,000), the lengths of AA and BB.

The second line contains nn integers A1,A2,…,AnA_1, A_2, \ldots, A_n (1≤Ai≤n+m1 \le A_i \le n+m).

The third line contains mm integers B1,B2,…,BmB_1, B_2, \ldots, B_m (1≤Bi≤n+m1 \le B_i \le n+m).

All elements of AA and BB are pairwise distinct. In other words, the concatenation of AA and BB is a permutation of 11 to n+mn+m.

Output

Print n+mn+m integers separated by single spaces. The ii-th number is the number of pairs (A′,B′)(A', B') with f(A′,B′)=if(A', B') = i, modulo 109+710^9+7.

Hint

The intervals [4,5][4, 5] does not apply here. For the whole sequences [1,2,5,7,4][1, 2, 5, 7, 4] and [8,3,6][8, 3, 6], f=2f = 2, and one interleaving with stability 2 is [1,8,2,5,3,7,4,6][1, 8, 2, 5, 3, 7, 4, 6].

For [1,2,5,7][1, 2, 5, 7] and [3][3], f=3f = 3, and one interleaving with stability 3 is [1,2,5,3,7][1, 2, 5, 3, 7]. No interleaving of this pair has stability below 3.

For [4][4] and [6][6], f=2f = 2, and both interleavings [4,6][4, 6] and [6,4][6, 4] are valid examples.

Every pair of intervals in this example can be interleaved so that the stability is at most 3. Therefore the answers for x≥4x \ge 4 are all 0.

Examples1

  1. Example 1

    Input
    5 3
    1 2 5 7 4
    8 3 6
    
    Expected output
    0 84 6 0 0 0 0 0