Under construction

Still being built. Things may change

Irreversible Reactions

Time limit2sMemory limit512 MB

Summary
Given a directed graph with random transitions, find the expected time to leave the set of states that can still return to the start, or report infinity.
Level

Hard8 of 10

Topics
Graph, Probability, Dynamic programming, Math
Solved
No attempts yet

Problem

Life is ephemeral. What is done sometimes cannot be undone. In this respect, chemical reaction networks resemble life.

For brevity, we model a chemical reaction network as follows. There are NN states in a chemical reaction network. The reaction starts at state SS. MM distinct pairs (uiu_i, viv_i) of states have a relation such that uiu_i can be transitioned to viv_i. For each unit of time, a state is transitioned to other states if there is at least one state that can be transitioned from it. If there are no such states, the state does not change. If there are two or more states to which the current state can be transitioned, the next state is selected uniformly at random from these states.

In an experiment on the chemical reaction network, we stop the experiment when the current state does not change. In addition, after transitions, it might reach a state from which it is impossible to return to SS by repeating transitions. In such cases we also stop the experiment. Your task as a bioinformatics researcher is to write a program to compute the expected time until we stop the experiment. If the expected time is infinite, output −1-1 instead.

A more precise description of the experiment termination criteria is the following. If there are no states that can be transitioned from the current state, we stop the experiment immediately. Otherwise, the experiment does not stop if it is possible to return to SS from the current state by repeating zero or more transitions. In other words, the state SS itself is always considered as a state that can return to SS in this case.

Input

The input consists of a single test case in the format below.

NN MM SS

u1u_1 v1v_1

⋮\vdots

uMu_M vMv_M

The first line contains three integers NN (2≤N≤2002 \le N \le 200), MM (1≤M≤min⁡(N(N−1)1000)1 \le M \le \min{(N(N-1)1000)}), SS (1≤S≤N1 \le S \le N). NN is the number of states in a chemical reaction network. MM is the number of transitions between states. SS is the initial state. The ii-th of the following MM lines represents the ii-th transition, which means a state uiu_i can be transitioned to viv_i (1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \ne v_i). Note that it does not necessarily imply viv_i can be transitioned to uiu_i. It is guaranteed that the pairs uiu_i and viv_i are distinct. That is, for 1≤i<j≤M1 \le i < j \le M, either ui≠uju_i \ne u_j or vi≠vjv_i \ne v_j holds.

Output

Output the expected time to reach one of the states from which it is impossible to return to the initial state SS. The expected time can be huge, thus output the expected time modulo 109+710^9 + 7. More precisely, if it is finite, the expected time can be represented as an irreducible fraction b/ab/a, and you should output the minimum non-negative integer xx which satisfies ax≡b mod 109+7ax \equiv b \bmod {10^9 + 7}. You may assume that such xx exists for the given input.

If the expected time is infinite, output −1-1 instead.

Examples5

  1. Example 1

    Input
    3 3 1
    1 2
    2 1
    2 3
    
    Expected output
    4
    
  2. Example 2

    Input
    5 7 1
    1 2
    2 1
    2 3
    3 1
    3 4
    4 1
    4 5
    
    Expected output
    22
    
  3. Example 3

    Input
    10 17 5
    4 6
    8 10
    5 8
    7 3
    8 3
    3 5
    9 6
    4 8
    3 2
    6 1
    3 7
    2 10
    2 8
    10 2
    2 9
    8 6
    5 4
    
    Expected output
    800000011
    
  4. Example 4

    Input
    3 2 3
    1 2
    2 3
    
    Expected output
    0
    
  5. Example 5

    Input
    3 3 3
    1 2
    2 3
    3 1
    
    Expected output
    -1