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# (Mono 4.6.2.0) (judge verdict: Accepted)

//#pragma warning disable

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

public class ModuloInt
{
    public static readonly ModuloInt Zero;
    public static readonly ModuloInt One;

    readonly long _n;  // 乗算を使用する場合、 n は int.MaxValue 以下でないと計算結果が正しくならない可能性がある
    static readonly long _mod;

    static ModuloInt()
    {
        _mod = (long)Math.Pow(10, 9) + 7;  // change if necessary (need to be a prime)

        Zero = new ModuloInt(0);
        One = new ModuloInt(1);
    }

    public ModuloInt(long n) { _n = (n + _mod) % _mod; }

    public long ToInt64() { return _n; }

    public static explicit operator ModuloInt(long x) { return new ModuloInt(x); }
    public static explicit operator long(ModuloInt x) { return x._n; }
    public static ModuloInt operator +(ModuloInt x, long y) { return new ModuloInt(x._n + y); }
    public static ModuloInt operator +(long x, ModuloInt y) { return new ModuloInt(x + y._n); }
    public static ModuloInt operator +(ModuloInt x, ModuloInt y) { return new ModuloInt(x._n + y._n); }
    public static ModuloInt operator -(ModuloInt x, long y) { return new ModuloInt(x._n - y); }
    public static ModuloInt operator -(long x, ModuloInt y) { return new ModuloInt(x - y._n); }
    public static ModuloInt operator -(ModuloInt x, ModuloInt y) { return new ModuloInt(x._n - y._n); }
    public static ModuloInt operator *(ModuloInt x, long y) { return new ModuloInt(x._n * y); }
    public static ModuloInt operator *(long x, ModuloInt y) { return new ModuloInt(x * y._n); }
    public static ModuloInt operator *(ModuloInt x, ModuloInt y) { return new ModuloInt(x._n * y._n); }
    public static ModuloInt operator /(ModuloInt x, long y) { return x / new ModuloInt(y); }
    public static ModuloInt operator /(long x, ModuloInt y) { return new ModuloInt(x) / y; }
    public static ModuloInt operator /(ModuloInt x, ModuloInt y)
    {
        if (x._n == y._n) return One;
        if (y._n == 1) return x;

        var result = m3(_mod, y._n);
        //Console.WriteLine(x._n+", "+y._n+result + "  correct=" + x._n/y._n);
        return x * result;
    }
    public ModuloInt Power(long n)
    {
        ModuloInt m = this;
        ModuloInt ret = One;
        while (n >= 1)
        {
            if ((n & 1) == 1)
            {
                ret *= m;
            }

            m = m * m;
            n >>= 1;
        }
        return ret;
    }

    private static long m3(long a, long b)
    {
        // a > b
        if (b == 0)
        {
            throw new Exception();
        }
        var x = (int)a;
        var y = (int)b;
        //var x0 = 1;
        //var x1 = 0;
        var y0 = 0;
        var y1 = 1;

        while (y != 0)
        {
            var q = (int)x / (int)y;
            var r = x - q * y;

            //var xx = x0 - q * x1;
            var yy = y0 - q * y1;
            
            x = y;
            y = r;

            //x0 = x1;
            //x1 = xx;

            y0 = y1;
            y1 = yy;
        }
        return y0;
    }

    public override string ToString() { return _n.ToString(); }
}

public class Test
{
    static Dictionary<long, ModuloInt> _fact = new Dictionary<long, ModuloInt>();

    public static ModuloInt fact(long n)
    {
        if (n == 0 || n == 1) return ModuloInt.One;

        ModuloInt ret;
        if (_fact.TryGetValue(n, out ret))
        {
            return ret;
        }
        else
        {
            _fact[n] = ret = fact(n - 1) * n;
            return ret;
        }
    }

    static Dictionary<Tuple<long, long>, long> _choose = new Dictionary<Tuple<long, long>, long>();
    public static ModuloInt choose(long N, long m)
    {
        if (_choose.ContainsKey(Tuple.Create(N, m)))
        {
            return new ModuloInt(_choose[Tuple.Create(N, m)]);
        }

        ModuloInt ret = (ModuloInt)1;
        ModuloInt div = (ModuloInt)1;
        m = Math.Min(m, N - m);

        /*for (int i = 0; i < m; i++)
        {
            _choose[Tuple.Create(N, (long)i)] = (ret / div).ToInt64();

            ret *= N - i;
            div *= (i + 1);
        }*/

        if (m == 0) return (ModuloInt)1;

        var ret2 = choose(N, m - 1) * (N - m + 1) / m;

        //var ret2 = ret / div;
        _choose[Tuple.Create(N, m)] = ret2.ToInt64();
        return ret2;
    }

    static Dictionary<Tuple<long, long, long>, long> _chooseg = new Dictionary<Tuple<long, long, long>, long>();
    public static ModuloInt chooseGroups(long N, long m, long c)
    {
        if (_chooseg.ContainsKey(Tuple.Create(N, m, c)))
        {
            return new ModuloInt(_chooseg[Tuple.Create(N, m, c)]);
        }

        ModuloInt ret = ModuloInt.One;
        ModuloInt div = fact(c);

        /*
        for (int i = 0; i < c; i++)
        {
            ret *= choose(N, m);
            N -= m;
        }*/
        if (c == 1)
        {
            return choose(N, m);
        }
        
        var ret2 = chooseGroups(N - m, m, c - 1) * choose(N, m) / c;

        //var ret2 = ret / div;
        _chooseg[Tuple.Create(N, m, c)] = ret2.ToInt64();
        return ret2;
    }
    
    public static void Main()
    {
        var line1 = Console.ReadLine().Split(' ').Select(long.Parse).ToArray();
        var N = line1[0];
        var A = line1[1];
        var B = line1[2];
        var C = line1[3];
        var D = line1[4];

        var fact_arr = Enumerable.Range(0, 1001).Select(x => fact(x)).ToArray();

        /*if (N == 1000 && A == 1 && B == 1000 && C == 1 && D == 1000)
        {
            Console.WriteLine("465231251");  // これは嘘解法
            return;
        }*/

        var dp1 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();  // dp[i-1][]
        var dp2 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();  // dp[i][]

        dp1[0] = (ModuloInt)1;

        for (int i = (int)A; i <= B; i++)
        {
            //Console.WriteLine(i);
            for (int j = 0; j <= N; j++)
            {
                dp2[j] = dp1[j];

                for (int k = (int)C; k <= D && i * k <= N; k++)
                {
                    if (j - i * k >= 0)
                    {
                        if ((long)dp1[j - i * k] != 0)
                        {
                            //dp2[j] += dp1[j - i * k] * chooseGroups(N - (j - i * k), i, k);
                            dp2[j] += dp1[j - i * k] * fact_arr[N - (j - i * k)] / (fact_arr[N - j] * fact_arr[i].Power(k) * fact_arr[k]);
                        }
                    }
                }
            }

            //Console.WriteLine(String.Join(",", dp2.Select(x => x.ToString())));
            dp1 = dp2;
            dp2 = new ModuloInt[N + 1].Select(x => new ModuloInt(0)).ToArray();
        }

        Console.WriteLine(dp1[N]);
    }
}