#P10813. 【MX-S2-T4】 换

【MX-S2-T4】 换

Background

Original link: https://oier.team/problems/S2D

Problem Description

Given n,Vn, V and a sequence of length mm: (p1,q1),(p2,q2),,(pm,qm)(p_1, q_1), (p_2, q_2), \dots, (p_m, q_m).

Find how many positive integer sequences aa of length nn satisfy that every element aia_i meets 1aiV1 \le a_i \le V, and after performing the following operation in order for k=1,2,,mk = 1, 2, \dots, m, the sequence aa becomes non-decreasing:

  • If apk>aqka_{p_k} > a_{q_k}, swap the values of apka_{p_k} and aqka_{q_k}.

Output the answer modulo 109+710^9 + 7.

Input Format

The first line contains three integers n,V,mn, V, m.

The next mm lines each contain two integers pk,qkp_k, q_k.

Output Format

Output one integer, the result modulo 109+710^9 + 7.

3 3 5
3 2
1 3
1 2
2 3
2 1
12
8 900000754 20
5 5
1 2
3 2
1 8
4 8
5 8
3 4
3 7
5 7
3 4
6 8
1 5
7 8
7 8
5 7
1 8
3 8
3 8
5 6
3 8

508510094

Hint

Sample Explanation #1

For the first sample, there are 1212 valid sequences:

{1,1,1}\{1,1,1\}, {1,1,2}\{1,1,2\}, {1,1,3}\{1,1,3\}, {1,2,1}\{1,2,1\}, {1,3,1}\{1,3,1\}, {2,1,1}\{2,1,1\}, {2,2,2}\{2,2,2\}, {2,2,3}\{2,2,3\}, {2,3,2}\{2,3,2\}, {3,1,1}\{3,1,1\}, {3,2,2}\{3,2,2\}, {3,3,3}\{3,3,3\}

Constraints

This problem uses bundled testdata.

  • Subtask 0 (8 pts): n6n \le 6, V8V \le 8, m50m \le 50.
  • Subtask 1 (31 pts): n8n \le 8.
  • Subtask 2 (37 pts): n15n \le 15.
  • Subtask 3 (24 pts): no special constraints.

For all testdata, 1n181 \le n \le 18, 1V1091 \le V \le 10^9, 1m5001 \le m \le 500, 1pk,qkn1 \le p_k, q_k \le n. Note that the relative order of pkp_k and qkq_k is not guaranteed, and the data may contain pk=qkp_k = q_k.

Translated by ChatGPT 5