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>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iomanip>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN (-2147483647 - 1)
#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e14
#define _INFLL (long long)-1e14
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<int, int> P;

class Euclid {
private:
	ll m, n;
	vvll mx_multiply(vvll mx1, vvll mx2) {
		vvll ret(mx1.size(), vll(mx2[0].size(), 0));
		Loop(i, mx1.size()) {
			Loop(j, mx1[0].size()) {
				Loop(k, mx2[0].size()) {
					ret[i][j] += mx1[i][k] * mx2[k][j];
				}
			}
		}
		return ret;
	}
public:
	ll x, y, gcd;
	Euclid(ll M, ll N) {
		m = M;
		n = N;
	}
	//mx+ny=gcd(m,n)であるx,yを求める
	void mainfunc() {
		vll r(100), k(100);
		bool swapflag = false, mflag = false, nflag = false;
		if (m < n) { swap(m, n); swapflag = true; }
		if (m < 0) { m *= -1; mflag = true; }
		if (n < 0) { n *= -1; nflag = true; }
		r[0] = m;
		r[1] = n;
		int h = 1;
		while (1) {
			k[h - 1] = r[h - 1] / r[h];
			r[h + 1] = r[h - 1] % r[h];
			if (r[h + 1] == 0) break;
			h++;
		}
		gcd = r[h];
		vvll mx(2, vll(2, 1));
		mx[0][0] = 0;
		mx[1][1] = (-1) * k[0];
		Loop1(i, h - 1) {
			vvll multiplier(2, vll(2, 1));
			multiplier[0][0] = 0;
			multiplier[1][1] = (-1) * k[i];
			mx = mx_multiply(multiplier, mx);
		}
		x = mx[0][0];
		y = mx[0][1];
		if (nflag) { n *= -1; y *= -1; }
		if (mflag) { m *= -1; x *= -1; }
		if (swapflag) { swap(m, n); swap(x, y); }
	}
};

//x! O(x)
vll mod_factorial(ll x, ll mod) {
	if (x < 0) return{};
	else {
		vll ret((int)x + 1);
		ret[0] = 1;
		Loop1(i, (int)x) ret[i] = ret[i - 1] * i % mod;
		return ret;
	}
}

//x^y
ll mod_pow(ll x, ll y, ll mod) {
	if (y == 1) return x;
	else {
		ll ans = mod_pow(x, y / 2, mod);
		ans = ans * ans % mod;
		if (y % 2 == 1) ans = ans * x % mod;
		return ans;
	}
}

//≡1/x
ll mod_reciprocal(ll x, ll mod) {
	Euclid *euclid = new Euclid(x, mod * (-1));
	euclid->mainfunc();
	ll ret = euclid->x;
	if (ret < 0) ret += (ret / mod*(-1) + 1) * mod;
	return ret;
}


int main() {
	ll n, a, b, c, d;
	cin >> n >> a >> b >> c >> d;
	//dp[i][j] = i人以下のグループのみで、j人使っている場合の数
	vll dp(n + 1, 0), dp2(n + 1, 0);
	dp[0] = 1;
	ll mod = 1e9 + 7;
	vll fact = mod_factorial(n, mod);
	vll recfact(n + 1);
	Loop(i, n + 1) {
		recfact[i] = mod_pow(fact[i], mod - 2, mod);
	}
	Loop1(i, b) {
		dp2[0] = dp[0];
		if (i < a) continue;
		Loop1(j, n) {
			dp2[j] = dp[j];
			for (int k = c; k <= d; k++) {
				ll l = j - i * k;
				if (l < 0) break;
				ll q = dp[l] * fact[n - l] % mod;
				q = q * recfact[n - j] % mod;
				q = q * mod_pow(recfact[i], k, mod) % mod;
				q = q * recfact[k] % mod;
				dp2[j] = (dp2[j] + q) % mod;
			}
		}
		dp = dp2;
	}
	cout << dp[n] << endl;
	return 0;
}