Language: C++

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cerr<<(#v)<<":";for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;
#define printVS(vs) cerr<<(#vs)<<":"<<endl;for(auto(s):(vs)){cerr<<(s)<< endl;}
#define printVV(vv) cerr<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;}
#define printP(p) cerr<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cerr<<(#vp)<<":"<<endl;for(auto(p):(vp)){cerr<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cerr << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cerr << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

ll powmod (ll a, ll p) {
	ll ans = 1;
	ll mul = a;
	for (; p > 0; p >>= 1, mul = (mul * mul) % mod) {
		if ((p & 1) == 1) ans = (ans * mul) % mod;
	}
	return ans;
}

const int MAX_N = 5000;
ll fact[MAX_N];
ll revFact[MAX_N];
// !!!!!!!!SET FACT!!!!!!!
void setFact(int N) {
	fact[0] = 1;
	for (int i = 1; i < N; i++) {
		fact[i] = fact[i - 1] * i;
		fact[i] %= mod;
	}
	revFact[N - 1] = powmod(fact[N - 1], mod - 2);
	for (int i = N - 2; i >= 0; i--) {
		revFact[i] = revFact[i + 1] * (i + 1);
		revFact[i] %= mod;
	}
}

ll getC(int a, int b) {
    assert(a >= 0 && b >= 0);
	return (((fact[a] * revFact[b]) % mod) * revFact[a - b]) % mod;
}

ll revmod(ll x) {
	return powmod(x, mod - 2);
}

int n, a, b, c, d;
// using State = tuple<int, int, int>;
using State = int;
const int B = 10000;

unordered_map<State, int> dp;

inline int makeState(int i, int j, int k) {
    return B * B * i + B * j + k;
}

inline void getState(State state, int& i, int& j, int& k) {
    k = state % B;
    state /= B;
    j = state % B;
    state /= B;
    i = state % B;
}

// void printState(State state) {
//     int i, j, k;
//     tie(i, j, k) = state;
//     output(i, j, k);
// }

// 残りi人で、ちょうどj人のグループがk個できたときの場合の数
int f(State state) {
    // printState(state);
    if (dp.count(state)) return dp[state];
    int i, j, k;
    // tie(i, j, k) = state;
    getState(state, i, j, k);
    if (i == 0) {
        // printState(state);

        if (k >= c) {
            // output("A1");
            return 1;
        }
        else {
            // output("A2");
            return 0;
        }
    }
    if (i < j) {
        // printState(state);

        // output("B");
        return 0;
    }
    if (j > b) {
        // printState(state);

        // output("C");
        return 0;
    }
    if (k > d) {
        // printState(state);

        // output("D");
        return 0;
    }
    if (i % j != 0 && (j + 1) * c > i) {
        return 0;
    }
    // int &ret = dp[state];
    int ret = 0;
    // 人数jのグループを作る
    // 人数の同じグループは区別しない
    // if (k + 1 <= d && i - j >= 0) (ret += (f(State(i - j, j, k + 1)) * getC(i, j))) % mod;
    if (k + 1 <= d && i - j >= 0) {
        // printState(state);

        // output("E");
        int hoge = f(makeState(i - j, j, k + 1));
        (hoge *= getC(i, j)) %= mod;
        (hoge *= revmod(k + 1)) %= mod;
        ret += hoge;
        // (ret += (f(State(i - j, j, k + 1)) * getC(i, j))) % mod;
    }
    // 人数j+1に移行する
    // if (j + 1 <= b && c <= k && k <= d) (ret *= f(State(i, j + 1, 0))) %= mod;
    if (j + 1 <= b && (k == 0 || c <= k && k <= d)) {
        // printState(state);

        // output("F");
        (ret += f(makeState(i, j + 1, 0))) %= mod;
    }
    return dp[state] = ret;
}


signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    setFact(MAX_N);
    while (cin >> n >> a >> b >> c >> d) {
        // dp.clear();
        cout << f(makeState(n, a, 0)) << endl;
        // for (auto p : dp) {
        //     int i, j, k;
        //     tie(i, j, k) = p.first;
        //     output(i, j, k, p.second);
        // }
        // cout << "-------------" << endl;
    }
}
#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cerr<<(#v)<<":";for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;
#define printVS(vs) cerr<<(#vs)<<":"<<endl;for(auto(s):(vs)){cerr<<(s)<< endl;}
#define printVV(vv) cerr<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;}
#define printP(p) cerr<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cerr<<(#vp)<<":"<<endl;for(auto(p):(vp)){cerr<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cerr << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cerr << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

ll powmod (ll a, ll p) {
	ll ans = 1;
	ll mul = a;
	for (; p > 0; p >>= 1, mul = (mul * mul) % mod) {
		if ((p & 1) == 1) ans = (ans * mul) % mod;
	}
	return ans;
}

