KFSTB - Help the Commander in Chief


The Country of byteland is in a state of war with its neighboring nation bitland which is a small island. The king of byteland needs to send his soldiers across the sea to attack bitland. It is not easy for bytelandian soldiers to cross the sea between the two countries as there are soldiers of bitland who are constantly on patrol.

But, The commander of byteland through his secret sources has gathered some information about the camping spots in between, It is as follows. There are C camping spots in the sea between the two countries. There are B one way bridges, each bridge is between two camps. Bytelandian army can use these bridges without any danger of being attacked. The commander also knows that if the army leaves a camping spot along a bridge safely it is not possible to return back safely to the same camping spot.

The camping spot closest to the bytelandian shore is S and the camping spot closest to the bitlandian shore is T. The commander in chief of byteland wants to formulate a strategy for the army to safely move from the camping spot S to T. You have been assigned the task of finding the number of possible paths that a soldier could follow from camping spot S to T.

Input

The first line of input contains the number of test cases D. For each test case the first line contains C, B, S and T number of camping spots, number of one way bridges, camping spot close to byteland and camping spot close to bitland respectively. The next B lines contain description of bridges. Each bridge is described on a single line by two integers X and Y denoting the starting and ending point of a bridge.

Output

In each line print the number of possible paths modulo 1000000007

Sample Input

2
4 4 1 4
1 2
1 3
2 4
3 4
5 5 1 5
1 2
2 3
2 4
3 5
4 5

Sample Output

2
2

Constraints

1<= D <= 10
1<= C <= 10000
1<= B <= 10000
1 <= X,Y <= C
1 <= S,T <= C


hide comments
satyam_656: 2021-04-01 14:02:14

Super Easy! dfs+dp

saurabh_kl: 2021-02-03 07:40:38

This problem can also be solved using level order traversal

Last edit: 2021-02-03 07:41:27
kshubham02: 2021-01-17 01:42:53

Problem statement is ambiguous :

"The commander also knows that if the army leaves a camping spot along a bridge safely it is not possible to return back safely to the same camping spot" - this indicates that bridges may exist between A and B and also B and A, but in your path if you travelled from A to B you cannot come back to A.

This is wrong - actually, if path exists from A to B, no path exists from B to A. In other words, there are no cycles in the graph.

xqfparaboy: 2020-09-19 17:20:28

Take Modulo during dp operation

the_brogrammer: 2020-08-19 06:02:05

remember to use take the modulus operator with 10^9+7.

jrseinc: 2020-08-18 19:06:43

IDK why everyone is saying that the statement is ambiguous. " if the army leaves a camping spot along a bridge safely it is not possible to return back safely to the same camping spot." this statement clearly shows that this is a DAG.

harry_shit: 2020-08-18 12:53:21

recommended :)

sarthak_19: 2020-07-05 17:21:40

Tropological Sort+Dp=AC

fahad991: 2020-06-08 05:48:21

Easy ..basic dp problem.

vidhan_1234: 2020-06-01 17:16:48

Accepted in 3 go...


Added by:rizwan hudda
Date:2012-09-04
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Kodefest 2012 IITK - Own problem