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

local mma, mmi = math.max, math.min
local mce, mfl = math.ceil, math.floor

local mod = 1000000007
local function bmul(x, y)
  local x0, y0 = x % 31623, y % 31623
  local x1, y1 = mfl(x / 31623), mfl(y / 31623)
  return (x1 * y1 * 14122 + (x1 * y0 + x0 * y1) * 31623 + x0 * y0) % mod
end
local function badd(x, y)
  return (x + y) % mod
end

local function modpow(src, pow)
  local res = 1
  while 0 < pow do
    if pow % 2 == 1 then
      res = bmul(res, src)
      pow = pow - 1
    end
    src = bmul(src, src)
    pow = mfl(pow / 2)
  end
  return res
end

local function modinv(src)
  return modpow(src, mod - 2)
end

local fact = {1}
for i = 2, 1000 do
  fact[i] = bmul(fact[i - 1], i)
end
local factinv = {}
for i = 1, 1000 do
  factinv[i] = modinv(fact[i])
end

local function getComb(n, k)
  if n == k then return 1 end
  return bmul(fact[n], bmul(factinv[k], factinv[n - k]))
end

local n, a, b, c, d = io.read("*n", "*n", "*n", "*n", "*n")
local rem = {}
local remalt = {}
for i = 1, n do
  rem[i] = 0
  remalt[i] = 0
end
rem[n] = 1

local function calc(ia, ic)
  local ret = 1
  for i = 0, ic - 1 do
    ret = bmul(ret, getComb((ic - i) * ia, ia))
  end
  return bmul(ret, factinv[ic])
end

local ans = 0
for ia = a, b do
  local src = (ia - a) % 2 == 0 and rem or remalt
  local dst = (ia - a) % 2 == 0 and remalt or rem
  for i = 1, n do dst[i] = src[i] end
  local cmin = c
  local cmax = mmi(d, mfl(n / ia))
  for ic = cmin, cmax do
    local v = ic * ia
    local z = calc(ia, ic)
    ans = badd(ans, bmul(src[v], z))
    for i_n = v + 1, n do
      dst[i_n - v] = badd(dst[i_n - v], bmul(src[i_n], bmul(z, getComb(i_n, i_n - v))))
    end
  end
end
print(ans)