This page is still under construction.

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

Sequence and Transformation

Time limit2sMemory limit512 MB

Summary
Count length-n sequences with entries in [1,m] whose image after applying a min-based affine transformation k times has the given max-minus-min value.
Level

Hard9 of 10

Topics
Combinatorics, Math, Dynamic programming, Implementation
Solved
No attempts yet

Problem

A transformation is applied to a sequence a1,a2,…,ana_1, a_2, \ldots, a_n of length nn. The transformation has two steps. First, build a new sequence b1,b2,…,bnb_1, b_2, \ldots, b_n with the formula below.

bi=(min⁡j=1naj)−ai+∑j=1naj(1≤i≤n)b_i = \left(\min_{j=1}^{n} a_j\right) - a_i + \sum_{j=1}^{n} a_j \quad (1 \le i \le n)

Then replace the sequence aa with the sequence bb, so aia_i takes the value bib_i for every 1≤i≤n1 \le i \le n.

For a sequence xx of length nn, define q(x)=max⁡i=1nxi−min⁡i=1nxiq(x) = \max_{i=1}^{n} x_i - \min_{i=1}^{n} x_i.

The sequence rr is the result of applying the transformation kk times to some sequence, and you are given the value q(r)q(r) together with kk. Write a program that counts the sequences c1,c2,…,cnc_1, c_2, \ldots, c_n satisfying both conditions below.

  1. 1≤ci≤m1 \le c_i \le m for every 1≤i≤n1 \le i \le n.
  2. q(d)=q(r)q(d) = q(r), where dd is the sequence obtained by applying the transformation kk times to the sequence cc.

Input

The first line contains the number of test cases TT (1≤T≤100001 \le T \le 10000).

Each test case is one line with four integers nn, mm, q(r)q(r), kk separated by spaces (1≤n,m,q(r),k≤1091 \le n, m, q(r), k \le 10^9).

Output

For each test case, print the answer modulo 109+710^9 + 7 on its own line.

Examples6

  1. Example 1

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

    Input
    5
    1 1000000000 1 1
    1 5 4 1000000000
    1 1 1000000000 1000000000
    1 2 1 7
    1 1000000000 999999999 3
    
    Expected output
    0
    0
    0
    0
    0
    
  3. Example 3

    Input
    6
    2 1 1 1
    3 5 5 2
    4 5 9 1
    10 1000000000 1000000000 5
    2 2 2 2
    7 1 1000000000 1
    
    Expected output
    0
    0
    0
    0
    0
    0
    
  4. Example 4

    Input
    5
    3 3 2 5
    3 4 2 1
    4 4 3 2
    5 6 4 3
    2 3 2 9
    
    Expected output
    12
    24
    110
    2640
    2
    
  5. Example 5

    Input
    4
    2 10 1 2
    3 10 1 3
    4 10 1 4
    5 10 1 5
    
    Expected output
    18
    54
    126
    270
    
  6. Example 6

    Input
    4
    1000000000 1000000000 1 1000000000
    1000000000 1000000000 999999999 1000000000
    1000000000 1000000000 999999998 1
    999999999 1000000000 500000000 1000000000
    
    Expected output
    875000022
    262865814
    256244787
    379452699