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 (GCC 5.4.1) (judge verdict: Accepted)

#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define min(p,q)((p)<(q)?(p):(q))
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invp(a,p)pom(a,p-2,p)
#define M 1000000007

ll fact[1010];
ll invfact[1010];
ll dp[1010][1010];
//dp[i][j]=i人以下のグループを決めてまだj人余ってる場合の数

int main(){
	int n,a,b,c,d;
	scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);
	fact[0]=1;
	rep(i,1,1005)fact[i]=fact[i-1]*i%M;
	invfact[1000]=invp(fact[1000],M);
	for(int i=999;i>=0;i--)invfact[i]=invfact[i+1]*(i+1)%M;
	
	dp[a-1][n]=1;
	rep(g,a,b+1){
		rep(rest,0,n+1){
			dp[g][rest]=(dp[g][rest]+dp[g-1][rest])%M;
			//残りのrest人からg人グループをcnt個作る
			//rest人から(g*cnt)人選んで、グループの順列がcnt!、グループ内の人の順列が(g!)^cnt
			rep(cnt,c,min(d,rest/g)+1){
				dp[g][rest-g*cnt]=(dp[g][rest-g*cnt]
								   +dp[g-1][rest]
								   *(fact[rest]*invfact[rest-g*cnt]%M)%M
								   *invfact[cnt]%M
								   *pom(invfact[g],cnt,M)
								  )%M;
			}
		}
	}
	printf("%lld\n",dp[b][0]);
	return 0;
}