/* This solution represents states as a (s+1) x (c+1) matrix, where the row is the number of solvers and the column is the number of coders. Two transition matrices are used to advance the state to the next time. A left transition matrix (s+1) x (s+1) does row operations to move solvers to the next time. A right transition matrix (c+1) x (c+1) does column operations to move coders to the next time. Powers of the transition matrices jump the state ahead to the next masking time. Runtime O(p * log n * (s^3 + c^3)) -- 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; const ll MOD = 1e9+7; vvll matmul(const vvll&a, const vvll&b) { int r = a.size(); int p = b.size(); int c = b[0].size(); assert (p == a[0].size()); vvll ans(r, vll(c)); rep(i,0,r) rep(k,0,p) rep(j,0,c) ans[i][j] = (ans[i][j] + a[i][k] * b[k][j]) % MOD; return ans; } vvll matexp(vvll a, ll pow) { int n = a.size(); vvll ans(n, vll(n)); rep(i,0,n) ans[i][i] = 1; while (pow) { if (pow&1) ans = matmul(ans, a); a = matmul(a, a); pow >>= 1; } return ans; } int main() { ll n, p, s, c; cin >> n >> p >> s >> c; vvll left_trans(s+1, vll(s+1)); rep(i,0,s+1) { if (i>0) left_trans[i-1][i] = i; left_trans[i][i] = 1; if (i+1 <= s) left_trans[i+1][i] = s-i; } vvll right_trans(c+1, vll(c+1)); rep(j,0,c+1) { if (j>0) right_trans[j][j-1] = j; right_trans[j][j] = 1; if (j+1 <= c) right_trans[j][j+1] = c-j; } vvll state((s+1), vll(c+1)); state[0][0] = 1; ll curtime = 0; while (p--) { ll time; char side; int sl, cl, tl; cin >> time >> side >> sl >> cl >> tl; --time; state = matmul(matexp(left_trans, time-curtime), state); state = matmul(state, matexp(right_trans, time-curtime)); curtime = time; // apply mask here if (side == 'B') { rep(i,0,s+1) rep(j,0,c+1) if (i < sl || j < cl || i+j < tl) state[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) state[i][j] = 0; } } state = matmul(matexp(left_trans, n-1-curtime), state); state = matmul(state, matexp(right_trans, n-1-curtime)); cout << state[0][0] << endl; return 0; }