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)


#include <iostream>
#include <algorithm>
#include <string.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

const int MAXN=1000;
int n,a,b,c,d;
ll f[MAXN+10];
ll finv[MAXN+10];
ll dp[MAXN+10][MAXN+10];

ll qp(ll x,ll n) {
    ll ret=1;
    while (n) {
        if (n&1) ret=(ret*x)%MOD;
        n=n>>1;
        x=(x*x)%MOD;
    }
    return ret;
}

int main() {
    scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);
    f[0]=finv[0]=1;
    for (int i=1;i<=MAXN;i++) {
        f[i]=(f[i-1]*i)%MOD;
        finv[i]=qp(f[i],MOD-2);
    }
    for (int i=0;i<=n;i++) dp[0][i]=1;
    for (int j=a;j<=b;j++) {
        for (int i=1;i<=n;i++) {
            ll accum=1;
            dp[i][j]=dp[i][j-1];
            for (int k=c;k*j<=i&&k<=d;k++) {
                accum=(accum*finv[j])%MOD;
                ll co1=(((f[i]*finv[k*j])%MOD)*finv[i-k*j])%MOD;
                ll co2=(((f[k*j]*finv[k])%MOD)*accum)%MOD;
                ll co=(co1*co2)%MOD;//printf("co1:%lld co2:%lld co:%lld\n",co1,co2,co);
                dp[i][j]=(dp[i][j]+(co*dp[i-k*j][j-1])%MOD)%MOD;
            }
            //printf("i:%d j:%d val:%lld\n",i,j,dp[i][j]);
        }
    }
    printf("%lld\n",dp[n][b]);
}