0%

Bingo

Bingo

time limit per test: 7 seconds

memory limit per test: 512 megabytes

Articles

Getting ready for VK Fest 2021, you prepared a table with nn 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,j104a_{i,j}⋅10^{−4}. All of the events are mutually independent.

Let’s call the table winning if there exists a line such that all nn events on it happen. The line could be any horizontal line (cells (i,1),(i,2),,(i,n)(i,1),(i,2),…,(i,n) for some ii), any vertical line (cells (1,j),(2,j),,(n,j)(1,j),(2,j),…,(n,j) for some jj), the main diagonal (cells (1,1),(2,2),,(n,n)(1,1),(2,2),…,(n,n)), or the antidiagonal (cells (1,n),(2,n1),,(n,1)(1,n),(2,n−1),…,(n,1)).

Find the probability of your table to be winning, and output it modulo 3160731607 (see Output section).

Input

The first line contains a single integer n(2n21)n (2≤n≤21) — the dimensions of the table.

The ii-th of the next nn lines contains nn integers ai,1,ai,2,,ai,nai,1,ai,2,…,ai,n (0<ai,j<1040<a_{i,j}<10^4). The probability of event in cell (i,j)(i,j) to happen is ai,j104a_{i,j}⋅10^{−4}.

Output

Print the probability that your table will be winning, modulo 3160731607.

Formally, let M=31607M=31607. It can be shown that the answer can be expressed as an irreducible fraction pq\frac{p}{q}, where pp and qq are integers and q≢0(modM). Output the integer equal to pq1mod Mp⋅q^{−1} mod \ M. In other words, output such an integer xx that 0x<M0≤x<M and xqp(mod M)x⋅q≡p(mod\ M).

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+2l1,l2,…,l2n+2 denote the 2n+22n+2 possible lines that can be formed. Let LiLi denote the event that line lili is formed, and Li\overline{Li} denote the event that line lili is not formed (i.e.i.e., P(Li)+P(Li)=1P(Li)+P(\overline{Li})=1).

Let’s find the probability that our table is not winning. It is equal to P(L1L2L2n+2)P(\overline{L_1}∩\overline{L_2}∩…∩\overline{L_{2n+2}}).

Note that the following two statements are true:

  • P(L1L2L2n+2)+P(L1L2L2n+2)=P(L2L2n+2)P(\overline{L_1}∩\overline{L_2}∩…∩\overline{L_{2n+2}})+P({L_1}∩\overline{L_2}∩…∩\overline{L_{2n+2}})=P(\overline{L_2}∩…∩\overline{L_{2n+2}});
  • P(L1L2L2n+2)=P(L2L2n+2L1)P(L1).P(L_1∩\overline{L_2}∩…∩\overline{L_{2n+2}})=P(\overline{L_2}∩…∩\overline{L_{2n+2}}|L_1)⋅P(L_1).

The first one follows from the law of total probability, and the second one follows from the definition of conditional probability.

These two statements combined allow us to use the following formula:

P(L1L2L2n+2)=P(L2L2n+2)P(L2L2n+2L1)P(L1).P(\overline{L_1}∩\overline{L_2}∩…∩\overline{L_{2n+2}})=P(\overline{L_2}∩…∩\overline{L_{2n+2}})−P(\overline{L_2}∩…∩\overline{L_{2n+2}}|L_1)⋅P(L_1).

We can apply this formula recursively. Specifically, we can make a function f(i,S)f(i,S), where S=s1,s2,,skS={s_1,s_2,…,s_k} is a subset of 1,2,,i1{1,2,…,i−1}, which calculates P(LiLi+1L2n+2Ls1Lsk)P(L_i∩L_{i+1}∩…∩L_{2n+2}|L_{s1}∩…∩L_{sk}). For i=2n+3i=2n+3, f(i,S)=1f(i,S)=1, and for i2n+2i≤2n+2 we can generalize the formula above as follows:

f(i,S)=f(i+1,S)f(i+1,Si)P(LiLs1Lsk)f(i,S)=f(i+1,S)−f(i+1,S∪{i})⋅P(L_i|L_{s1}∩…∩L_{sk}).

Here, P(LiLs1Lsk)P(L_i|L_{s1}∩…∩L_{sk}) is the probability that line lili is formed given that lines ls1,,lskl_{s1},…,l_{sk} are formed. This is equal to the product of probabilities of all cells belonging to lili which do not belong to any of ls1,,lskl_{s1},…,l_{sk}.

The answer to the problem, i.e.i.e. the probability that our table is winning, is 1f(1,{})1−f(1,\{\}).

