#P4019. 多边形染色

多边形染色

Description

Flokirie has a beautiful convex nn-gon with vertices numbered from 11 to nn. Each edge connects vertex ii and i+1i+1, and in particular, vertex nn is connected to vertex 11.

He wants to color each vertex with some color kk (kck \le c), i.e., each vertex is assigned a color from 11 to cc, and adjacent vertices must not have the same color.

He wants to know how many feasible colorings there are. He calculated it on paper and solved it in 55 minutes.

He thought that was too trivial, so he defined the following “fancy” operations:

  1. 1 x p: vertex xx must be colored with color pp.
  2. 2 x p: vertex xx must not be colored with color pp.
  3. 3 x y: change the attribute of the edge between vertex xx and vertex yy (guaranteed that y=x±1y = x \pm 1 and x,y1,nx, y \ne 1, n), i.e., vertex xx must have the same color as vertex yy.

Now, he wants to know the total number of feasible colorings after applying all operations. Since the result may be large, output it modulo 987654321987654321.

Input Format

The first line contains three positive integers nn, mm, cc, representing the number of sides of the polygon, the number of operations, and the number of colors.

Each of the following mm lines contains three positive integers describing one operation, as defined above.

Output Format

Output a single integer: the number of feasible colorings modulo 987654321987654321.

3 0 3
6
5 2 5
2 3 4
3 2 3
208

Hint

Test Point ID nn mm cc
121 \sim 2 3n103 \le n \le 10 0m100 \le m \le 10 1c51 \le c \le 5
343 \sim 4 3n1003 \le n \le 100 0m300 \le m \le 30 1c81 \le c \le 8
55 3n2×1033 \le n \le 2 \times 10^3 m=0m = 0 1c101 \le c \le 10
66 0m1000 \le m \le 100
77 3n5×1043 \le n \le 5 \times 10^4 m=0m = 0
88 0m5000 \le m \le 500
9109 \sim 10 0m1030 \le m \le 10^3

It is guaranteed that for 100%100\% of the testdata, 3n5×1043 \le n \le 5 \times 10^4, 0m1030 \le m \le 10^3, 1c101 \le c \le 10.

Translated by ChatGPT 5