This page is still under construction.

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

Test Data Analysis

Time limit2sMemory limit256 MB

Summary
Count bounded arrays of length N whose maximum contiguous subarray sum equals D, modulo 1,000,000,007.
Level

Hard8 of 10

Topics
Dynamic programming, Prefix sum, Combinatorics
Solved
No attempts yet

Problem

For an array XX of length NN, the maximum subarray problem asks for the largest sum of any contiguous subarray.

Each position ii must satisfy Ai≤Xi≤BiA_i \leq X_i \leq B_i. Count how many different arrays XX have maximum subarray sum exactly DD. Print the count modulo 1,000,000,0071{,}000{,}000{,}007.

Input

The first line contains an integer TT, the number of test cases.

Each test case begins with integers NN and DD (1≤N≤10001 \leq N \leq 1000, −1000≤D≤1000-1000 \leq D \leq 1000).

The next NN lines give AiA_i and BiB_i (−1000≤Ai≤Bi≤1000-1000 \leq A_i \leq B_i \leq 1000) for each position.

Output

For each test case, print one line with the number of valid arrays modulo 1,000,000,0071{,}000{,}000{,}007.

Note

When D=3D = 3 and every range is [−1,2][-1, 2] with N=3N = 3, exactly 12 arrays reach maximum subarray sum 3.

Examples4

  1. Example 1

    Input
    2
    3 3
    -1 2
    -1 2
    -1 2
    5 3
    -1 5
    -2 3
    -4 5
    -2 3
    -2 6
    
    Expected output
    12
    1897
    
  2. Example 2

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

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

    Input
    1
    2 -1
    -1 -1
    -1 -1
    
    Expected output
    1