## https://yukicoder.me/problems/11039 MOD = 998244353 class CombinationCalculator: """ modを考慮したPermutation, Combinationを計算するためのクラス """ def __init__(self, size, mod): self.mod = mod self.factorial = [0] * (size + 1) self.factorial[0] = 1 for i in range(1, size + 1): self.factorial[i] = (i * self.factorial[i - 1]) % self.mod self.inv_factorial = [0] * (size + 1) self.inv_factorial[size] = pow(self.factorial[size], self.mod - 2, self.mod) for i in reversed(range(size)): self.inv_factorial[i] = ((i + 1) * self.inv_factorial[i + 1]) % self.mod def calc_combination(self, n, r): if n < 0 or n < r or r < 0: return 0 if r == 0 or n == r: return 1 ans = self.inv_factorial[n - r] * self.inv_factorial[r] ans %= self.mod ans *= self.factorial[n] ans %= self.mod return ans def calc_permutation(self, n, r): if n < 0 or n < r: return 0 ans = self.inv_factorial[n - r] ans *= self.factorial[n] ans %= self.mod return ans def calc_combination(n, r , combi): ans = 1 if r < 0 or n < r: return 0 if r == 0 or n == r: return 1 for i in range(r): ans *= (n - i) % MOD ans %= MOD ans *= combi.inv_factorial[r] ans %= MOD return ans def main(): O = [] for _ in range(5): O.append(list(map(int, input().split()))) X = [] for _ in range(5): X.append(list(map(int, input().split()))) combi = CombinationCalculator(10, MOD) # dpをつかう dp = [[[0] * 34 for _ in range(5)] for _ in range(9)] dp[0][0][0] = 1 for a in range(5): for b in range(34): x = O[a][b] new_dp = [[[0] * 34 for _ in range(5)] for _ in range(9)] 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 x > 0: for m in range(1, min(8, x) + 1): a_ = a * m b_ = b * m if a_score + a_ <= 4 and b_ + b_score <= 33 and n + m <= 8: c = calc_combination(x, m, combi) new_dp[n + m][a_score + a_][b_ + b_score] += (dp[n][a_score][b_score] * c) % MOD new_dp[n + m][a_score + a_][b_ + b_score] %= MOD dp = new_dp # Xを使う answer = 0 for ao in range(5): for bo in range(34): val = dp[-1][ao][bo] for ax in range(5): for bx in range(34): if ax + ao == 4 and bx + bo == 33: if bx == 0: answer += (val * X[ax][bx]) % MOD answer %= MOD else: if ax + ao >= bx + bo - 4: answer += (val * X[ax][bx]) % MOD answer %= MOD print(answer) if __name__ == "__main__": main()