#P10219. [省选联考 2024] 虫洞

[省选联考 2024] 虫洞

Problem Description

Country E has nn cities, numbered from 11 to nn. To make travel between cities more convenient, the Ministry of Transportation of Country E wants to build some wormholes among the nn cities. Each wormhole is a directed passage from one city to another. The start and end of a passage are allowed to be the same city, and there may also be multiple wormholes connecting the same pair of cities.

To distinguish the construction time of wormholes, the ministry assigns each wormhole a positive integer label.

We call a wormhole construction plan good if it satisfies the following four conditions:

  1. There exists a non-negative integer dd such that each city is the start of exactly dd wormholes and also the end of exactly dd wormholes.
  2. For each city, among the labels of wormholes that start from it, 11 to dd each appear exactly once.
  3. For each city, among the labels of wormholes that end at it, 11 to dd each appear exactly once.
  4. Pick any city uu and positive integers 1j1,j2d1\le j_1, j_2 \le d. Starting from uu, first go through a wormhole labeled j1j_1, then go through a wormhole labeled j2j_2, and arrive at city v1v_1. Starting from uu, first go through a wormhole labeled j2j_2, then go through a wormhole labeled j1j_1, and arrive at city v2v_2. Then it must always hold that v1=v2v_1=v_2.

In particular, the plan that builds no wormholes is also good.

Now, the builder has already built mnmn wormholes and assigned them labels 1m1\sim m, and the current construction plan is good. He wants to additionally build knkn wormholes and assign them labels (m+1)(m+k)(m+1)\sim (m+k). He must ensure that all these (m+k)n(m + k)n wormholes still form a good construction plan. He wants to know how many ways there are to build the new knkn wormholes such that the resulting plan of (m+k)n(m + k)n wormholes is good.

Since the answer can be very large, you only need to output it modulo 998244353998244353.

Input Format

The first line contains four non-negative integers c,n,m,kc, n, m, k, where cc denotes the test point ID. In the samples, cc means that the Constraints of that sample are the same as those of test point cc.

In the next nmnm lines, each line contains three positive integers u,v,wu, v, w, representing a wormhole labeled ww, with start city uu and end city vv.

Output Format

Output one integer, the number of plans modulo 998244353998244353.

1 4 1 1
1 2 1
2 1 1
3 4 1
4 3 1
8

Hint

[Sample 1 Explanation]

In this sample, the already-built wormholes with label 11 are 12,21,34,431\to 2,2\to 1,3\to 4,4\to 3. To make the 88 wormholes form a good construction plan, there are 88 possible choices for the newly built wormholes with label 22:

  1. 11,22,33,441\to 1, 2\to 2, 3\to 3, 4\to 4
  2. 11,22,34,431\to 1, 2\to 2, 3\to 4, 4\to 3
  3. 12,21,33,441\to 2, 2\to 1, 3\to 3, 4\to 4
  4. 12,21,34,431\to 2, 2\to 1, 3\to 4, 4\to 3
  5. 13,24,31,421\to 3, 2\to 4, 3\to 1, 4\to 2
  6. 13,24,32,411\to 3, 2\to 4, 3\to 2, 4\to 1
  7. 14,23,31,421\to 4, 2\to 3, 3\to 1, 4\to 2
  8. 14,23,32,411\to 4, 2\to 3, 3\to 2, 4\to 1

[Sample 2]

See the attached wormhole2.in/ans.

This sample has c=2c = 2, and it satisfies the constraints of test point 2.

[Sample 3]

See the attached wormhole3.in/ans.

This sample has c=5c = 5, and it satisfies the constraints of test point 5.

[Sample 4]

See the attached wormhole4.in/ans.

This sample has c=7c = 7, and it satisfies the constraints of test point 7.

[Sample 5]

See the attached wormhole5.in/ans.

This sample has c=9c = 9, and it satisfies the constraints of test point 9.

[Sample 6]

See the attached wormhole6.in/ans.

This sample has c=11c = 11, and it satisfies the constraints of test point 11.

[Sample 7]

See the attached wormhole7.in/ans.

This sample has c=15c = 15, and it satisfies the constraints of test point 15.

[Sample 8]

See the attached wormhole8.in/ans.

This sample has c=17c = 17, and it satisfies the constraints of test point 17.

[Sample 9]

See the attached wormhole9.in/ans.

This sample has c=20c = 20, and it satisfies the constraints of test point 20.

[Sample 10]

See the attached wormhole10.in/ans.

This sample has c=22c = 22, and it satisfies the constraints of test point 22.

[Subtasks]

For all test points,

  • 1n21031\le n \le 2\cdot 10^3, 0m1030 \le m \le 10^3, 1k10151 \le k \le 10^{15};
  • 1u,vn1 \le u,v \le n, 1wm1 \le w \le m;
  • It is guaranteed that the initially built mnmn wormholes form a good construction plan.
Test point ID nn mm kk
141\sim 4 5\le 5 3\le 3 3 \le 3
565\sim 6 2103\le 2\cdot 10^3 =0=0 =1=1
787\sim 8 102\le 10^2 =1=1
9109\sim 10 10\le 10
111411\sim 14 103\le 10^3
151615\sim 16 =0=0 1015\le 10^{15}
171917\sim 19 10\le 10
202120\sim 21 2103\le 2\cdot 10^3 103\le 10^3 102\le 10^2
222522\sim 25 1015\le 10^{15}

[Hint]

Some test points of this problem have large input sizes, so we recommend using a faster input method.

Translated by ChatGPT 5