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)

using System;
using System.Collections.Generic;

public class CSharp{
    public static void Main(){
        Kyopuro chokudai = new Kyopuro();
	    chokudai.AtCoder();
	}
}

public class Kyopuro{
    
    int n, a, b, c, d;
    int[] fac, inv;
    int[][] invpow;
    
    public void AtCoder(){
        const int p = 1000000007;
        long tmp = 0;
        string[] s = Console.ReadLine().Split(' ');
        n = int.Parse(s[0]);
        a = int.Parse(s[1]);
        b = int.Parse(s[2]);
        c = int.Parse(s[3]);
        d = int.Parse(s[4]);
        fac = new int[n+1];
        inv = new int[n+1];
        invpow = new int[n+1][];
        for(int i=0; i<=n; i++) invpow[i] = new int[n+1];
        
        fac[0] = 1;
        for(int i=1; i<=n; i++){
            tmp = fac[i-1];
            tmp *= i;
            tmp %= p;
            fac[i] = (int)tmp;
        }
        for(int i=0; i<=n; i++){
            inv[i] = Mod.Pow(fac[i], p-2);
        }
        for(int i=1; i<=n; i++){
            for(int j=0; j<=n; j++){
                invpow[i][j] = Mod.Pow(inv[i], j);
            }
        }
        
        int[][] dp = new int[n+1][];
        for(int i=0; i<=n; i++) dp[i] = new int[n+1];
        for(int i=0; i<=n; i++) dp[i][0] = 1;
        for(int i=a; i<=b; i++){
            for(int j=1; j<=n; j++){
                dp[i][j] += dp[i-1][j];
                dp[i][j] %= p;
                for(int k=c; k<=d; k++){
                    if(j-i*k<0) break;
                    tmp = dp[i-1][j-i*k];
                    tmp = tmp * fac[j] % p;
                    tmp = tmp * inv[j-i*k] % p;
                    tmp = tmp * invpow[i][k] % p;
                    tmp = tmp * inv[k] % p;
                    dp[i][j] += (int)tmp;
                    dp[i][j] %= p;
                }
            }
        }
        long ans = 0;
        for(int i=c; i<=d; i++){
            ans += dp[i][n];
            ans %= p;
        }
        Console.WriteLine(dp[b][n]);
    }
    
    public static void DebugWL(object x){ System.Diagnostics.Debug.WriteLine(x); }
}

// Mod(int n)
// int[] fac: factors (1-indexed)
// int n: the length of fac

// int .Ncr(n,r): nCr
// int .Fac(n): n!%p
// int .Pow(n,m): n^m%p

public class Mod{
	const int p = 1000000007;

	public static int Pow(int n, int m){
		if(m==0) return 1;
		if(m==1) return n;
		long a = 1;
		if(m%2 == 0){
			a = Pow(n,m/2);
			a = a * a % p;
		}else{
			a = Pow(n,m/2);
			a = a * a % p;
			a = a * n % p;
		}
		return (int)a;
	}
}