#include #include using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; using mint = modint998244353; const int mod = 998244353; //const int INF = 1e9; //const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n) - 1; i >= 0; --i) #define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } vector> f(vector>a) { vector dp(9, vector(5, vector(34, 0))); dp[0][0][0] = 1; for (auto p : a) { vector sdp(9, vector(5, vector(34, 0))); rep(i, 9)rep(j, 5)rep(k, 34) { sdp[i][j][k] += dp[i][j][k]; int ni = i + 1; if (ni == 9)continue; int nj = j + p.first; int nk = k + p.second; if (nj > 4)continue; if (nk > 33)continue; sdp[ni][nj][nk] += dp[i][j][k]; } swap(dp, sdp); } return dp[8]; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector>a(n); rep(i, n)cin >> a[i].first; rep(i, n)cin >> a[i].second; vector>b, c, d; rep(i, n) { int p = a[i].first; if ("X" == a[i].second) { int q = 0; b.emplace_back(p, q); } else if ('X' == a[i].second.back()) { a[i].second.pop_back(); int q = stoi(a[i].second); c.emplace_back(p, q); } else { int q = stoi(a[i].second); d.emplace_back(p, q); } } auto dp = f(d); mint ans = 0; for (auto p : b) { // X int x = 4 - p.first; int y = 33 - p.second; ans += dp[x][y]; } cout << ans.val() << endl; // つまらない問題 return 0; }