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 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<inttypes.h>

typedef int32_t i32;
typedef int64_t i64;

const i32 mod = 1000000007;

i32 *fact = NULL;
i32 *iFact = NULL;
void init (const i32 n) {
  fact = (i32 *) calloc (n + 1, sizeof (i32));
  fact[0] = 1;
  for (i32 i = 1; i <= n; ++i) {
    fact[i] = (i64) i * fact[i - 1] % mod;
  }
  i32 t = 1;
  i32 a = fact[n];
  while (a > 1) {
    t = (i64) t * (mod - mod / a) % mod;
    a = mod % a;
  }
  iFact = (i32 *) calloc (n + 1, sizeof (i32));
  iFact[n] = t;
  for (i32 i = n - 1; i >= 0; --i) {
    iFact[i] = (i64) (i + 1) * iFact[i + 1] % mod;
  }
}

i32 comb (i32 n, i32 k) {
  if (!(0 <= k && k <= n)) return 0;
  return (i64) fact[n] * iFact[k] % mod * iFact[n - k] % mod;
}

void run (void) {
  i32 n, a, b, c, d;
  scanf ("%" SCNi32 "%" SCNi32 "%" SCNi32 "%" SCNi32 "%" SCNi32, &n, &a, &b, &c, &d);
  init (n);
  i32 *dp = (i32 *) calloc (n + 1, sizeof (i32));
  i32 *pow = (i32 *) calloc (n + 1, sizeof (i32));
  dp[0] = 1;
  for (i32 i = a; i <= b; ++i) {
    pow[0] = 1;
    for (i32 j = 1; j <= n; ++j) {
      pow[j] = (i64) iFact[i] * pow[j - 1] % mod;
    }
    for (i32 j = n; j >= 1; --j) {
      i32 local = dp[j];
      for (i32 k = c; k <= d && k * i <= j; ++k) {
	i32 way = (i64) comb (j, k * i) * fact[k * i] % mod * pow[k] % mod * iFact[k] % mod;
	local = (local + (i64) way * dp[j - k * i]) % mod;
      }
      dp[j] = local;
    }
  }
  printf ("%" PRIi32 "\n", dp[n]);
}

int main (void) {
  run ();
  return 0;
}