Expected Cells (Stretch)


This is a stretch problem.

There is an n×mn \times m grid of cells. Cell (r,c)(r, c) has a positive weight wr,cw_{r, c}. Let W=∑r,cwr,cW = \sum_{r, c} w_{r, c}.

Two cells AA and BB are sampled independently, with replacement. The probability of choosing cell (r,c)(r, c) on a single sample is wr,c/Ww_{r, c} / W.

Let RR be the smallest axis-aligned rectangle of grid cells that contains both sampled cells, and let AA be the number of cells inside RR (the area of RR counted in cells). If AA and BB are the same cell, then RR is that single cell and the area is 11.

Output the expected value of AA.

It can be shown that the answer can be expressed as a rational number P/QP/Q in lowest terms with QQ coprime to 109+710^9 + 7. Output P⋅Q−1 mod (109+7)P \cdot Q^{-1} \bmod (10^9 + 7).

Input

The first line contains two integers nn and mm.

Each of the next nn lines contains mm integers. The cc-th number on the rr-th of these lines is wr,cw_{r, c}.

Rows and columns are indexed from 11.

Output

Print a single integer: the expected number of cells in the bounding rectangle, modulo 109+710^9 + 7.

Constraints

  • 1≤n,m1 \le n, m
  • n⋅m≤2⋅105n \cdot m \le 2 \cdot 10^5
  • 1≤wr,c≤1061 \le w_{r, c} \le 10^6

Example 1

Input 1
2 2
1 1
1 1
Output 1
250000004
Explanation

All 1616 ordered pairs of cells are equally likely. The expected area is 9/49/4, and 9⋅4−1≡250000004(mod109+7)9 \cdot 4^{-1} \equiv 250000004 \pmod{10^9 + 7}.

Example 2

Input 2
1 1
5
Output 2
1
Explanation

Both samples are the only cell, so the rectangle always has area 11.

Example 3

Input 3
1 3
1 1 1
Output 3
888888897
Explanation

The expected area is 17/917/9, and 17⋅9−1≡888888897(mod109+7)17 \cdot 9^{-1} \equiv 888888897 \pmod{10^9 + 7}.

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.