Under construction

Still being built. Things may change

Differencia

Time limit14sMemory limit256 MB

Summary
Given sequences a and b, process m generated operations that either assign x to a range of a or count positions in a range where a_i is at least b_i.
Level

Hard8 of 10

Topics
Segment tree, Binary search, Sorting, Implementation
Solved
No attempts yet

Problem

Professor Zhang has two sequences a1,a2,…,ana_1, a_2, \ldots, a_n and b1,b2,…,bnb_1, b_2, \ldots, b_n. He wants to perform two kinds of operations on the sequences:

  • "+ ll rr xx": set aia_i to xx for all l≤i≤rl \le i \le r.
  • "? ll rr": find the number of ii such that ai≥bia_i \ge b_i and l≤i≤rl \le i \le r.

The number of operations can be large, so the mm operations are specified by parameters AA and BB given to the following generator routine.

int a = A, b = B, C =  (1<<31), M = (1<<16)-1;
int rnd(int last) {
  a = (36969 + (last >> 3)) * (a & M) + (a >> 16);
  b = (18000 + (last >> 3)) * (b & M) + (b >> 16);
  return (C & ((a << 16) + b)) % 1000000000;
}

For the ii-th operation, first call rnd(last) three times to get ll, rr and xx: l=rnd(last) mod n+1l = \text{rnd}(last) \bmod n + 1, r=rnd(last) mod n+1r = \text{rnd}(last) \bmod n + 1, x=rnd(last)+1x = \text{rnd}(last) + 1. If l>rl > r, swap them. The operation is of type "?" if l+r+xl + r + x is even, and of type "+" otherwise. Here lastlast is the answer of the most recent "?" operation, and last=0last = 0 at the beginning of each test case.

Input

There are multiple test cases. The first line contains an integer TT, the number of test cases. For each test case, the first line contains four integers nn, mm, AA and BB (1≤n≤1051 \le n \le 10^{5}, 1≤m≤3 000 0001 \le m \le 3\,000\,000, 1≤A,B≤2161 \le A, B \le 2^{16}). The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^{9}). The third line contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤1091 \le b_i \le 10^{9}).

There are at most 300300 test cases, and the total input size is at most 88 mebibytes.

Output

For each test case, output the integer S=(∑i=1mi⋅zi) mod (109+7)S = \left(\sum_{i=1}^{m} i \cdot z_i\right) \bmod (10^9 + 7), where ziz_i is the answer to the ii-th query. If the ii-th operation is of type "+", then zi=0z_i = 0.

Examples1

  1. Example 1

    Input
    3
    5 10 1 2
    5 4 3 2 1
    1 2 3 4 5
    5 10 3 4
    5 4 4 2 1
    1 2 3 4 5
    5 10 5 6
    5 4 5 2 1
    1 2 2 4 5
    
    Expected output
    81
    88
    87