Problem:
Score : 600 points

Problem Statement

There are N people, conveniently numbered 1 through N.
We want to divide them into some number of groups, under the following two conditions:

Every group contains between A and B people, inclusive.

Let F_i be the number of the groups containing exactly i people. Then, for all i, either F_i=0 or C≤F_i≤D holds.

Find the number of these ways to divide the people into groups.
Here, two ways to divide them into groups is considered different if and only if there exists two people such that they belong to the same group in exactly one of the two ways.
Since the number of these ways can be extremely large, print the count modulo 10^9+7.

Constraints

1≤N≤10^3

1≤A≤B≤N

1≤C≤D≤N

Input

The input is given from Standard Input in the following format:

N A B C D

Output

Print the number of ways to divide the people into groups under the conditions, modulo 10^9+7.

Sample Input 1

3 1 3 1 2

Sample Output 1

4

There are four ways to divide the people:

(1,2),(3)

(1,3),(2)

(2,3),(1)

(1,2,3)

The following way to divide the people does not count: (1),(2),(3). This is because it only satisfies the first condition and not the second.

Sample Input 2

7 2 3 1 3

Sample Output 2

105

The only ways to divide the people under the conditions are the ones where there are two groups of two people, and one group of three people.
There are 105 such ways.

Sample Input 3

1000 1 1000 1 1000

Sample Output 3

465231251

Sample Input 4

10 3 4 2 5

Sample Output 4

0

The answer can be 0.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	static final long MOD = 1000000007;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int d = sc.nextInt();
		sc.close();

		long[] fact = new long[n];
		long[] ifact = new long[n];
		long mfact = 1;
		long mifact = 1;
		for(int i=1; i<=n; i++){
			mfact *= i;
			mfact %= MOD;
			fact[i-1] = mfact;
			mifact *= inverseElement(i, MOD);
			mifact %= MOD;
			ifact[i-1] = mifact;
		}
		long[][] xfact = new long[n][n];
		for(int i=0; i<n; i++){
			long tfact = 1;
			for(int j=0; j<n; j++){
				tfact *= ifact[i];
				tfact %= MOD;
				xfact[i][j] = tfact;
			}
		}

		// long[][] dp = new long[b-a+2][n+1];
		// for(int i=1; i<b-a+2; i++){
		// 	int mab = i + a - 1;
		// 	for(int j=1; j<n+1; j++){
		// 		dp[i][j] = dp[i-1][j];
		// 		for(int k=c; k<=d; k++){
		// 			int mabcd = mab*k;
		// 			if(mabcd > n) continue;
		// 			long tfact = fact[k-1];
		// 			for(int l=0; l<k; l++){
		// 				tfact *= fact[mab-1];
		// 			}
		// 			if(mabcd == j){
		// 				dp[i][j] += fact[n-1] / tfact;
		// 			}else if(mabcd < j){
		// 				dp[i][j] += dp[i-1][j-mabcd] / tfact;
		// 			}
		// 		}
		// 	}
		// }
		
		long[][] dp = new long[b-a+2][n+1];
		for(int i=1; i<b-a+2; i++){
			int mab = i + a - 1;
			out: for(int j=1; j<n+1; j++){
				dp[i][j] = dp[i-1][j];
				for(int k=c; k<=d; k++){
					int mabcd = mab*k;
					if(mabcd > n) continue out;
					long tfact = ifact[k-1];
					tfact *= xfact[mab-1][k-1];
					tfact %= MOD;
					// for(int l=0; l<k; l++){
					// 	tfact *= ifact[mab-1];
					// 	tfact %= MOD;
					// }
					if(mabcd == j){
						dp[i][j] += fact[n-1] * tfact;
					}else if(mabcd < j){
						dp[i][j] += dp[i-1][j-mabcd] * tfact;
					}
					dp[i][j] %= MOD;
				}
			}
		}

		System.out.println(dp[b-a+1][n]);
	}

	public static long inverseElement(long a, long n){
        long b = n, u=1, v=0;
        while(b > 0) {
            long t = a/b;
            a -= t*b;
            u -= t*v;
            //swap
            t = a;
            a = b;
            b = t;
            t = u;
            u = v;
            v = t;
        }
        u = u % n;
        if(u < 0) u += n;
        return u;
    }

}