Expected Cells (Stretch)
View as PDFThis is a stretch problem.
There is an grid of cells. Cell
has a positive weight
.
Let
.
Two cells and
are sampled independently, with replacement. The probability of
choosing cell
on a single sample is
.
Let be the smallest axis-aligned rectangle of grid cells that contains both sampled
cells, and let
be the number of cells inside
(the area of
counted in cells).
If
and
are the same cell, then
is that single cell and the area is
.
Output the expected value of .
It can be shown that the answer can be expressed as a rational number in lowest
terms with
coprime to
. Output
.
Input
The first line contains two integers and
.
Each of the next lines contains
integers. The
-th number on the
-th of these
lines is
.
Rows and columns are indexed from .
Output
Print a single integer: the expected number of cells in the bounding rectangle, modulo
.
Constraints
Example 1
Input
2 2
1 1
1 1
Output
250000004
Explanation
All ordered pairs of cells are equally likely. The expected area is
, and
.
Example 2
Input
1 1
5
Output
1
Explanation
Both samples are the only cell, so the rectangle always has area .
Example 3
Input
1 3
1 1 1
Output
888888897
Explanation
The expected area is , and
.
Comments