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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <list>
#include <stack>

using namespace std;

typedef pair<long long int, long long int> P;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

long long int DP[1100][1100] = {};
long long int fact[1100];

long long int pow_m1(long long int num){
	long long int pow_num = MOD - 2;
	long long int pow_cur = num;
	long long int ret = 1;
	while(pow_num > 0){
		if(pow_num % 2 == 1){
			ret *= pow_cur;
			ret %= MOD;
		}
		pow_cur *= pow_cur;
		pow_cur %= MOD;
		pow_num /= 2;
	}
	return ret;
}

int main(){
	
	int N, A, B, C, D;
	cin >> N >> A >> B >> C >> D;
	
	fact[0] = 1;
	for(int i = 1; i <= N; i++){
		fact[i] = fact[i - 1] * i;
		fact[i] %= MOD;
	}
	
	DP[A - 1][0] = 1;
	for(int i = A; i <= B; i++){
		for(int j = 0; j <= N; j++){
			long long int S = 1, SS;
			DP[i][j] = DP[i - 1][j];
			for(int k = C; i * k <= j && k <= D; k++){
				S *= fact[i];
				S %= MOD;
				SS = S * fact[N - j];
				SS %= MOD;
				SS = SS * fact[k];
				SS %= MOD;
				SS = fact[N - j + i * k] * pow_m1(SS);
				SS %= MOD;
				DP[i][j] += DP[i - 1][j - i * k] * SS;
				DP[i][j] %= MOD;
			}
		}
	}
	
	cout << DP[B][N] << endl;
}