/* This solution does DP over time and state. The state is the number of students of each type in group B. States that don't result in the challenge problems being solved are masked out. Runtime O(n * s * c) Runtime error because it requires too much memory. -- Lawry */ #include #define rep(i,j,k) for (int i=j;i<(k);++i) using namespace std; typedef long long ll; typedef vector vll; typedef vector vvll; typedef vector vvvll; const int MOD = 1e9+7; int main() { int n, p, s, c; cin >> n >> p >> s >> c; vector>> filters(n); while (p--) { int t; char r; int sl, cl, tl; cin >> t >> r >> sl >> cl >> tl; --t; filters[t].push_back({r=='B', sl, cl, tl}); } vvvll dp(n, vvll(s+1, vll(c+1))); dp[0][0][0] = 1; rep(time,1,n) { // mask now vvll& cur = dp[time]; vvll& prev = dp[time-1]; rep(i,0,s+1) rep(j,0,c+1) { rep(di,-1,2) rep(dj,-1,2) { if (i + di < 0 || i+di > s || j+dj < 0 || j+dj > c) continue; ll add = prev[i][j]; if (di<0) add *= i; else if (di>0) add *= (s-i); if (dj<0) add *= j; else if (dj>0) add *= (c-j); cur[i+di][j+dj] = (cur[i+di][j+dj] + add) % MOD; } } for (auto [side, sl, cl, tl] : filters[time]) { if (side) { // on B rep(i,0,s+1) rep(j,0,c+1) if (i < sl || j < cl || i+j < tl) cur[i][j] = 0; } else { rep(i,0,s+1) rep(j,0,c+1) if (s-i < sl || c-j < cl || s-i+c-j < tl) cur[i][j] = 0; } } } cout << dp[n-1][0][0] << endl; return 0; }