## https://yukicoder.me/problems/no/2783 MOD = 998244353 def main(): N = int(input()) A = list(map(int, input().split())) B = input().split() # 8枚目を選ぶ dp = [[[0 for _ in range(34)] for _ in range(5)] for _ in range(9)] dp[0][0][0] = 1 for i in range(N): if "X" in B[i]: continue new_dp = [[[0 for _ in range(34)] for _ in range(5)] for _ in range(9)] a = A[i] b = int(B[i]) for n in range(9): for a_score in range(5): for b_score in range(34): # 選ばないケース new_dp[n][a_score][b_score] += dp[n][a_score][b_score] new_dp[n][a_score][b_score] %= MOD # 選ぶケース if n + 1 <= 8 and a + a_score <= 4 and b + b_score <= 33: if b == 0: new_dp[n + 1][a + a_score][b + b_score] += dp[n][a_score][b_score] new_dp[n + 1 ][a + a_score][b + b_score] %= MOD else: new_dp[n + 1][a + a_score][b + b_score] += dp[n][a_score][b_score] new_dp[n + 1 ][a + a_score][b + b_score] %= MOD dp = new_dp targets= [] for i in range(N): if "X" in B[i]: targets.append((A[i], B[i])) # 9枚目を選ぶ answer = 0 for a, b in targets: b_ = b[:-1] if len(b_) == 0: b_ = 0 else: b_ = int(b_) for a_score in range(5): for b_score in range(34): if a + a_score == 4 and b_ + b_score == 33: if b_ == 0: answer += dp[-1][a_score][b_score] answer %= MOD else: if a + a_score >= b_score + b_ - 4: answer += dp[-1][a_score][b_score] answer %= MOD print(answer) if __name__ == "__main__": main()