A - Rotate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

3 つの数字 x,y,z をこの順に並べてできる 3 桁の整数を xyz と表すことにします。

どの桁も 0 でない 3 桁の整数 abc が与えられるので、abc+bca+cab を求めてください。

制約

  • abc は どの桁も 0 でない 3 桁の整数

入力

入力は以下の形式で標準入力から与えられる。

abc

出力

答えを出力せよ。


入力例 1

123

出力例 1

666

123+231+312=666 となります。


入力例 2

999

出力例 2

2997

999+999+999=2997 となります。

Score : 100 points

Problem Statement

Let xyz denote the 3-digit integer whose digits are x, y, z from left to right.

Given a 3-digit integer abc none of whose digits is 0, find abc+bca+cab.

Constraints

  • abc is a 3-digit integer abc none of whose digits is 0.

Input

Input is given from Standard Input in the following format:

abc

Output

Print the answer.


Sample Input 1

123

Sample Output 1

666

We have 123+231+312=666.


Sample Input 2

999

Sample Output 2

2997

We have 999+999+999=2997.

B - Climbing Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

N 個の台が一列に並んでおり、左から i 番目の台の高さは H_i です。

高橋君は最初、左端の台の上に立っています。

高橋君は高い所が好きなので、次のルールで可能な限り移動を繰り返します。

  • いま立っているのが右端の台ではなく、かつ、右隣にある台の高さが自分がいま立っている台より高いとき、右隣の台に移動する

最終的に高橋君が立っている台の高さを求めてください。

制約

  • 2 \leq N \leq 10^5
  • 1 \leq H_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
H_1 \ldots H_N

出力

答えを出力せよ。


入力例 1

5
1 5 10 4 2

出力例 1

10

最初、高橋君は左端にある高さ 1 の台に立っています。右隣の台の高さは 5 であり、いま立っている台より高いので、右隣の台に移動します。

移動後、高橋君は左から 2 番目にある高さ 5 の台に立っています。右隣の台の高さは 10 であり、いま立っている台より高いので、右隣の台に移動します。

移動後、高橋君は左から 3 番目にある高さ 10 の台に立っています。右隣の台の高さは 4 であり、いま立っている台より低いので、高橋君は移動をやめます。

よって、最終的に高橋君が立っている台の高さは 10 です。


入力例 2

3
100 1000 100000

出力例 2

100000

入力例 3

4
27 1828 1828 9242

出力例 3

1828

Score : 200 points

Problem Statement

There are N platforms arranged in a row. The height of the i-th platform from the left is H_i.

Takahashi is initially standing on the leftmost platform.

Since he likes heights, he will repeat the following move as long as possible.

  • If the platform he is standing on is not the rightmost one, and the next platform to the right has a height greater than that of the current platform, step onto the next platform.

Find the height of the final platform he will stand on.

Constraints

  • 2 \leq N \leq 10^5
  • 1 \leq H_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
H_1 \ldots H_N

Output

Print the answer.


Sample Input 1

5
1 5 10 4 2

Sample Output 1

10

Takahashi is initially standing on the leftmost platform, whose height is 1. The next platform to the right has a height of 5 and is higher than the current platform, so he steps onto it.

He is now standing on the 2-nd platform from the left, whose height is 5. The next platform to the right has a height of 10 and is higher than the current platform, so he steps onto it.

He is now standing on the 3-rd platform from the left, whose height is 10. The next platform to the right has a height of 4 and is lower than the current platform, so he stops moving.

Thus, the height of the final platform Takahashi will stand on is 10.


Sample Input 2

3
100 1000 100000

Sample Output 2

100000

Sample Input 3

4
27 1828 1828 9242

Sample Output 3

1828
C - The Kth Time Query

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 300

問題文