const int MAX_N = 5000;
ll fact[MAX_N];
ll revFact[MAX_N];
// !!!!!!!!SET FACT!!!!!!!
void setFact(int N) {
	fact[0] = 1;
	for (int i = 1; i < N; i++) {
		fact[i] = fact[i - 1] * i;
		fact[i] %= mod;
	}
	revFact[N - 1] = powmod(fact[N - 1], mod - 2);
	for (int i = N - 2; i >= 0; i--) {
		revFact[i] = revFact[i + 1] * (i + 1);
		revFact[i] %= mod;
	}
}

ll getC(int a, int b) {
    assert(a >= 0 && b >= 0);
	return (((fact[a] * revFact[b]) % mod) * revFact[a - b]) % mod;
}

ll revmod(ll x) {
	return powmod(x, mod - 2);
}

int n, a, b, c, d;
// using State = tuple<int, int, int>;
using State = int;
const int B = 10000;

unordered_map<State, int> dp;

inline int makeState(int i, int j, int k) {
    return B * B * i + B * j + k;
}

inline void getState(State state, int& i, int& j, int& k) {
    k = state % B;
    state /= B;
    j = state % B;
    state /= B;
    i = state % B;
}

// void printState(State state) {
//     int i, j, k;
//     tie(i, j, k) = state;
//     output(i, j, k);
// }

// 残りi人で、ちょうどj人のグループがk個できたときの場合の数
int f(State state) {
    // printState(state);
    if (dp.count(state)) return dp[state];
    int i, j, k;
    // tie(i, j, k) = state;
    getState(state, i, j, k);
    if (i == 0) {
        // printState(state);

        if (k >= c) {
            // output("A1");
            return 1;
        }
        else {
            // output("A2");
            return 0;
        }
    }
    if (i < j) {
        // printState(state);

        // output("B");
        return 0;
    }
    if (j > b) {
        // printState(state);

        // output("C");
        return 0;
    }
    if (k > d) {
        // printState(state);

        // output("D");
        return 0;
    }
    if (i % j != 0 && (j + 1) * c > i) {
        return 0;
    }
    // int &ret = dp[state];
    int ret = 0;
    // 人数jのグループを作る
    // 人数の同じグループは区別しない
    // if (k + 1 <= d && i - j >= 0) (ret += (f(State(i - j, j, k + 1)) * getC(i, j))) % mod;
    if (k + 1 <= d && i - j >= 0) {
        // printState(state);

        // output("E");
        int hoge = f(makeState(i - j, j, k + 1));
        (hoge *= getC(i, j)) %= mod;
        (hoge *= revmod(k + 1)) %= mod;
        ret += hoge;
        // (ret += (f(State(i - j, j, k + 1)) * getC(i, j))) % mod;
    }
    // 人数j+1に移行する
    // if (j + 1 <= b && c <= k && k <= d) (ret *= f(State(i, j + 1, 0))) %= mod;
    if (j + 1 <= b && (k == 0 || c <= k && k <= d)) {
        // printState(state);

        // output("F");
        (ret += f(makeState(i, j + 1, 0))) %= mod;
    }
    return dp[state] = ret;
}


signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    setFact(MAX_N);
    while (cin >> n >> a >> b >> c >> d) {
        // dp.clear();
        cout << f(makeState(n, a, 0)) << endl;
        // for (auto p : dp) {
        //     int i, j, k;
        //     tie(i, j, k) = p.first;
        //     output(i, j, k, p.second);
        // }
        // cout << "-------------" << endl;
    }
}


Compiler output:
a.cc:196:13: error: redefinition of 'void output()'
  196 | inline void output(){ cerr << endl; }
      |             ^~~~~~
a.cc:19:13: note: 'void output()' previously defined here
   19 | inline void output(){ cerr << endl; }
      |             ^~~~~~
