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"bits/stdc++.h"
using namespace std;
using ll = int64_t;

constexpr ll MOD = (ll)1e9 + 7;

ll MODpow(ll n, ll m) {
    ll result = 1;
    while (m) {
        if (m % 2 == 1) {
            result *= n;
            result %= MOD;
        }

        m /= 2;
        n *= n;
        n %= MOD;
    }

    return result;
}

class Combination {
public:
    Combination(ll max_num, ll mod) {
        fact_.resize(max_num + 1, 1);
        inv_fact_.resize(max_num + 1, 1);
        mod_ = mod;
        for (ll i = 2; i <= max_num; i++) {
            fact_[i] = i * fact_[i - 1] % mod_;
            inv_fact_[i] = MODpow(fact_[i], mod_ - 2);
            assert(fact_[i] * inv_fact_[i] % mod_ == 1);
        }
    }
    ll operator()(ll n, ll m) const {
        if (m < 0 || m > n) return 0;
        return fact_[n] * inv_fact_[n - m] % mod_ * inv_fact_[m] % mod_;
    }
    ll fact(ll n) const {
        return fact_[n];
    }
private:
    vector<ll> fact_, inv_fact_;
    ll mod_;
};

ll N, A, B, C, D;
vector<vector<ll>> memo;
Combination comb(2001, MOD);

//i人をj人のグループだけを使って分ける方法の数
ll f(ll i, ll j) {
    ll k = i / j;

    return comb.fact(i) * MODpow(MODpow(comb.fact(j), k), MOD - 2) % MOD * MODpow(comb.fact(k), MOD - 2) % MOD;
}

//i人をj人以下のグループだけを使って場合分けする数
ll solve(ll i, ll j) {
    if (i == 0) {
        return 1;
    }
    if (i < 0) {
        return 0;
    }
    if (j == A - 1) {
        return 0;
    }

    if (memo[i][j] != -1) {
        return memo[i][j];
    }

    //j人ちょうどのグループをk個とする
    ll result = solve(i, j - 1);
    for (ll k = C; k <= D; k++) {
        if (j * k > i) {
            break;
        }

        ll tmp = solve(i - j * k, j - 1);

        //i人からj * k人選ぶ方法
        (tmp *= comb(i, j * k)) %= MOD;

        //j * k人をk個のグループに分ける方法
        (tmp *= f(j * k, j)) %= MOD;

        (result += tmp) %= MOD;
    }

    return memo[i][j] = (result % MOD);
}

int main() {
    cin >> N >> A >> B >> C >> D;
    memo.resize(N + 1, vector<ll>(N + 1, -1));
    cout << solve(N, B) << endl;
}