use proconio::input; fn main() { input! { n:usize, a:[usize;n], b:[String;n], } let mut dp = vec![vec![vec![vec![vec![ModInt::::new(0); 2]; 34]; 5]; 10]; n + 1]; dp[0][0][0][0][0] = ModInt::new(1); for i in 0..n { for j in 0..10 { for u in 0..5 { for d in 0..34 { for k in 0..2 { dp[i + 1][j][u][d][k] = dp[i + 1][j][u][d][k] + dp[i][j][u][d][k]; if j >= 9 { continue; } if b[i].parse::().is_ok() { let di = b[i].parse::().unwrap(); if u + a[i] > 4 || d + di > 33 { continue; } dp[i + 1][j + 1][u + a[i]][d + di][k] = dp[i + 1][j + 1][u + a[i]][d + di][k] + dp[i][j][u][d][k]; } if b[i].starts_with('X') && k == 0 { if u + a[i] > 4 { continue; } dp[i + 1][j + 1][u + a[i]][d][1] = dp[i + 1][j + 1][u + a[i]][d][1] + dp[i][j][u][d][k]; } } } } } } let ans = dp[n][9][4][33][1]; println!("{}", ans); } const MOD: usize = 998244353; use modint::*; mod modint { use std::fmt; use std::ops; #[derive(Copy, Clone, PartialEq, Eq)] pub struct ModInt { pub val: usize, } impl ModInt { pub fn new(val: usize) -> Self { Self { val: val % MOD } } pub fn pow(mut self, mut e: usize) -> Self { let mut res = Self::new(1); while 0 < e { if e & 1 != 0 { res *= self; } self *= self; e >>= 1; } res } } impl From for ModInt { fn from(value: usize) -> Self { Self { val: value % MOD } } } impl fmt::Display for ModInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.val) } } impl fmt::Debug for ModInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.val) } } impl ops::Neg for ModInt { type Output = Self; fn neg(self) -> Self::Output { Self { val: (MOD - self.val) % MOD, } } } impl ops::Add for ModInt { type Output = Self; fn add(self, rhs: Self) -> Self::Output { Self { val: (self.val + rhs.val) % MOD, } } } impl ops::AddAssign for ModInt { fn add_assign(&mut self, rhs: Self) { *self = *self + rhs; } } impl ops::Mul for ModInt { type Output = Self; fn mul(self, rhs: Self) -> Self::Output { Self { val: self.val * rhs.val % MOD, } } } impl ops::MulAssign for ModInt { fn mul_assign(&mut self, rhs: Self) { *self = *self * rhs; } } impl ops::Sub for ModInt { type Output = Self; fn sub(mut self, rhs: Self) -> Self::Output { if self.val < rhs.val { self.val += MOD; } Self { val: (self.val - rhs.val) % MOD, } } } impl ops::SubAssign for ModInt { fn sub_assign(&mut self, rhs: Self) { if self.val < rhs.val { self.val += MOD; } *self = *self - rhs; } } impl ops::Div for ModInt { type Output = Self; fn div(self, rhs: Self) -> Self { assert!(rhs.val != 0); self * rhs.pow(MOD - 2) } } impl ops::DivAssign for ModInt { fn div_assign(&mut self, rhs: Self) { *self = *self / rhs } } }