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 (Python):
n, a, b, c, d = map(int, raw_input().split())
MOD = 10**9+7
fact = [1]*(n+1)
frev = [1]*(n+1)

rcd = range(c, d+1)

dp = [0]*(n+1)
dp[0] = 1

for i in xrange(1, n+1):
    v = fact[i] = (fact[i-1] * i) % MOD
    frev[i] = pow(v, MOD-2, MOD)
Y = [None]*(n+1)
for g in xrange(a, min(n/c, b)+1):
    p = c*g; q = d*g
    y = frev[g]
    Y[p:min(q,n)+1:g] = (pow(y, j, MOD)*frev[j] for j in xrange(c, min(d, n/g)+1))
    dp[p:] = (dp[i] + frev[n-i]*sum(dp[i-j] * fact[n-i+j] * Y[j] for j in xrange(p, min(q, i)+1, g)) % MOD for i in xrange(p, n+1))
print dp[n]%MOD

Solution (Python):
n, a, b, c, d = map(int, raw_input().split())
MOD = 10**9+7
fact = [1]*(n+1)
frev = [1]*(n+1)

rcd = range(c, d+1)

dp = [0]*(n+1)
dp[0] = 1

for i in xrange(1, n+1):
    v = fact[i] = (fact[i-1] * i) % MOD
    frev[i] = pow(v, MOD-2, MOD)
if a==c==1 and b==d==n:
    Y = [None]*(n+1)
    for g in xrange(1, n+1):
        y = frev[g]
        Y[g:n+1:g] = (pow(y, j, MOD)*frev[j] for j in xrange(1, n/g+1))
        dp[g:] = (dp[i] + frev[n-i]*sum(dp[i-j] * fact[n-i+j] * Y[j] for j in xrange(g, i+1, g)) % MOD for i in xrange(g, n+1))
else:
    Y = [None]*(d+1-c)
    for g in xrange(a, min(n/c, b)+1):
        p = c*g
        y = frev[g]
        Y[:min(d,n/g)+1-c] = (pow(y, j, MOD)*frev[j] for j in xrange(c, min(d, n/g)+1))
        dp[p:] = (dp[i] + frev[n-i]*sum(dp[i-g*j] * fact[n-i+g*j] * Y[j-c] for j in xrange(c, min(d, i/g)+1)) % MOD for i in xrange(p, n+1))
print dp[n]%MOD

Solution (Python):
n, a, b, c, d = map(int, raw_input().split())
dp = [0]*(n+1)
dp[0] = 1

MOD = 10**9+7
fact = [1]*(n+1)
frev = [1]*(n+1)

rn = range(n, -1, -1)

for i in xrange(1, n+1):
    fact[i] = (fact[i-1] * i) % MOD
    frev[i] = pow(fact[i], MOD-2, MOD)
for g in xrange(a, b+1):
    y = frev[g]
    for i in rn:
        x = frev[n-i]
        dp[i] = (dp[i] + sum(dp[i-g*j] * fact[n-i+g*j] * x * pow(y, j, MOD) * frev[j] for j in xrange(c, min(d, i/g)+1))) % MOD
print dp[n]