This is the hard version of the problem. The only differences between the two versions are the constraints on $$$k$$$ and the sum of $$$k$$$.
In ancient Persia, Khayyam, a clever merchant and mathematician, is playing a game with his prized treasure chest containing $$$n$$$ red rubies worth $$$2$$$ dinars each and $$$m$$$ blue sapphires worth $$$1$$$ dinar each. He also has a satchel, which starts empty, and $$$k$$$ scrolls with pairs $$$(r_1, b_1), (r_2, b_2), \ldots, (r_k, b_k)$$$ that describe special conditions.
The game proceeds for $$$n + m$$$ turns as follows:
Note that the value of some gems might be affected by multiple decrees, and in that case the gems' value is doubled multiple times.
Determine the expected value of Khayyam's satchel at the end of the game, modulo $$$998,244,353$$$.
Formally, let $$$M = 998,244,353$$$. It can be shown that the exact answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x < M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \leq n, m \leq 2 \cdot 10^5$$$, $$$0 \leq k \leq 5000$$$) — the number of red rubies, the number of blue sapphires, and the number of scrolls describing special conditions, respectively.
Each of the next $$$k$$$ lines contains two integers $$$r_i$$$, $$$b_i$$$ ($$$0 \leq r_i \leq n$$$, $$$0 \leq b_i \leq m$$$, $$$1 \leq r_i + b_i \leq n+m-1$$$). It is guaranteed that the pairs $$$(r_i, b_i)$$$ are distinct.
It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 \cdot 10^5$$$, and the sum of $$$k$$$ over all test cases does not exceed $$$5000$$$.
For each test case, print a single integer: the expected value of Khayyam's satchel at the end of the process, modulo $$$998,244,353$$$.
53 4 01 1 11 03 3 21 12 23 3 22 11 210 4 51 08 06 40 27 4
10 499122180 798595498 149736666 414854846
In the first test case, at the end of the process, there will always be $$$3$$$ red rubies and $$$4$$$ blue sapphires. None of the special conditions described in the scrolls are met, so the value of Khayyam's satchel remains unchanged. The total value of the satchel at the end is always $$$2 \cdot 3 + 1 \cdot 4 = 10$$$.
In the second test case, consider the following two cases:
Thus, the expected value at the end is $$$\frac{1}{2} \cdot 3 + \frac{1}{2} \cdot 4 = \frac{7}{2}$$$, which is $$$499,122,180$$$ modulo $$$998,244,353$$$.
Name |
---|