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: Accepted)

using namespace std;
 
#include <bits/stdc++.h>

const int MOD = 1000000007;

inline int add(int _a, int _b)
{
	_a += _b;
	if(_a >= MOD)
	{
		_a -= MOD;
	}
	return _a;
}
 
inline int mul(int _a, int _b)
{
	return (long long) _a * _b % MOD;
}
 
long long power(long long base, long long expo, long long _mod)
{
	if(base == 1)	return 1;
	if(expo == 0)	return 1;
	if(expo == 1)	return base;

	int ret = power(base, expo / 2, _mod);
	ret = mul(ret, ret);
	if(expo % 2 == 1)
	{
		ret = mul(ret,base);
	}

	return ret;
}
 
int N, A, B, C, D;
int dp[1003][1003];
int dp2[1003][1003];
int comb[2003][2003];
 
void pre()
{
	for(int i = 0; i < 2003; i++)
	{
		comb[i][0] = 1;
		for(int j = 1; j <= i; j++)
		{
			comb[i][j] = add(comb[i - 1][j], comb[i - 1][j - 1]);
		}
	}
}
 
int getWays(int groups, int groupSize)
{
	if(groups == 0)
		return 1;
 
	int &ret = dp2[groups][groupSize];
	if(ret != -1)
		return ret;
	ret = mul(comb[groups * groupSize][groupSize], power(groups, MOD - 2, MOD));
	ret = mul(getWays(groups - 1, groupSize), ret);
	return ret;
}
 
int rec(int people, int groupSize)
{
	if(groupSize > B)
	{
		if(people == 0)	return 1;
		return 0;
	}
 
	int &ret = dp[people][groupSize];
	if(ret != -1)
		return ret;
	// cerr << people << " " << groupSize << "\n";
	ret = rec(people, groupSize + 1);
 
	for(int i = C; i <= D; i++)
	{
		int peopleIncluded = i * groupSize;
		if(peopleIncluded > people)
			break;
		int thisAns = rec(people - peopleIncluded, groupSize + 1);
		thisAns = mul(thisAns, comb[people][peopleIncluded]);
		thisAns = mul(thisAns, getWays(i, groupSize));
		ret = add(ret, thisAns);
	}
 
	return ret;
}
 
 
int main()
{
	int i, j;
 
	pre();
	
	memset(dp, -1, sizeof(dp));
	memset(dp2, -1, sizeof(dp2));
 
	for(i = 0; i < 1001; i++)
	{
		for(j = 0; j < 1001; j++)
		{
			if(i * j < 1003)
				getWays(i, j);
		}
	}
 
	cin >> N >> A >> B >> C >> D;
 
	printf("%d\n", rec(N, A));
 
	return 0;
}