Under construction

Still being built. Things may change

Younghee's Errand

Time limit2sMemory limit1024 MB

Summary
Given an N by N grid of candies (o) and chocolates (x), find the average expected candy and chocolate counts over all target cells after each label flip.
Level

Hard9 of 10

Topics
Combinatorics, Dynamic programming, Matrix
Solved
No attempts yet

Problem

Younghee, the class president at Snoops High School, has an errand from her teacher. She must find Chulsoo somewhere on campus and tell him to go to the faculty office.

She has no time for it, though, because she has to solve the math workbook problems due in the next period. So she lends Kere a secret map that marks where she hid her candies and chocolates, and asks whether Kere can run the errand instead.

The map has these properties:

  • The map is an N×NN \times N grid, and each cell is one area of the school.
  • From each cell, you can move up, down, left, or right. The distance between adjacent cells is the same in every direction.
  • A cell with candy is marked o, and a cell with chocolate is marked x. A cell with nothing has no mark.

Kere runs the errand in these steps. "Choose uniformly at random" means picking one of the available options, with each option having the same probability.

  1. Find the set SS of shortest paths from the top-left cell to Chulsoo's cell that pass through at least one candy or chocolate cell.
  2. If SS is empty, Kere gets 0 candies and 0 chocolates. Otherwise, choose one path in SS uniformly at random. Walk along it and collect every candy and chocolate on the way.

Kere wants to know the expected number of candies and the expected number of chocolates collected with this map.

The problem is that Younghee does not know where Chulsoo is. So assume Chulsoo can be in any cell, and find the average of the expected candy count and the average of the expected chocolate count over all possible positions.

Some cells are known to hold an item, but the map may have labeled them as the wrong kind. Each time a report says the information for cell (r,c)(r, c) is wrong, flip that cell's label and report the average expected values on the updated map.

Input

The first line contains the grid size NN (1≤N≤10001 \le N \le 1000).

The next NN lines each contain a string sis_i. The jj-th character of sis_i describes row ii, column jj of the map.

  • .: nothing in this cell
  • o: candy in this cell
  • x: chocolate in this cell

The next line contains the number of reports QQ (1≤Q≤1000001 \le Q \le 100000). The next QQ lines each contain two integers rir_i and cic_i (1≤ri,ci≤N1 \le r_i, c_i \le N). The cell at (ri,ci)(r_i, c_i) changes from chocolate to candy, or from candy to chocolate.

It is guaranteed that cell (ri,ci)(r_i, c_i) is not . in the original map.

Output

Print the answers as follows. Let M=998244353M = 998244353. If the average expected value is the irreducible fraction A/BA / B, print A⋅BM−2A \cdot B^{M-2} modulo MM. For every Chulsoo position, the size of SS is either 0 or coprime to MM, and this is guaranteed.

On the first line, print the average expected candy count and the average expected chocolate count for the original map, separated by a space.

From line 2 through line Q+1Q+1, line i+1i+1 prints the same two averages for the map after applying the first ii reports, separated by a space.

Hint

For the original map, the candy average is computed as follows.

  • If Chulsoo is at row 1, column 1, no path contains a candy or chocolate, so Younghee gets 0 candies.
  • If Chulsoo is at row 1, column 2, the result is also 0 candies.
  • If Chulsoo is at row 2, column 1, the only shortest path with a candy or chocolate goes down one cell. It collects 1 candy.
  • If Chulsoo is at row 2, column 2, there are two shortest paths: an L-shaped path and the other L-shaped path. The first collects no candy, and the second collects 1 candy. The expected candy count is 0.5.

So the average is (0+0+1+0.5)/4=3/8(0 + 0 + 1 + 0.5) / 4 = 3/8, and the output is 3⋅8M−23 \cdot 8^{M-2} modulo MM, which is 623902721.

The chocolate average works out similarly to (0+0+0+1)/4=1/4(0 + 0 + 0 + 1) / 4 = 1/4, so the output is 1⋅4M−21 \cdot 4^{M-2} modulo MM, which is 748683265.

After the first report, the map has no chocolate, so the chocolate average is 0.

After the second report, the map is the same as the original, so the first and third output lines are identical.

Examples1

  1. Example 1

    Input
    2
    ..
    ox
    4
    2 2
    2 2
    2 1
    2 2
    
    Expected output
    623902721 748683265
    374341633 0
    623902721 748683265
    0 374341633
    748683265 623902721