This allows us to implement an O(2nn)O(2^n⋅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)f(i,S), we don’t have to make any more recursive calls. Why would it become easy to calculate f(i,S)f(i,S) though?

Let’s order the lines in such a way that ln+3,ln+4,,l2n+2l_{n+3},l_{n+4},…,l_{2n+2} are the horizontal lines of the table. Consider a call of the form f(n+3,S)f(n+3,S). This call is basically asking: “what is the probability that none of lines ln+3,ln+4,,l2n+2l_{n+3},l_{n+4},…,l_{2n+2} are formed, given that lines ls1,,Lskl_{s1},…,L_{sk} are formed?”.

Note that the horizontal lines are independent, and we can actually answer this question in O(n2)O(n^2). Specifically, for any horizontal line, the probability that it is not formed is 11 minus the product of probabilities of all its cells not belonging to any of ls1,,lskl_{s1},…,l_{sk}. The overall value of f(n+3,S)f(n+3,S) is the product of probabilities for individual horizontal lines.

This way, we have built an O(2nn2)O(2^n⋅n^2) solution. This might be fast enough depending on your implementation, but there are at least two ways to optimize it to O(2nn)O(2^n⋅n):

  • The first way is to maintain the products of probabilities of untouched cells for horizontal lines on the fly. For simplicity, assume that l1l_1 and l2l_2 are the diagonal lines. For each i=3,4,,n+2i=3,4,…,n+2, after we process (vertical) line lil_i, we can update the products for all horizontal lines with the cells of lil_i (in O(n)O(n), make a recursive call, and roll back the updates (in O(n)O(n) again). Once we get to i=n+3i=n+3, instead of going through every cell of the table in O(n2)O(n^2), we can just multiply nn horizontal line products in O(n)O(n).
  • The second way is to define g(i,mask)g(i,mask) (1in;mask(1≤i≤n; mask is a bitmask of size nn) to be the product of ai,ja_{i,j} over all jj belonging to maskmask. All values of gg can be calculated in O(2nn)O(2^n⋅n) using dynamic programming: g(i,0)=1g(i,0)=1, and g(i,mask)=g(i,mask2j)ai,jg(i,mask)=g(i,mask⊕2^j)⋅a_{i,j}, where jj is any bit set in maskmask. When we arrive at a f(n+3,S)f(n+3,S) call, for each of the nn 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 gg in O(1)O(1).

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<bits/stdc++.h>
using namespace std;

const int mod = 31607;
const int inv = 3973;

int n, a[22][22], g[22][1<<21], dp[1<<23];

void D(int x, int y, int S) {
if (S >> y & 1) g[x][S] = g[x][S^(1<<y)] * a[x][y+1] % mod;
if (y+1 < n) D(x, y+1, S|(1<<y+1)), D(x, y+1, S);
}

int dfs(int x, int S) {
if (x == n+3) {
return dp[S];
}
int k = 1;
if (x <= n) {
for(int i = 1; i <= n; ++ i) {
k = k * a[i][x] % mod;
}
}
else {
if (x == n+1) {
for(int i = 0; i < n; ++ i) {
if (S>>i&1) continue;
k = k * a[i+1][i+1] % mod;
}
}
else {
for(int i = 0; i < n; ++ i) {
if (S>>(n-1-i)&1) continue;
if ((S>>n&1) && (n&1) && i == n/2) continue;
k = k * a[i+1][n-i] % mod;
}
}
}
return (mod + (dfs(x+1, S) - k * dfs(x+1, S|(1ll<<(x-1))) % mod) % mod) % mod;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; ++ i) {
for(int j = 1; j <= n; ++ j) {
cin >> a[i][j];
a[i][j] = a[i][j] * inv % mod;
}
}
for(int i = 1; i <= n; ++ i) {
g[i][0] = 1;
D(i, 0, 1), D(i, 0, 0);
}
for(int S = 0; S < (1<<(n+2)); ++ S) {
int res = 1;
for(int i = 1; i <= n; ++ i) {
int k = S;
if (k >> n & 1) {
k ^= 1<<n;
k |= 1<<(i-1);
}
if (k >> (n+1) & 1) {
k ^= 1<<(n+1);
k |= 1<<(n-i);
}
res = res * (mod + 1 - g[i][((1<<n)-1)^k]) % mod;
}
dp[S] = res;
}
cout << (mod+1- dfs(1, 0)) % mod << "\n";
return 0;
}
-------------本文结束感谢您的阅读-------------