## JOI '16 Open P3 - Skyscraper

View as PDF

Points: 25 (partial)
Time limit: 1.0s
Memory limit: 512M

Problem type
##### JOI Open Contest

The International Olympiad in Informatics will be held in Tsukuba City, Japan. In order to prepare for IOI, we are planning to construct skyscraper buildings in the main street of Tsukuba City. Since we want to create a new sightseeing spot, the buildings have to satisfy the following conditions.

We are planning to construct buildings along a straight line in the main street. The heights of them are . They are different from each other. Since the order of buildings are not yet decided, we can permute their heights if necessary. We will decorate them for IOI. Due to the constraints of materials used for decoration, the sum of the absolute values of the differences of the heights of two adjacent buildings must be less than or equal to . In other words, if the heights of the buildings from one side of the main street are , we must have . Here, is the absolute value of .

How many permutations of buildings satisfy the above conditions?

Given the number of buildings, their heights, and the upper limit of the sum of the absolute values of the differences of the heights of two adjacent buildings, write a program which calculates the number of permutations of buildings satisfying the condition. Since this number can be too big, output the remainder of division of it by .

#### Input Specification

Read the following data from the standard input.

• The first line contains two space separated integers, , . This means the number of buildings is , and the upper limit of the sum of the absolute values of the differences of the heights of two adjacent buildings is .
• The second line contains space separated integers . This means the height of the -th building is .

#### Output Specification

The output consists of one line. It contains an integer, the remainder of division of the number of permutations of buildings satisfying the condition by .

#### Constraints

All input data satisfy the following conditions.

• .
• .
• .
• .

• .

The following conditions are satisfied.

• .
• .

#### Sample Input 1

4 10
3 6 2 9

#### Sample Output 1

6

#### Explanation for Sample Output 1

There are 24 permutations in total. Among them, there are 6 permutations for which the sums of the absolute values of the differences of the heights of two adjacent builds are less than or equal to 10.

• If , , , , we have .
• If , , , , we have .
• If , , , , we have .
• If , , , , we have .
• If , , , , we have .
• If , , , , we have .

#### Sample Input 2

8 35
3 7 1 5 10 2 11 6

#### Sample Output 2

31384