Getting ready for VK Fest 2021, you prepared a table with n rows and nn columns, and filled each cell of this table with some event related with the festival that could either happen or not: for example, whether you will win a prize on the festival, or whether it will rain.
Forecasting algorithms used in VK have already estimated the probability for each event to happen. Event in row ii and column jj will happen with probability ai,j⋅10−4. All of the events are mutually independent.
Let’s call the table winning if there exists a line such that all n events on it happen. The line could be any horizontal line (cells (i,1),(i,2),…,(i,n) for some ii), any vertical line (cells (1,j),(2,j),…,(n,j) for some j), the main diagonal (cells (1,1),(2,2),…,(n,n)), or the antidiagonal (cells (1,n),(2,n−1),…,(n,1)).
Find the probability of your table to be winning, and output it modulo 31607 (see Output section).
Input
The first line contains a single integer n(2≤n≤21) — the dimensions of the table.
The i-th of the next n lines contains n integers ai,1,ai,2,…,ai,n (0<ai,j<104). The probability of event in cell (i,j) to happen is ai,j⋅10−4.
Output
Print the probability that your table will be winning, modulo 31607.
Formally, let M=31607. It can be shown that the answer can be expressed as an irreducible fraction qp, where p and q are integers and q≢0(modM). Output the integer equal to p⋅q−1modM. In other words, output such an integer x that 0≤x<M and x⋅q≡p(modM).
Examples
input
1 2 3
2 5000 5000 5000 5000
output
1
5927
input
1 2 3
2 2500 6000 3000 4000
output
1
24812
input
1 2 3 4
3 1000 2000 3000 4000 5000 6000 7000 8000 9000
output
1
25267
Tutorial
Let l1,l2,…,l2n+2 denote the 2n+2 possible lines that can be formed. Let Li denote the event that line li is formed, and Li denote the event that line li is not formed (i.e., P(Li)+P(Li)=1).
Let’s find the probability that our table is not winning. It is equal to P(L1∩L2∩…∩L2n+2).
We can apply this formula recursively. Specifically, we can make a function f(i,S), where S=s1,s2,…,sk is a subset of 1,2,…,i−1, which calculates P(Li∩Li+1∩…∩L2n+2∣Ls1∩…∩Lsk). For i=2n+3, f(i,S)=1, and for i≤2n+2 we can generalize the formula above as follows:
f(i,S)=f(i+1,S)−f(i+1,S∪i)⋅P(Li∣Ls1∩…∩Lsk).
Here, P(Li∣Ls1∩…∩Lsk) is the probability that line li is formed given that lines ls1,…,lsk are formed. This is equal to the product of probabilities of all cells belonging to li which do not belong to any of ls1,…,lsk.
The answer to the problem, i.e. the probability that our table is winning, is 1−f(1,{}).
This allows us to implement an O(2n⋅n) solution, which is too slow. In fact, this solution is equivalent to applying inclusion-exclusion principle.
To optimize this solution, note that once it becomes easy to calculate f(i,S), we don’t have to make any more recursive calls. Why would it become easy to calculate f(i,S) though?
Let’s order the lines in such a way that ln+3,ln+4,…,l2n+2 are the horizontal lines of the table. Consider a call of the form f(n+3,S). This call is basically asking: “what is the probability that none of lines ln+3,ln+4,…,l2n+2 are formed, given that lines ls1,…,Lsk are formed?”.
Note that the horizontal lines are independent, and we can actually answer this question in O(n2). Specifically, for any horizontal line, the probability that it is not formed is 1 minus the product of probabilities of all its cells not belonging to any of ls1,…,lsk. The overall value of f(n+3,S) is the product of probabilities for individual horizontal lines.
This way, we have built an O(2n⋅n2) solution. This might be fast enough depending on your implementation, but there are at least two ways to optimize it to O(2n⋅n):
The first way is to maintain the products of probabilities of untouched cells for horizontal lines on the fly. For simplicity, assume that l1 and l2 are the diagonal lines. For each i=3,4,…,n+2, after we process (vertical) line li, we can update the products for all horizontal lines with the cells of li (in O(n), make a recursive call, and roll back the updates (in O(n) again). Once we get to i=n+3, instead of going through every cell of the table in O(n2), we can just multiply n horizontal line products in O(n).
The second way is to define g(i,mask)(1≤i≤n;mask is a bitmask of size n) to be the product of ai,j over all j belonging to mask. All values of g can be calculated in O(2n⋅n) using dynamic programming: g(i,0)=1, and g(i,mask)=g(i,mask⊕2j)⋅ai,j, where jj is any bit set in mask. When we arrive at a f(n+3,S) call, for each of the n horizontal lines, instead of going through its cells, we can construct the mask of cells the values in which we want to multiply, and use the corresponding value of g in O(1).