Under construction

Still being built. Things may change

Lexicopolis

Time limit2sMemory limit1024 MB

Summary
Find the lexicographically smallest walk of exactly k edges from s to t, comparing edge magic numbers, and output its polynomial value modulo 1e9+7.
Level

Hard8 of 10

Topics
Graph, Dynamic programming, String
Solved
No attempts yet

Problem

Lexicopolis is an ancient city known for its legends and treasures. The city is known for its tangled network of one-way roads. There are nn intersections and mm one-way roads. Road ii lets people travel only from intersection uiu_i to intersection viv_i, and it carries a magic number wiw_i.

A path of length kk from ss to tt is a sequence of roads e1,e2,…,eke_1, e_2, \dots, e_k that can be traveled in order from ss to tt. When two paths are compared, look at the first position where their magic numbers differ. The path with the smaller number at that position is lexicographically smaller. Compare the magic numbers, not the road indices.

A rumor says that a tourist who finds the lexicographically smallest path of length kk from ss to tt receives a gift from the government. Write a program that finds this path.

Input

The first line contains six integers nn, mm, ss, tt, xx, and kk. Here nn is the number of intersections, mm is the number of roads, ss is the starting intersection, tt is the destination intersection, xx is the base used in the final answer, and kk is the path length. Each of the next mm lines contains three integers uiu_i, viv_i, and wiw_i. This means road ii goes from uiu_i to viv_i and has magic number wiw_i.

Output

If no path of length kk from ss to tt exists, print -1. Otherwise, take the lexicographically smallest path e1,e2,…,eke_1, e_2, \dots, e_k and print the value of ∑i=1kweixk−i\sum_{i=1}^{k} w_{e_i} x^{k-i} modulo 109+710^9+7.

Constraints

  • 2≤n≤502 \le n \le 50
  • 1≤m≤n2−n1 \le m \le n^2 - n
  • 1≤ui,vi≤n1 \le u_i, v_i \le n, and ui≠viu_i \ne v_i
  • 1≤wi≤1091 \le w_i \le 10^9
  • Each ordered pair (ui,vi)(u_i, v_i) appears at most once.
  • 1≤s,t≤n1 \le s, t \le n
  • 1≤k≤1091 \le k \le 10^9
  • 1≤x≤1091 \le x \le 10^9

Examples4

  1. Example 1

    Input
    3 6 1 3 10 4
    1 2 2
    2 1 1
    1 3 1
    3 1 2
    2 3 1
    3 2 2
    
    Expected output
    1211
    
  2. Example 2

    Input
    3 6 1 3 10 5
    1 2 2
    2 1 1
    1 3 1
    3 1 2
    2 3 1
    3 2 2
    
    Expected output
    12121
    
  3. Example 3

    Input
    6 7 5 6 10 10
    1 2 1
    2 4 2
    3 4 1
    4 5 3
    5 3 5
    4 6 2
    6 5 1
    
    Expected output
    121513477
    
  4. Example 4

    Input
    6 7 1 6 123 2
    1 2 1000000000
    2 4 2
    3 4 3
    4 5 4
    5 3 1
    4 6 2
    6 5 1
    
    Expected output
    -1