QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#11854. Microchips

Statistics

Microchips produced by Byteland-Electronics are simple semiconductor panels with transistors located on top of them. The ends of some pairs of transistors are connected using special micro-wires. Micro-wires conduct electricity in one direction only and each of them is characterized by its impedance (impedance is a more advanced version of resistance). The quality of a microchip is measured as the number of different paths inside of the microchip with impedance equal exactly to $ I $.

By a path in the microchip we mean any way of getting from one transistor to another one (the second transistor can be the same as the first one), traveling through micro-wires in the direction of their electricity conduction. Each path consists of at least one micro-wire. A path can pass through any transistor and micro-wire an arbitrary number of times. Impedance of a path is defined as the product of the impedances of all micro-wires on that path.

Write a program which:

  • reads from the standard input a description of a microchip and numbers $ I $ and $ k $,
  • calculates the remainder of the division of the number of paths with impedance $ I $ by $ k $,
  • writes the result to the standard output.

Input Format

The first line of input contains four integers $ n $, $ m $, $ I $ and $ k $ (2 ≤ $ n $ ≤ 50, $1 ≤ m ≤ n(n - 1)$, $1 ≤ I ≤ 2·10^{9}$, $2 ≤ k ≤ 10^{9}$), separated by single spaces. $ n $ represents the number of transistors in the microchip, $ m $ - the number of micro-wires, $ I $ - impedance of paths we are looking for and $ k $ - the number by which division is performed. Each of the following $ m $ lines contains three integers $ a_{j} $, $ b_{j} $ and $ i_{j} $ (1 ≤ $ a_{j} $, $ b_{j} $ ≤ $ n $, $ a_{j} ≠ b_{j} $, $1 ≤ i_{j} ≤ 2·10^{9}$), separated by single spaces and representing a micro-wire with impedance $ i_{j} $, which conducts electricity from transistor $ a_{j} $ to transistor $ b_{j} $. None of the ordered pairs ($ a_{j} $, $ b_{j} $) appears more than once in a test case.

Output Format

The first and only line of the standard output should contain one word NIESKONCZONOSC (i.e. infinity in Polish), if the number of paths with impedance $ I $ is infinite, or the remainder of the division of the number of paths with impedance $ I $ by $ k $ in the opposite case.

Example

Input

4 6 6 1000
2 1 3
1 3 2
1 4 2
4 2 4
4 3 3
3 4 2

Output

5

Input 2

4 4 1 1000
2 1 1
4 2 1
3 4 1
1 3 1

Output 2

NIESKONCZONOSC
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.