Island
Time limit7sMemory limit512 MB
Count subsets of coastal settlements such that every lake-boundary settlement can reach a chosen one through a planar road network, modulo 10^9+7.
- Level
Hard9 of 10
- Topics
- Graph, Dynamic programming, Union-find
- Solved
- No attempts yet
Problem
Welcome to the island of Bitcairn! It has everything: settlements, roads, a beautiful lake, the Internet, and a monster living in the lake that is ready to destroy the whole island. A monster in the lake?
Byteasar, governor of Bitcairn, has ordered you to prepare an evacuation plan for tourists in case of a monster attack. He gave you the following information about the island:
- The island is a ring. Its outer boundary is the boundary of the sea, and its inner boundary is the boundary of the lake.
- The settlements are numbered consecutively from to .
- Settlements to are on the boundary of the lake. They are numbered clockwise or counterclockwise along that boundary.
- Settlements to are on the coast (the boundary of the sea). They are numbered clockwise or counterclockwise along the coast.
- There are roads connecting settlements. A road cannot pass through the lake, the sea, or another settlement. Two roads may intersect only at a shared endpoint, so the road network is a planar graph. Each road is either directed or undirected.
- Every tourist in a settlement on the lake boundary can reach the coast through the road network, possibly using several roads.
To enable evacuation, choose a subset of coastal settlements where seaports will be built. The plan is safe if and only if every tourist in a settlement on the lake boundary can reach at least one settlement on the sea boundary where a seaport is built. Count the safe plans.
Input
The first line contains four integers , , , and (, , , ).
Each of the next lines describes one road in one of two formats:
-
-{-}: a bidirectional road between settlements and -
->: a directed road from settlement to settlement
Here and . No two roads connect the same pair of settlements. The settlements and roads form a planar graph. From every settlement on the lake boundary, at least one coastal settlement can be reached.
Output
Print the number of safe plans modulo .
Hint

In the first test, a seaport must be built at settlement so that tourist is safe. Tourists and can also reach settlement , so they are safe too. Therefore, it does not matter whether seaports are built at settlements and .
In the second test, seaports must be built at at least two of settlements , , and , which is enough to keep all tourists safe. It does not matter whether a seaport is built at settlement . Checking by hand, there are safe plans in total.