Under construction

Still being built. Things may change

Assigned Seating 2

Time limit1sMemory limit1024 MB

Summary
Count the ways to place each row's students in columns so that, within every column, height plus row offset strictly increases toward the back, modulo 10^9+7.
Level

Medium6 of 10

Topics
Sorting, Combinatorics, Binary search
Solved
No attempts yet

Problem

This problem differs from 지정좌석 배치하기 1 only in its last paragraph and its output. Code that solves 지정좌석 배치하기 1 cannot solve this problem.

Professor Dalgu, a popular instructor at UDP University, has been assigned a lecture hall with a capacity of N×MN \times M students. The seats form a rectangle of NN rows and MM columns. So that students in the back can follow the lecture, the seat height rises by DD for each row. Counting from the front, the rows are 11 to NN, and the seat height of row ii is D×iD \times i.

Professor Dalgu uses assigned seats for easy attendance checks, so each of the N×MN \times M students sits in the same seat all semester. The professor surveyed which row each student wants and selected MM students for each row. Let hi,1,hi,2,⋯ ,hi,Mh_{i,1}, h_{i,2}, \cdots, h_{i,M} be the heights of the students who want row ii.

No student's view may be blocked by a student in the same column in an earlier row. Specifically, the seat height plus the student's height must be greater than the seat height plus the height of every student in the same column in an earlier row.

Professor Dalgu wants to know how many ways there are to seat all students in their requested rows while meeting this condition. Find the count modulo 109+710^9 + 7.

Input

The first line contains NN, MM, and DD separated by spaces. (1≤N,M≤1 000; 1≤D≤100 000)(1 \leq N, M \leq 1\,000;\ 1 \leq D \leq 100\,000)

The next NN lines contain the heights of the MM students who want row ii, with line i+1i+1 holding hi,1,hi,2,⋯ ,hi,Mh_{i,1}, h_{i,2}, \cdots, h_{i,M} separated by spaces. (1≤hi,j≤100 000)(1 \leq h_{i,j} \leq 100\,000)

Output

Print the number of ways to seat all students in their requested rows that meet the condition, modulo 109+7(=1 000 000 007)10^9 + 7 (=1\,000\,000\,007).

Examples2

  1. Example 1

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

    Input
    2 1 5
    8
    2
    
    Expected output
    0