Problem:
Score : 2000 points

Problem Statement

You are given an H \times W grid.
The square at the top-left corner will be represented by (0, 0), and the square at the bottom-right corner will be represented by (H-1, W-1).

Of those squares, N squares (x_1, y_1), (x_2, y_2), ..., (x_N, y_N) are painted black, and the other squares are painted white.

Let the shortest distance between white squares A and B be the minimum number of moves required to reach B from A visiting only white squares, where one can travel to an adjacent square sharing a side (up, down, left or right) in one move.

Since there are H × W - N white squares in total, there are _{(H×W-N)}C_2 ways to choose two of the white squares.

For each of these _{(H×W-N)}C_2 ways, find the shortest distance between the chosen squares, then find the sum of all those distances, modulo 1 000 000 007=10^9+7.

Constraints

1 \leq H, W \leq 10^6

1 \leq N \leq 30

0 \leq x_i \leq H-1

0 \leq y_i \leq W-1

If i \neq j, then either x_i \neq x_j or y_i \neq y_j.

There is at least one white square.

For every pair of white squares A and B, it is possible to reach B from A visiting only white squares.

Input

Input is given from Standard Input in the following format:

H W
N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the sum of the shortest distances, modulo 10^9+7.

Sample Input 1

2 3
1
1 1

Sample Output 1

20

We have the following grid (.: a white square, #: a black square).

...
.#.

Let us assign a letter to each white square as follows:

ABC
D#E

Then, we have:

dist(A, B) = 1

dist(A, C) = 2

dist(A, D) = 1

dist(A, E) = 3

dist(B, C) = 1

dist(B, D) = 2

dist(B, E) = 2

dist(C, D) = 3

dist(C, E) = 1

dist(D, E) = 4

where dist(A, B) is the shortest distance between A and B. The sum of these distances is 20.

Sample Input 2

2 3
1
1 2

Sample Output 2

16

Let us assign a letter to each white square as follows:

ABC
DE#

Then, we have:

dist(A, B) = 1

dist(A, C) = 2

dist(A, D) = 1

dist(A, E) = 2

dist(B, C) = 1

dist(B, D) = 2

dist(B, E) = 1

dist(C, D) = 3

dist(C, E) = 2

dist(D, E) = 1

The sum of these distances is 16.

Sample Input 3

3 3
1
1 1

Sample Output 3

64

Sample Input 4

4 4
4
0 1
1 1
2 1
2 2

Sample Output 4

268

Sample Input 5

1000000 1000000
1
0 0

Sample Output 5

333211937

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <cstring>
#include <cstdio>
using ll = long long;
constexpr int MOD = 1e9 + 7;
constexpr int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int app_x[1000005], app_y[1000005], idx_x[1000005], idx_y[1000005];
int px[65], py[65], dis[65][65], que[100005], sz_x[65], sz_y[65], ans;
bool ban[65][65];
void compress(int n, int m, int k, int *app, int *idx, int *sz)
{
	sz[0] = 1;
	int u = m - app[0], d = ((ll)n * m - k - u) % MOD;
	for (int i = 1; i < n; i++)
	{
		idx[i] = idx[i - 1];
		if (!app[i] && !app[i - 1])
			ans = (ans + (ll)2 * u * d) % MOD;
		else
			idx[i]++;
		sz[idx[i]]++;
		u = (u + m - app[i]) % MOD;
		d = (d + app[i] - m + MOD) % MOD;
	}
}
inline int calc(int x, int y) { return (ll)sz_x[x] * sz_y[y] % MOD; }
inline void bfs(int n, int m, int sx, int sy)
{
	memset(dis, 0x3f, sizeof(dis));
	int he = 0, ta = 0;
	que[ta++] = sx;
	que[ta++] = sy;
	dis[sx][sy] = 0;
	while (he < ta)
	{
		int x = que[he++], y = que[he++];
		ans = (ans + (ll)calc(x, y) * calc(sx, sy) % MOD * dis[x][y]) % MOD;
		for (int i = 0; i < 4; i++)
		{
			int xx = x + dx[i], yy = y + dy[i];
			if (xx < 0 || xx >= n || yy < 0 || yy >= m)
				continue;
			if (!ban[xx][yy] && dis[xx][yy] > 1e9)
			{
				dis[xx][yy] = dis[x][y] + 1;
				que[ta++] = xx;
				que[ta++] = yy;
			}
		}
	}
}
int main()
{
	// freopen("J.in", "r", stdin);
	int n, m, k;
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 0; i < k; i++)
	{
		scanf("%d%d", px + i, py + i);
		app_x[px[i]]++;
		app_y[py[i]]++;
	}
	compress(n, m, k, app_x, idx_x, sz_x);
	compress(m, n, k, app_y, idx_y, sz_y);
	for (int i = 0; i < k; i++)
		ban[idx_x[px[i]]][idx_y[py[i]]] = true;
	for (int i = 0; i <= idx_x[n - 1]; i++)
	{
		for (int j = 0; j <= idx_y[m - 1]; j++)
		{
			if (!ban[i][j])
				bfs(idx_x[n - 1] + 1, idx_y[m - 1] + 1, i, j);
		}
	}
	printf("%lld\n", (ll)ans * (MOD + 1) / 2 % MOD);
	return 0;
}