Another increasing subsequence problem
Difficulty: Hard

N numbers are given to you A1, A2, A3, ... AN-1, AN. How many increasing subsequences exist with at most K numbers where the difference between two consecutive numbers is at most D.

An increasing subsequence of K numbers is a sequence Ai1, Ai2, ... ,Aik-1, Aik where (1 <= i1 < i2 < ik-1 < ik <= N) 
and (Ai1 < Ai2 < ... < Aik-1 < Aik).

In this problem  Ai - Ai-1 <= D for each two consecutive numbers in the subsequence.

**Input specification**

The first line contains three numbers N (1 <= N <= 10000), K (1 <= K <= 100) and D (1 <= D <= 1000). Next, follow one line with N numbers Ai. (1<=Ai<=100000). 

**Output specification**

Output just one line containing the remainder of the division by 1000000007 of the required answer.


**Sample input**
<br>5 5 3
<br>1 5 4 4 1

**Sample output**
<br>7

Input Format:

Output Format:

Sample Input:

Sample Output:

Python 3 starter code:


Reference solution (Python):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;



public class Main {
	
	static int BIT[];
	static int MOD = (int) 1e9 + 7;
	
	static void update(int x, int v) {
		for(int i = x;i<=100000;i+=(i&-i))
			BIT[i] = (BIT[i] + v) % MOD;
	}
	
	static int query(int x) {
		int res = 0;
		for(int i = x;i>0;i-=(i&-i))
			res = (res + BIT[i]) % MOD;
		return res;
	}
	
	public static void main(String[] args) throws IOException {
		int N, K, D, upper, lower;
		int arr[];
		int DP[][];	
		
		InputStreamReader sr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(sr);
		
		String cad[] = br.readLine().split(" ");
		N = Integer.parseInt(cad[0]);
		K = Integer.parseInt(cad[1]);
		D = Integer.parseInt(cad[2]);
	
		arr = new int[N+1];
		DP = new int[K+1][N];
		BIT = new int[100001];
		
		cad = br.readLine().split(" ");
		for(int i = 0;i<N;i++) {
			arr[i] = Integer.parseInt(cad[i]);
			DP[1][i] = 1;
		}		
		
		for(int i = 2;i<=K;i++) {
			BIT = new int[100001];
			
			for(int j = 0;j<N;j++) {
				if(j + 1 >= i) {
					upper = query(arr[j]-1);
					lower = query(Math.max(arr[j]-D-1, 0));
					DP[i][j] = (upper + MOD - lower) % MOD;
				}
				update(arr[j], DP[i-1][j]);
			}
		}
		
		int sol = 0;
		for(int i = 1;i<=K;i++)
			for(int j = 0;j<N;j++)
				sol = (sol + DP[i][j]) % MOD;
		
		System.out.println(sol);
	}
}