Under construction

Still being built. Things may change

Cubes

Time limit1sMemory limit1024 MB

Summary
Counts the distinct weight sequences reachable from a row when adjacent cubes whose total weight is at most K can swap, modulo 1e9+7.
Level

Medium7 of 10

Topics
Combinatorics, Sorting, Hash map
Solved
No attempts yet

Problem

Octavia has NN cubes of identical size among her toys. The cubes may differ in weight, and two cubes may also share the same weight. Octavia likes to set the cubes in a long row. After many hours of play, she started to wonder how many different rows of cubes she can make.

A row of cubes is given as NN integers, where each integer is the weight of the corresponding cube in the initial row. Octavia can swap two adjacent cubes only if their total weight is at most KK. She may repeat this swap on any adjacent pair as many times as she wants. Two rows are different if their sequences of cube weights differ.

For example, take 4 cubes with weights [1,2,1,3][1, 2, 1, 3] and K=3K = 3. There are 3 possible rows. The first is the initial row. To get the second, swap the cubes at positions 2 and 3 to get [1,1,2,3][1, 1, 2, 3]. The cubes at positions 1 and 3 cannot be swapped because they are not adjacent. The cubes at positions 3 and 4 cannot be swapped because their total weight is 4, which is larger than K=3K = 3. The last row comes from swapping the first two cubes, giving [2,1,1,3][2, 1, 1, 3]. With the same initial row and K=1K = 1, there is only one possible row. With K=2K = 2, there is also only one. With K=4K = 4, there are 6 rows, and with K=5K = 5, there are 12 rows.

Write a program named cubes that finds the number of different rows of cubes Octavia can obtain.

Input

The first line has two integers NN, the number of cubes, and KK, the maximum total weight of two adjacent cubes that can be swapped. The second line has NN positive integers w1,…,wNw_1, \dots, w_N, the weights in the initial row.

Output

Print a single line with the number of possible rows, modulo 1 000 000 0071\,000\,000\,007.

Constraints

1≤N≤300 0001 \le N \le 300\,000

1≤wi,K≤1 000 000 0001 \le w_i, K \le 1\,000\,000\,000

Hint

In the second case, only the cubes with weights 1 and 3 can be swapped, so the answer is 2. The two possible rows are [4,3,1,5,2][4, 3, 1, 5, 2] and [4,1,3,5,2][4, 1, 3, 5, 2].

Examples2

  1. Example 1

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

    Input
    5 4
    4 3 1 5 2
    
    Expected output
    2