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 9.2.1) (judge verdict: Time Limit Exceeded)

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
long long mod = 1e9 + 7;
long long fact[1005];
long long dp[1003][1003];
long long bigmod(long long a, long long p){
    if(p == 0){
        return 1;
    }
    long long ans = bigmod(a, p/2);
    ans = (ans * ans) % mod;
    if(p % 2 == 1){
        ans = (ans * a) % mod;
    }
    return ans;
}
long long inverse(long long a){
    return bigmod(a, mod - 2);
}
long long nCr(long long n, long long r){
    return (fact[n] * inverse((fact[r] * fact[n - r]) % mod)) % mod;
}
long long group(long long a, long long k){
    long long x = fact[a * k];
    long long y = (bigmod(fact[a], k) * fact[k]) % mod;
    return (x * inverse(y)) % mod;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    fact[0] = 1;
    for(long long i = 1; i <= 1002; i++){
        fact[i] = (fact[i - 1] * i) % mod;
    }
    int n, a, b, c, d;
    cin >> n >> a >> b >> c >> d;
    for(int i = 0; i <= b; i++){
        for(int j = 0; j <= n; j++){
            dp[i][j] = 0;
        }
    }
    dp[a][0] = 1;
    for(long long k = c; k <= d; k++){
        if(a * k <= n){
            dp[a][a * k] = group(a, k);
        }
    }
    for(long long i = a + 1; i <= b; i++){
        dp[i][0] = 1;
        for(long long j = 1; j <= n; j++){
            dp[i][j] = dp[i - 1][j];
            for(long long k = c; k <= d; k++){
                if(j - k * i < 0){
                    break;
                }
                long long cur = (nCr(j, k * i) * dp[i - 1][j - k * i]) % mod;
                cur = (cur * group(i, k)) % mod;
                dp[i][j] = (dp[i][j] + cur) % mod;
            }
        }
    }
    cout << dp[b][n];
    return 0;
}