a.cc:198:13: error: redefinition of 'template<class First, class ... Rest> void output(const First&, const Rest& ...)'
  198 | inline void output(const First& first, const Rest&... rest) {
      |             ^~~~~~
a.cc:21:13: note: 'template<class First, class ... Rest> void output(const First&, const Rest& ...)' previously declared here
   21 | inline void output(const First& first, const Rest&... rest) {
      |             ^~~~~~
a.cc:208:11: error: redefinition of 'const long long int inf'
  208 | const int inf = 1ll << 60;
      |           ^~~
a.cc:31:11: note: 'const long long int inf' previously defined here
   31 | const int inf = 1ll << 60;
      |           ^~~
a.cc:209:11: error: redefinition of 'const long long int mod'
  209 | const int mod = 1e9 + 7;
      |           ^~~
a.cc:32:11: note: 'const long long int mod' previously defined here
   32 | const int mod = 1e9 + 7;
      |           ^~~
a.cc:212:4: error: redefinition of 'll powmod(ll, ll)'
  212 | ll powmod (ll a, ll p) {
      |    ^~~~~~
a.cc:35:4: note: 'll powmod(ll, ll)' previously defined here
   35 | ll powmod (ll a, ll p) {
      |    ^~~~~~
a.cc:221:11: error: redefinition of 'const long long int MAX_N'
  221 | const int MAX_N = 5000;
      |           ^~~~~
a.cc:44:11: note: 'const long long int MAX_N' previously defined here
   44 | const int MAX_N = 5000;
      |           ^~~~~
a.cc:222:4: error: redefinition of 'll fact [5000]'
  222 | ll fact[MAX_N];
      |    ^~~~
a.cc:45:4: note: 'll fact [5000]' previously declared here
   45 | ll fact[MAX_N];
      |    ^~~~
a.cc:223:4: error: redefinition of 'll revFact [5000]'
  223 | ll revFact[MAX_N];
      |    ^~~~~~~
a.cc:46:4: note: 'll revFact [5000]' previously declared here
   46 | ll revFact[MAX_N];
      |    ^~~~~~~
a.cc:225:6: error: redefinition of 'void setFact(long long int)'
  225 | void setFact(int N) {
      |      ^~~~~~~
a.cc:48:6: note: 'void setFact(long long int)' previously defined here
   48 | void setFact(int N) {
      |      ^~~~~~~
a.cc:238:4: error: redefinition of 'll getC(long long int, long long int)'
  238 | ll getC(int a, int b) {
      |    ^~~~
a.cc:61:4: note: 'll getC(long long int, long long int)' previously defined here
   61 | ll getC(int a, int b) {
      |    ^~~~
a.cc:243:4: error: redefinition of 'll revmod(ll)'
  243 | ll revmod(ll x) {
      |    ^~~~~~
a.cc:66:4: note: 'll revmod(ll)' previously defined here
   66 | ll revmod(ll x) {
      |    ^~~~~~
a.cc:247:5: error: redefinition of 'long long int n'
  247 | int n, a, b, c, d;
      |     ^
a.cc:70:5: note: 'long long int n' previously declared here
   70 | int n, a, b, c, d;
      |     ^
a.cc:247:8: error: redefinition of 'long long int a'
  247 | int n, a, b, c, d;
      |        ^
a.cc:70:8: note: 'long long int a' previously declared here
   70 | int n, a, b, c, d;
      |        ^
a.cc:247:11: error: redefinition of 'long long int b'
  247 | int n, a, b, c, d;
      |           ^
a.cc:70:11: note: 'long long int b' previously declared here
   70 | int n, a, b, c, d;
      |           ^
a.cc:247:14: error: redefinition of 'long long int c'
  247 | int n, a, b, c, d;
      |              ^
a.cc:70:14: note: 'long long int c' previously declared here
   70 | int n, a, b, c, d;
      |              ^
a.cc:247:17: error: redefinition of 'long long int d'
  247 | int n, a, b, c, d;
      |                 ^
a.cc:70:17: note: 'long long int d' previously declared here
   70 | int n, a, b, c, d;
      |                 ^
a.cc:250:11: error: redefinition of 'const long long int B'
  250 | const int B = 10000;
      |           ^
a.cc:73:11: note: 'const long long int B' previously defined here
   73 | const int B = 10000;
      |           ^
a.cc:252:27: error: redefinition of 'std::unordered_map<long long int, long long int> dp'
  252 | unordered_map<State, int> dp;
      |                           ^~
a.cc:75:27: note: 'std::unordered_map<long long int, long long int> dp' previously declared here
   75 | unordered_map<State, int> dp;
      |                           ^~
a.cc:254:12: error: redefinition of 'long long int makeState(long long int, long long int, long long int)'
  254 | inline int makeState(int i, int j, int k) {
      |            ^~~~~~~~~
a.cc:77:12: note: 'long long int makeState(long long int, long long int, long long int)' previously defined here
   77 | inline int makeState(int i, int j, int k) {
      |            ^~~~~~~~~
a.cc:258:13: error: redefinition of 'void getState(State, long long int&, long long int&, long long int&)'
  258 | inline void getState(State state, int& i, int& j, int& k) {
      |             ^~~~~~~~
a.cc:81:13: note: 'void getState(State, long long int&, long long int&, long long int&)' previously defined here
   81 | inline void getState(State state, int& i, int& j, int& k) {
      |             ^~~~~~~~
a.cc:273:5: error: redefinition of 'long long int f(State)'
  273 | int f(State state) {
      |     ^
a.cc:96:5: note: 'long long int f(State)' previously defined here
   96 | int f(State state) {
      |     ^
a.cc:339:8: error: redefinition of 'int main()'
  339 | signed main() {
      |        ^~~~
a.cc:162:8: note: 'int main()' previously defined here
  162 | signed main() {
      |        ^~~~