By Deyao Chen
You and your friend Kay are taking the underground train / tube system to travel around the city Rondon. The doors are close as you approach the train at station . Kay barely squeezes through the small gap of the closing doors whereas you are left behind. You signal with hand gesture that he should return to the same station to meet you so you can travel together later. To kill some time, you find a seat on the platform, take out your phone and start watching a movie. After minutes, you put your phone down but Kay is nowhere to be seen on the train that has just arrived. To get an idea of whereabouts of Kay, you decide to count the number of routes to get back to station that takes him exactly minutes, and the total number of routes possible in exactly minutes.
All trains in the system run in both direction. It is possible for two lines to run parallel for a few stations. For example, line might go from station - - - while train might run from station - - - . Taking different lines counts as a different route even if the stations you go through are the same. It takes minute to get to one station from adjacent stations. Changing trains takes no time; Kay is on a train all the time, spending no time on the platform. Kay might have returned to station while you were watching a movie but there's no way you could have seen him.
It is guaranteed that the sum of the number of stations across all the lines never exceededs . All the stations are numbered sequentially from to , the number of stations that at least one train passes through. For example, if line goes through - - and line goes through - - , and (note that in this case station and are connected to nowhere else). The number of lines is denoted as . It is also guaranteed that each line connects at least two stations.
There is only one train line.
and There is no pair of two lines running in parallel.
and
There is no pair of two lines running in parallel.
No more constraints
The first line contains four numbers, , , , and which is the number of minutes you have to wait, the number of stations, the station you are currently at, and the number of lines.
In the next lines, each line denotes the stations a train line goes through. These stations are numbered from to .
Output two numbers -- the number of routes to get back to station costing exactly minutes and the number of total possible routes to travel from station costing minute. Since the answers can be astronomically large, output the remainder of the answers divided by .
2 10 6 1
1 6 8
2 2
In this example, there is a line running from station 1 - 6 - 8, with a total of 10 stations in the system (some stations might be connected to nowhere). You wait in station 6 for 2 minutes, there are two ways Kay could have gone: 6 - 1 - 6 or 6 - 8 - 6. There are also only two total ways.
2 10 6 2
1 6 8
1 6 8
8 8
There are two lines running from station 1 - 6 - 8. There are 8 ways Kay could have gone. Let's say kay went through station 6 - 1 - 6, he can take line 1 then line 1, line 1 then line 2, line 2 then line 1, or line 2 then line 2. There are 4 paths in this scenario. The same goes for the other way, making a total of 8 ways.
4 3 2 3
1 3
1 3
2 1
5 15