Mail Delivery
Time limit2sMemory limit1024 MB
Count the DFS visiting orders from the capital where every parcel's source city is visited before its destination city, modulo 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Tree, DFS
- Solved
- No attempts yet
Statement
A region is testing a robotic courier to automate mail delivery in remote areas.
The region has cities connected by two-way roads. Along these roads, you can travel from any city to any other city. Two cities joined by a road are called neighbors. Each city has at most neighbors. The cities are numbered from to , and city is the capital.
Each city has an office of a courier company. The courier must deliver parcels. The -th parcel has a source city and a destination city .
The robot courier follows this procedure.
- It starts in the capital and moves between cities along roads. While moving, it can carry any number of parcels.
- Each time it arrives at a city it has not visited before, it stops at that city's office, leaves every parcel it carries that is addressed to that city, and picks up every parcel that is sent from that city.
- If a neighbor of the current city has not been visited, it picks one such neighbor and moves there.
- If all neighbors have been visited and the courier is not in the capital, it moves to the neighbor closest to the capital. If it is in the capital, the trip ends.
The courier visits each office exactly once, on the first visit. The -th parcel is delivered if the courier visits the office in city before the office in city .
The visiting order depends on which unvisited neighbor the courier picks at each step. A visiting order is called valid if every parcel is delivered.
Given the road map and the parcels, count the valid visiting orders. Output the count modulo .
Input
The first line contains two integers and , the number of cities and the number of parcels (, ).
Each of the next lines describes a road with two integers and , the cities it connects (, ).
The roads connect all cities. Each city has at most roads.
Each of the next lines describes a parcel with two integers and , its source and destination cities (, ).
Output
Print one integer: the number of valid visiting orders modulo .
Hint

The figure shows the cities and roads of the first example. Parcels go from city 5 to city 3 and from city 4 to city 6. The following visiting orders are valid. The first visit to each office is in bold: (1, 4, 1, 2, 5, 2, 6, 2, 1, 3, 1) and (1, 4, 1, 2, 6, 2, 5, 2, 1, 3, 1).
In the second example, the cities and roads are the same, and the parcel goes from city 5 to city 2. This is impossible. The courier does not stop at an office on a repeat visit to city 2, and city 5 cannot be visited for the first time before city 2 on any route from the capital.