Vasya and Magic Matrix
Contest: Codeforces Round 510 (Div. 2)
Difficulty rating: 2300
Tags: dp, math, probabilities

Vasya has got a magic matrix $$$a$$$ of size $$$n \times m$$$. The rows of the matrix are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$m$$$ from left to right. Let $$$a_{ij}$$$ be the element in the intersection of the $$$i$$$-th row and the $$$j$$$-th column.

Vasya has also got a chip. Initially, the chip is in the intersection of the $$$r$$$-th row and the $$$c$$$-th column (that is, in the element $$$a_{rc}$$$). Vasya performs the following process as long as possible: among all elements of the matrix having their value less than the value of the element with the chip in it, Vasya randomly and equiprobably chooses one element and moves his chip to this element.

After moving the chip, he adds to his score the square of the Euclidean distance between these elements (that is, between the element in which the chip is now and the element the chip was moved from). The process ends when there are no elements having their values less than the value of the element with the chip in it.

Euclidean distance between matrix elements with coordinates $$$(i_1, j_1)$$$ and $$$(i_2, j_2)$$$ is equal to $$$\sqrt{(i_1-i_2)^2 + (j_1-j_2)^2}$$$.

Calculate the expected value of the Vasya's final score.

It can be shown that the answer can be represented as $$$\frac{P}{Q}$$$, where $$$P$$$ and $$$Q$$$ are coprime integer numbers, and $$$Q \not\equiv 0~(mod ~ 998244353)$$$. Print the value $$$P \cdot Q^{-1}$$$ modulo $$$998244353$$$.

Input format:
The first line of the input contains two integers $$$n$$$ and $$$m$$$ $$$(1 \le n, m \le 1\,000)$$$ — the number of rows and the number of columns in the matrix $$$a$$$.

The following $$$n$$$ lines contain description of the matrix $$$a$$$. The $$$i$$$-th line contains $$$m$$$ integers $$$a_{i1}, a_{i2}, \dots, a_{im} ~ (0 \le a_{ij} \le 10^9)$$$.

The following line contains two integers $$$r$$$ and $$$c$$$ $$$(1 \le r \le n, 1 \le c \le m)$$$ — the index of row and the index of column where the chip is now.

Output format:
Print the expected value of Vasya's final score in the format described in the problem statement.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
In the first example, Vasya will move his chip exactly once. The expected value of the final score is equal to $$$\frac{1^2 + 2^2+ 1^2}{3} = 2$$$.

Example input:
1 4
1 1 2 1
1 3

Example output:
2

Example input:
2 3
1 5 7
2 3 1
1 2

Example output:
665496238

Editorial:
Let's iterate over all the elements of the matrix in order of increasing their values and calculate the expected value for these elements to solve the problem. Suppose that now we consider the element at intersection of the $$$R$$$-th row and the $$$C$$$-th column. Let the elements having value less than the value of the current element be $$$(r_1, c_1), (r_2, c_2), \dots , (r_k, c_k)$$$, where $$$r_i$$$ is the row of the $$$i$$$-th element and $$$c_i$$$ is the column of the $$$i$$$-th element.
Then the expected value for the current element can be calculated as follows: $$$\frac{\sum\limits_{i=1}^{k}{(ev_i + (R - r_i)^2 + (C - c_i)^2)}}{k}$$$, where $$$ev_i$$$ is the expected value of the $$$i$$$-th element. We can rewrite the formula to the following form using equivalent transforms: $$$\frac{\sum\limits_{i=1}^{k}{ ev_i }}{k} + R^2 + C^2 + \frac{\sum\limits_{i=1}^{k}{ {r_i}^2 }}{k} + \frac{\sum\limits_{i=1}^{k}{ {c_i}^2 }}{k} - 2\frac{\sum\limits_{i=1}^{k}{ Rr_i }}{k} -2\frac{\sum\limits_{i=1}^{k}{ Cc_i }}{k}$$$.
So we need to maintain five sums for the elements having value less than current element: sum of their values, sum of their row indices, sum of their column indices, sum of squares of their row indices and sum of squares of their column indices. We can maintain all these sums if we will iterate over all the elements continuously in order of increasing their values.
It is also necessary to note that we need to process all the elements having equal values at once and recalculate all the sums right after calculating the expected values for these elements.