#P5371. [SNOI2019] 纸牌

[SNOI2019] 纸牌

Description

There is a deck of cards. There are nn types of cards, labeled 1,2,,n1,2,\ldots,n, and each type has CC cards. Therefore, the deck contains a total of nCnC cards.

Three consecutive cards (i,i+1,i+2)(i,i+1,i+2) or three identical cards (i,i,i)(i,i,i) can form a set. If a multiset of cards can be divided into several (possibly zero) sets, then it is called a winning hand.

You have drawn some initial cards from the deck. Now you want to pick some cards to form a winning hand. How many different winning hands can you form? Output the answer modulo 998244353998244353. That is, based on the initial cards, you may select some additional cards (or select none).

Two groups of cards are considered the same if and only if, for every card type, the quantities of that type in the two groups are the same.

Input Format

The first line contains two integers n,Cn,C, representing the number of card types and the number of cards of each type.

The second line contains one integer XX, representing the number of types present in the initial cards.

The next XX lines each contain two integers ki,aik_i,a_i, meaning that there are aia_i cards of type kik_i in the initial cards. The values kik_i are given in strictly increasing order.

Output Format

Output one line with one non-negative integer, the answer modulo 998244353998244353.

3 3
0
10
9 4
9
1 3
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 3
3521

Hint

Sample Explanation 1

All valid choices are as follows:

  1. {}\{\} (choose no cards)
  2. {1,1,1}\{1,1,1\}
  3. {2,2,2}\{2,2,2\}
  4. {3,3,3}\{3,3,3\}
  5. {1,2,3}\{1,2,3\}
  6. {1,1,1,2,2,2}\{1,1,1,2,2,2\}
  7. {1,1,1,3,3,3}\{1,1,1,3,3,3\}
  8. {2,2,2,3,3,3}\{2,2,2,3,3,3\}
  9. {1,1,2,2,3,3}\{1,1,2,2,3,3\}
  10. {1,1,1,2,2,2,3,3,3}\{1,1,1,2,2,2,3,3,3\}

Constraints

For all testdata, 1n10181\leq n\leq 10^{18}, 0aiC10000\leq a_i\leq C\leq 1000, and 0X10000\leq X\leq 1000. Note that aia_i and CC may be 00.

  • For 20%20\% of the data, n=9n=9 and C=4C=4.
  • For another 15%15\% of the data, n105n\leq 10^5 and C=2C=2.
  • For another 15%15\% of the data, X5X\leq 5 and C10C\leq 10.
  • For another 10%10\% of the data, X=0X=0.
  • For another 20%20\% of the data, n105n\leq 10^5.
  • For the remaining 20%20\% of the data, there are no special restrictions.

Translated by ChatGPT 5