#P2220. [HAOI2012] 容易题

[HAOI2012] 容易题

Description

There is a sequence AA of length mm consisting of positive integers, where every element lies in [1,n][1, n].

You are given some constraints (AxA_x cannot be yy). Let the product of the sequence AA be A\prod A. Compute the sum of the products over all valid sequences.

In other words, let SS be the set of all valid sequences {A,A,}\{A, A', \ldots\}, and compute

TST.\sum_{T \in S} \prod T.

The answer is taken modulo 109+710^9 + 7.

Input Format

The first line contains three positive integers n,m,kn, m, k. Here, nn and mm are as described above, and kk is the number of constraints.

Each of the next kk lines contains two positive integers x,yx, y, representing the constraint AxyA_x \ne y.

Output Format

Output a single integer on one line: the answer.

If there is no valid sequence, output 00.

3 4 5
1 1
1 1
2 2
2 3
4 3

90

Hint

Sample Explanation #1

A1A_1 cannot be 11, A2A_2 cannot be 2,32, 3, and A4A_4 cannot be 33, so there are the following 1212 possible sequences:

Sequence Product
{2,1,1,1}\{2, 1, 1, 1\} 22
{2,1,1,2}\{2, 1, 1, 2\} 44
{2,1,2,1}\{2, 1, 2, 1\}
{2,1,2,2}\{2, 1, 2, 2\} 88
{2,1,3,1}\{2, 1, 3, 1\} 66
{2,1,3,2}\{2, 1, 3, 2\} 1212
{3,1,1,1}\{3, 1, 1, 1\} 33
{3,1,1,2}\{3, 1, 1, 2\} 66
{3,1,2,1}\{3, 1, 2, 1\}
{3,1,2,2}\{3, 1, 2, 2\} 1212
{3,1,3,1}\{3, 1, 3, 1\} 99
{3,1,3,2}\{3, 1, 3, 2\} 1818

Constraints

  • For 30%30\% of the testdata, n4n \leq 4, m10m \leq 10, k10k \leq 10.
  • For another 20%20\% of the testdata, k=0k = 0.
  • For 70%70\% of the testdata, n,m,k1000n, m, k \leq 1000.
  • For 100%100\% of the testdata, 1n,m1091 \leq n, m \leq 10^9, 0k1050 \leq k \leq 10^5, 1xm1 \leq x \leq m, 1yn1 \leq y \leq n.

Translated by ChatGPT 5