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

#![allow(unused_imports, unused_variables, dead_code)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

trait InputValue {
    fn parse(s: &str) -> Self;
}

fn read<T: InputValue>() -> T {
    let mut buf = String::new();
    let _ = stdin().read_line(&mut buf);
    T::parse(&buf.trim())
}

fn readnc<T: InputValue>() -> Vec<T> {
    let mut vec = vec![];
    let line: String = read();
    for token in line.split_whitespace() {
        vec.push(T::parse(token));
    }
    vec
}

fn readn<T: InputValue>(n: usize) -> Vec<T> {
    let mut vec = vec![];
    for _ in 0..n {
        vec.push(read());
    }
    vec
}

macro_rules! parse_single_value {
        ($($t:ty),*) => {
            $(
                impl InputValue for $t {
                    fn parse(s: &str) -> $t { s.parse().unwrap() }
                }
            )*
    	}
    }
parse_single_value!(i32, i64, f32, f64, usize, String);

macro_rules! parse_tuple {
    	($($t:ident),*) => {
    		impl<$($t),*> InputValue for ($($t),*) where $($t: InputValue),* {
    			fn parse(s: &str) -> ($($t),*) {
    				let mut tokens = s.split_whitespace();
    				let t = ($($t::parse(tokens.next().unwrap())),*);
    				t
    			}
    		}
    	}
    }
parse_tuple!(A, B, C, D, E);

// ===

const MOD: i64 = 1000000007;

fn powmod(a: i64, p: i64, m: i64) -> i64 {
    let mut ret = 1i64;
    let mut aa = a;
    let mut pp = p;
    while pp >= 1 {
        if pp & 1 == 1 {
            ret *= aa;
            ret %= m;
        }
        aa = aa * aa % m;
        pp >>= 1;
    }
    ret
}


fn main() {
    let (n, a, b, c, d): (usize, usize, usize, usize, usize) = read();

    let mut comb: Vec<Vec<i64>> = vec![vec![0; n+1]; n+1];
    for i in 0..(n+1) {
        comb[i][0] = 1;
        comb[i][i] = 1;
        for j in 1..i {
            comb[i][j] = comb[i-1][j-1] + comb[i-1][j];
            comb[i][j] %= MOD;
        }
    }

    let mut invfact: Vec<i64> = vec![0; n+1];
    invfact[0] = 1;

    let mut fact: i64 = 1;
    for i in 1..(n+1) {
        fact = fact * i as i64 % MOD;
        invfact[i] = powmod(fact, MOD-2, MOD);
    }


    let mut dp: Vec<Vec<i64>> = vec![vec![0; n+1]; n+1];
    dp[0][0] = 1;

    let diff = b - a;
    for g in 0..(diff+1) {
        let per = a + g;
        for i in 0..(n+1) {
            let base = dp[g][i];
            if base == 0 {
                continue;
            }
            dp[g+1][i] += base;
            dp[g+1][i] %= MOD;

            let mut left = n - i;
            let mut way: i64 = 1;
            for cnt in 1..(d+1) {
                let to = i + per * cnt;
                if to > n {
                    break;
                }

                way *= comb[left][per];
                way %= MOD;
                left -= per;

                if c <= cnt {
                    dp[g+1][to] += base * way % MOD * invfact[cnt] % MOD;
                    dp[g+1][to] %= MOD;
                }
            }
        }
    }
    println!("{:?}", dp[diff+1][n]);
}