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)

#include <iostream>
#define mod 1000000007
#define LL long long
using namespace std;

LL N,A,B,C,D,comb[1010][1010],dp[1010][1010],fact[1010],inv[1010],ifact[1010];


int main(){
    cin >> N >> A >> B >> C >> D;

    LL i,j,k;
    for (i=0; i<=N; i++){
        comb[i][0]=1;
        for (j=1; j<=i; j++)
            comb[i][j]=(comb[i-1][j-1]+comb[i-1][j])%mod;
    }

    fact[0]=fact[1]=inv[1]=ifact[1]=ifact[0]=1;
    for (i=2; i<=N; i++){
        fact[i]=(i*fact[i-1])%mod;
        inv[i]=(-inv[mod%i]*(mod/i))%mod+mod;
        ifact[i]=(ifact[i-1]*inv[i])%mod;
    }

    dp[A-1][0]=1; LL cr=1,aux=1;
    for (i=A; i<=B; i++){
        aux=1;
        for (j=1; j<=C-1; j++)
            aux=(aux*ifact[i])%mod;

        for (j=0; j<=N; j++){
            dp[i][j]=dp[i-1][j];
            cr=aux;
            for (k=C; k*i<=j && k<=D; k++){
                cr=(cr*ifact[i])%mod;
                dp[i][j]=(dp[i][j]+((((((comb[j][k*i]*fact[k*i])%mod)*cr)%mod)*ifact[k])%mod)*dp[i-1][j-k*i])%mod;

            }
        }
    }

    cout << dp[B][N] << "\n";

    return 0;
}