長さ N の数列 A = (a_1, a_2, \dots, a_N) があります。
以下で説明される Q 個のクエリに答えてください。

  • クエリ i : 整数の組 (x_i, k_i) が与えられます。A の要素を a_1, a_2, \dots と前から順に見ていったときに、数 x_ik_i 回目に登場するのは A の前から何番目の要素を見たときかを出力してください。
    ただし条件を満たす要素が存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
  • 1 \leq k_i \leq N (1 \leq i \leq Q)
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N Q
a_1 a_2 \dots a_N
x_1 k_1
x_2 k_2
\vdots
x_Q k_Q

出力

Q 行出力せよ。i 行目ではクエリ i に対する答えを出力せよ。


入力例 1

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

出力例 1

1
2
5
-1
3
6
-1
-1

A の中で 1a_1, a_2, a_5 に登場します。よって、クエリ 1 からクエリ 4 の答えは順に 1, 2, 5, -1 となります。


入力例 2

3 2
0 1000000000 999999999
1000000000 1
123456789 1

出力例 2

2
-1

Score : 300 points

Problem Statement

We have a sequence of N numbers: A = (a_1, a_2, \dots, a_N).
Process the Q queries explained below.

  • Query i: You are given a pair of integers (x_i, k_i). Let us look at the elements of A one by one from the beginning: a_1, a_2, \dots Which element will be the k_i-th occurrence of the number x_i?
    Print the index of that element, or -1 if there is no such element.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
  • 1 \leq k_i \leq N (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 a_2 \dots a_N
x_1 k_1
x_2 k_2
\vdots
x_Q k_Q

Output

Print Q lines. The i-th line should contain the answer to Query i.


Sample Input 1

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

Sample Output 1

1
2
5
-1
3
6
-1
-1

1 occurs in A at a_1, a_2, a_5. Thus, the answers to Query 1 through 4 are 1, 2, 5, -1 in this order.


Sample Input 2

3 2
0 1000000000 999999999
1000000000 1
123456789 1

Sample Output 2

2
-1
D - Multiply and Rotate

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

正の整数 a があります。また、黒板に 1 個の数が 10 進表記で書かれています。
黒板に現在書かれている数を x としたとき、高橋君は次のいずれかの操作を行い、黒板に書かれている数を変化させることができます。

  • x を消し、 xa 倍した数を 10 進表記で新たに書きこむ。
  • x を文字列とみなして、列の末尾の数字を文字列の先頭に移動させる。
    ただし、この操作は x \geq 10 かつ x10 で割り切れないときにしか行えない。

たとえば a = 2, x = 123 であるとき、高橋君は次のいずれかの操作を行うことができます。

  • x を消して、 x \times a = 123 \times 2 = 246 を新たに書きこむ。
  • x を文字列とみなして、123 の末尾の数字である 3 を先頭に移動させる。黒板に書かれている数は 123 から 312 に変化する。

はじめ、黒板には 1 が書かれています。書かれている数を N に変化させるには最小で何回の操作が必要ですか?ただし、どのように操作しても書かれている数を N に変化させられない場合は -1 を出力してください。

制約

  • 2 \leq a \lt 10^6
  • 2 \leq N \lt 10^6
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

a N

出力

答えを出力せよ。


入力例 1

3 72

出力例 1

4

以下に説明する操作を行うことで、 黒板に書かれている数を 4 回で 1 から 72 に変化させることができます。

  • 1 つ目の操作を行う。黒板に書かれている数は 1 \to 3 に変わる。
  • 1 つ目の操作を行う。黒板に書かれている数は 3 \to 9 に変わる。
  • 1 つ目の操作を行う。黒板に書かれている数は 9 \to 27 に変わる。
  • 2 つ目の操作を行う。黒板に書かれている数は 27 \to 72 に変わる。

3 回以下の操作で 72 に変化させることはできないため、答えは 4 になります。


入力例 2

2 5

出力例 2

-1

どのように操作しても黒板に書かれている数を 5 に変化させることはできません。


入力例 3

2 611

出力例 3

12

適切に操作を選ぶことで、 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 61112 回の操作で黒板に書かれている数を 611 に変化させることができ、これが最小です。


入力例 4

2 767090

出力例 4

111

Score : 400 points

Problem Statement

We have a positive integer a. Additionally, there is a blackboard with a number written in base 10.
Let x be the number on the blackboard. Takahashi can do the operations below to change this number.

  • Erase x and write x multiplied by a, in base 10.
  • See x as a string and move the rightmost digit to the beginning.
    This operation can only be done when x \geq 10 and x is not divisible by 10.

For example, when a = 2, x = 123, Takahashi can do one of the following.

  • Erase x and write x \times a = 123 \times 2 = 246.
  • See x as a string and move the rightmost digit 3 of 123 to the beginning, changing the number from 123 to 312.

The number on the blackboard is initially 1. What is the minimum number of operations needed to change the number on the blackboard to N? If there is no way to change the number to N, print -1.

Constraints

  • 2 \leq a \lt 10^6
  • 2 \leq N \lt 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

a N

Output

Print the answer.


Sample Input 1

3 72

Sample Output 1

4

We can change the number on the blackboard from 1 to 72 in four operations, as follows.

  • Do the operation of the first type: 1 \to 3.
  • Do the operation of the first type: 3 \to 9.
  • Do the operation of the first type: 9 \to 27.
  • Do the operation of the second type: 27 \to 72.

It is impossible to reach 72 in three or fewer operations, so the answer is 4.


Sample Input 2

2 5

Sample Output 2

-1

It is impossible to change the number on the blackboard to 5.


Sample Input 3

2 611

Sample Output 3

12

There is a way to change the number on the blackboard to 611 in 12 operations: 1 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 46 \to 92 \to 29 \to 58 \to 116 \to 611, which is the minimum possible.


Sample Input 4

2 767090

Sample Output 4

111
E - MST + 1

Time Limit: 4 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 頂点 M 辺の重み付き無向連結グラフ G が与えられます。G には自己ループや多重辺が含まれている可能性があります。
頂点には頂点 1, 頂点 2, \dots, 頂点 N と番号がついています。
辺には辺 1, 辺 2, \dots, 辺 M と番号がついています。辺 i は頂点 a_i と頂点 b_i を結ぶ重み c_i の辺です。ここで、1 \leq i \lt j \leq M を満たすすべての整数の組 (i, j) について c_i \neq c_j が成り立ちます。

以下で説明される Q 個のクエリに答えてください。
i 番目のクエリでは整数の組 (u_i, v_i, w_i) が与えられます。ここで、1 \leq j \leq M を満たすすべての整数 j について w_i \neq c_j が成り立ちます。
頂点 u_i と頂点 v_i を結ぶ重み w_i の無向辺を e_i として、Ge_i を追加してできるグラフ G_i を考えます。 このとき G_i の最小全域木 T_i は一意に定まることが証明できますが、T_ie_i は含まれるでしょうか?答えを Yes あるいは No で出力してください。

ここで、クエリの前後で G は変化しないことに注意してください。言い換えると、クエリ iGe_i を追加したグラフを考えたとしても、他のクエリで出てくる Ge_i が追加されていることはありません。

最小全域木とは? G全域木 とは、G に含まれるすべての頂点と G に含まれる辺の一部からなる木のことを言います。
G最小全域木 とは、G の全域木の中で辺の重みの和が最小である木のことを言います。

制約

  • 2 \leq N \leq 2 \times 10^5
  • N - 1 \leq M \leq 2 \times 10^5
  • 1 \leq a_i \leq N (1 \leq i \leq M)
  • 1 \leq b_i \leq N (1 \leq i \leq M)
  • 1 \leq c_i \leq 10^9 (1 \leq i \leq M)
  • c_i \neq c_j (1 \leq i \lt j \leq M)
  • グラフ G は連結である。
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq u_i \leq N (1 \leq i \leq Q)
  • 1 \leq v_i \leq N (1 \leq i \leq Q)
  • 1 \leq w_i \leq 10^9 (1 \leq i \leq Q)
  • w_i \neq c_j (1 \leq i \leq Q, 1 \leq j \leq M)
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N M Q
a_1 b_1 c_1
a_2 b_2 c_2
\vdots
a_M b_M c_M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_Q v_Q w_Q

出力

Q 行出力せよ。i 行目ではクエリ i への答えを Yes または No で出力せよ。


入力例 1

5 6 3
1 2 2
2 3 3
1 3 6
2 4 5
4 5 9
3 5 8
1 3 1
3 4 7
3 5 7

出力例 1

Yes
No
Yes

以下では頂点 u と頂点 v を結ぶ重み w の無向辺を (u,v,w) と表します。 G を図に表したものを以下に挙げます。

image

たとえばクエリ 1 では Ge_1 = (1,3,1) を追加したグラフ G_1 を考えます。G_1 の最小全域木 T_1 の辺集合は \lbrace (1,2,2),(1,3,1),(2,4,5),(3,5,8) \rbrace であり e_1 を含みます。よって Yes を出力します。


入力例 2

2 3 2
1 2 100
1 2 1000000000
1 1 1
1 2 2
1 1 5

出力例 2

Yes
No

Score : 500 points

Problem Statement

Given is a weighted undirected connected graph G with N vertices and M edges, which may contain self-loops and multi-edges.
The vertices are labeled as Vertex 1, Vertex 2, \dots, Vertex N.
The edges are labeled as Edge 1, Edge 2, \ldots, Edge M. Edge i connects Vertex a_i and Vertex b_i and has a weight of c_i. Here, for every pair of integers (i, j) such that 1 \leq i \lt j \leq M, c_i \neq c_j holds.

Process the Q queries explained below.
The i-th query gives a triple of integers (u_i, v_i, w_i). Here, for every integer j such that 1 \leq j \leq M, w_i \neq c_j holds.
Let e_i be an undirected edge that connects Vertex u_i and Vertex v_i and has a weight of w_i. Consider the graph G_i obtained by adding e_i to G. It can be proved that the minimum spanning tree T_i of G_i is uniquely determined. Does T_i contain e_i? Print the answer as Yes or No.

Note that the queries do not change T. In other words, even though Query i considers the graph obtained by adding e_i to G, the G in other queries does not have e_i.

What is minimum spanning tree? The spanning tree of G is a tree with all of the vertices in G and some of the edges in G.
The minimum spanning tree of G is the tree with the minimum total weight of edges among the spanning trees of G.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • N - 1 \leq M \leq 2 \times 10^5
  • 1 \leq a_i \leq N (1 \leq i \leq M)
  • 1 \leq b_i \leq N (1 \leq i \leq M)
  • 1 \leq c_i \leq 10^9 (1 \leq i \leq M)
  • c_i \neq c_j (1 \leq i \lt j \leq M)
  • The graph G is connected.
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq u_i \leq N (1 \leq i \leq Q)
  • 1 \leq v_i \leq N (1 \leq i \leq Q)
  • 1 \leq w_i \leq 10^9 (1 \leq i \leq Q)
  • w_i \neq c_j (1 \leq i \leq Q, 1 \leq j \leq M)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1
a_2 b_2 c_2
\vdots
a_M b_M c_M
u_1 v_1 w_1
u_2 v_2 w_2
\vdots
u_Q v_Q w_Q

Output

Print Q lines. The i-th line should contain the answer to Query i: Yes or No.


Sample Input 1

5 6 3
1 2 2
2 3 3
1 3 6
2 4 5
4 5 9
3 5 8
1 3 1
3 4 7
3 5 7

Sample Output 1

Yes
No
Yes

Below, let (u,v,w) denote an undirected edge that connects Vertex u and Vertex v and has the weight of w. Here is an illustration of G:

image

For example, Query 1 considers the graph G_1 obtained by adding e_1 = (1,3,1) to G. The minimum spanning tree T_1 of G_1 has the edge set \lbrace (1,2,2),(1,3,1),(2,4,5),(3,5,8) \rbrace, which contains e_1, so Yes should be printed.


Sample Input 2

2 3 2
1 2 100
1 2 1000000000
1 1 1
1 2 2
1 1 5

Sample Output 2

Yes
No
F - Variety of Digits

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

M 個の数字 C_i が与えられます。

1 以上 N 以下の整数のうち、先頭に余分な 0 をつけずに 10 進法で表した時に C_1,\ldots,C_M を全て含むようなもの全ての和を、 998244353 で割った余りを求めてください。

制約

  • 1 \leq N < 10^{10^4}
  • 1 \leq M \leq 10
  • 0 \leq C_1 < \ldots < C_M \leq 9
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
M
C_1 \ldots C_M

出力

答えを出力せよ。


入力例 1

104
2
0 1

出力例 1

520

1 以上 104 以下の整数のうち、10 進法で表した時に 0, 1 を共に含むようなものは、10,100,101,102,103,1046 個あります。
これらの和は 520 です。


入力例 2

999
4
1 2 3 4

出力例 2

0

1 以上 999 以下の整数で、1, 2, 3, 4 を全て含むようなものは存在しません。


入力例 3

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
5
0 2 4 6 8

出力例 3

397365274

998244353 で割った余りを求めてください。

Score : 500 points

Problem Statement

Given are M digits C_i.

Find the sum, modulo 998244353, of all integers between 1 and N (inclusive) that contain all of C_1, \ldots, C_M when written in base 10 without unnecessary leading zeros.

Constraints

  • 1 \leq N < 10^{10^4}
  • 1 \leq M \leq 10
  • 0 \leq C_1 < \ldots < C_M \leq 9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
M
C_1 \ldots C_M

Output

Print the answer.


Sample Input 1

104
2
0 1

Sample Output 1

520

Between 1 and 104, there are six integers that contain both 0 and 1 when written in base 10: 10,100,101,102,103,104.
The sum of them is 520.


Sample Input 2

999
4
1 2 3 4

Sample Output 2

0

Between 1 and 999, no integer contains all of 1, 2, 3, 4.


Sample Input 3

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
5
0 2 4 6 8

Sample Output 3

397365274

Be sure to find the sum modulo 998244353.

G - Gardens

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

高橋君は A 株のリンゴの苗、 B 株のバナナの苗、C 株のサクランボの苗を持っています。同じ種類の苗は区別できません。
N 園の庭を持っている高橋君は、次の条件をすべて満たすように持っている苗を植えることにしました。

  • すべての庭に少なくとも 1 株以上の苗を植える。
  • 1 つの庭に同じ種類の果物の苗を 2 株以上植えてはいけない。
  • 持っているすべての苗を植える必要はない。

条件を満たす植え方は何通りありますか?答えを 998244353 で割ったあまりを求めてください。
ただし、植え方が異なるとは、ある庭が存在して、片方の植え方でその庭に植えられた果物の苗の集合がもう片方と異なることをいいます。

制約

  • 1 \leq N \leq 5 \times 10^6
  • 0 \leq A \leq N
  • 0 \leq B \leq N
  • 0 \leq C \leq N
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N A B C

出力

答えを出力せよ。


入力例 1

2 2 1 1

出力例 1

21

条件を満たす植え方は 21 通りあり、図示すると以下のようになります。
(縦に並んだ 2 つの枠が庭で、A,B,C はそれぞれリンゴの苗、バナナの苗、サクランボの苗を意味しています。)

image


入力例 2

2 0 0 0

出力例 2

0

条件を満たす植え方が存在しない場合もあります。


入力例 3

2 0 2 2

出力例 3

9

入力例 4

100 12 34 56

出力例 4

769445780

入力例 5

5000000 2521993 2967363 3802088

出力例 5

264705492

Score : 600 points

Problem Statement

Takahashi has A apple seedlings, B banana seedlings, and C cherry seedlings. Seedlings of the same kind cannot be distinguished.
He will plant these seedlings in his N gardens so that all of the following conditions are satisfied.

  • At least one seedling must be planted in every garden.
  • It is not allowed to plant two or more seedlings of the same kind in the same garden.
  • It is not necessary to plant all seedlings he has.

How many ways are there to plant seedlings to satisfy the conditions? Find the count modulo 998244353.
Two ways are distinguished when there is a garden with different sets of seedlings planted in these two ways.

Constraints

  • 1 \leq N \leq 5 \times 10^6
  • 0 \leq A \leq N
  • 0 \leq B \leq N
  • 0 \leq C \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C

Output

Print the answer.


Sample Input 1

2 2 1 1

Sample Output 1

21

As illustrated below, there are 21 ways to plant seedlings to satisfy the conditions.
(The two frames arranged vertically are the gardens. A, B, C stand for apple, banana, cherry, respectively.)

image


Sample Input 2

2 0 0 0

Sample Output 2

0

There may be no way to plant seedlings to satisfy the conditions.


Sample Input 3

2 0 2 2

Sample Output 3

9

Sample Input 4

100 12 34 56

Sample Output 4

769445780

Sample Input 5

5000000 2521993 2967363 3802088

Sample Output 5

264705492
Ex - Painting Weighted Graph

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点 M 辺の無向グラフが与えられます。i 番目の辺は頂点 A_iB_i を結び、重みは C_i です。

最初、全ての頂点は黒く塗られています。あなたは、次の操作を高々 K 回行うことができます。

  • 操作:頂点 v と整数 x を自由に選ぶ。頂点 v から重み x 以下の辺のみを通って到達可能な頂点全て(v 自身を含む)を赤く塗る

操作後に赤く塗られている頂点の集合として考えられるものは何通りありますか?
998244353 で割ったあまりを求めてください。

制約

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq K \leq 500
  • 1 \leq A_i,B_i \leq N
  • 1 \leq C_i \leq 10^9
  • 入力に含まれる値は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N M K
A_1 B_1 C_1
\vdots
A_M B_M C_M

出力

答えを出力せよ。


入力例 1

3 2 1
1 2 1
2 3 2

出力例 1

6

例えば (v,x)=(2,1) と選んで操作を行うと、頂点 1,2 を赤く塗ることができ、(v,x)=(1,0) と選んで操作を行うと、頂点 1 を赤く塗ることができます。

高々 1 回の操作の後で赤く塗られている頂点の集合としてあり得るものは \{\},\{1\},\{2\},\{3\},\{1,2\},\{1,2,3\}6 つです。


入力例 2

5 0 2

出力例 2

16

与えられるグラフは連結とは限りません。


入力例 3

6 8 2
1 2 1
2 3 2
3 4 3
4 5 1
5 6 2
6 1 3
1 2 10
1 1 100

出力例 3

40

与えられるグラフは多重辺や自己ループを持つかもしれません。

Score : 600 points

Problem Statement

Given is an undirected graph with N vertices and M edges. The i-th edge connects Vertices A_i and B_i and has a weight of C_i.

Initially, all vertices are painted black. You can do the following operation at most K times.

  • Operation: choose any vertex v and any integer x. Paint red all vertices reachable from the vertex v by traversing edges whose weights are at most x, including v itself.

How many sets can be the set of vertices painted red after the operations?
Find the count modulo 998244353.

Constraints

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 10^5
  • 1 \leq K \leq 500
  • 1 \leq A_i,B_i \leq N
  • 1 \leq C_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 B_1 C_1
\vdots
A_M B_M C_M

Output

Print the answer.


Sample Input 1

3 2 1
1 2 1
2 3 2

Sample Output 1

6

For example, an operation with (v,x)=(2,1) paints Vertices 1,2 red, and an operation with (v,x)=(1,0) paints Vertex 1.

After at most one operation, the set of vertices painted red can be one of the following six: \{\},\{1\},\{2\},\{3\},\{1,2\},\{1,2,3\}.


Sample Input 2

5 0 2

Sample Output 2

16

The given graph may not be connected.


Sample Input 3

6 8 2
1 2 1
2 3 2
3 4 3
4 5 1
5 6 2
6 1 3
1 2 10
1 1 100

Sample Output 3

40

The given graph may have multi-edges and self-loops.