from collections import * import sys import heapq import bisect import itertools from functools import lru_cache from types import GeneratorType from fractions import Fraction import math import copy import random # sys.setrecursionlimit(int(1e7)) # @lru_cache(maxsize=None) # CPython特化 # @bootstrap # PyPy特化(こっちのほうが速い) yield dfs(), yield Noneを忘れずに def bootstrap(f, stack=[]): # yield def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc dxdy1 = ((0, 1), (0, -1), (1, 0), (-1, 0)) # 上下右左 dxdy2 = ( (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (1, -1), (-1, 1), ) # 8方向すべて dxdy3 = ((0, 1), (1, 0)) # 右 or 下 dxdy4 = ((1, 1), (1, -1), (-1, 1), (-1, -1)) # 斜め INF = float("inf") _INF = 1 << 60 MOD = 998244353 mod = 998244353 MOD2 = 10**9 + 7 mod2 = 10**9 + 7 # memo : len([a,b,...,z])==26 # memo : 2^20 >= 10^6 # 小数の計算を避ける : x/y -> (x*big)//y ex:big=10**9 # @:小さい文字, ~:大きい文字,None: 空の文字列 # ユークリッドの互除法:gcd(x,y)=gcd(x,y-x) # memo : d 桁以下の p 進表記を用いると p^d-1 以下のすべての # 非負整数を表現することができる # memo : (X,Y) -> (X+Y,X−Y) <=> 点を原点を中心に45度回転し、√2倍に拡大 # memo : (x,y)のx正から見た偏角をラジアンで(-πからπ]: math.atan2(y, x) # memo : a < bのとき ⌊a⌋ ≦ ⌊b⌋ input = lambda: sys.stdin.readline().rstrip() mi = lambda: map(int, input().split()) li = lambda: list(mi()) ii = lambda: int(input()) py = lambda: print("Yes") pn = lambda: print("No") pf = lambda: print("First") ps = lambda: print("Second") # 階乗 & 逆元計算 factorial = [1] inverse = [1] for i in range(1, 10): factorial.append(factorial[-1] * i % MOD) inverse.append(pow(factorial[-1], MOD - 2, MOD)) # 組み合わせ計算 def nCr(n, r): if n < r or r < 0: return 0 elif r == 0: return 1 tmp = 1 for x in range(n, n - r, -1): tmp *= x tmp %= MOD return tmp * inverse[r] % MOD O = [li() for _ in range(5)] X = [li() for _ in range(5)] 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): for j in range(7, -1, -1): for x in range(4, -1, -1): for y in range(33, -1, -1): for r in range(1, min(9, O[a][b] + 1)): if x + r * a <= 4 and y + r * b <= 33 and j + r <= 8: dp[j + r][x + r * a][y + r * b] += dp[j][x][y] * nCr( O[a][b], r ) dp[j + r][x + r * a][y + r * b] %= MOD ans = 0 for a in range(5): for b in range(1): ans += dp[8][4 - a][33] * X[a][b] ans %= MOD print(ans)