Ruritania is a country with a very badly maintained road network, which is not exactly good news for lorry drivers that constantly have to do deliveries. In fact, when roads are maintained, they become one-way. It turns out that it is sometimes impossible to get from one town to another in a legal way – however, we know all towns are reachable, though illegally!
Fortunately for us, the police tend to be very corrupt and they will allow a lorry driver to break the rules and drive in the wrong direction provided they receive 'a small gift'. There is one patrol car for every road and they will request 1000 Ruritanian dinars when a driver drives in the wrong direction. However, being greedy, every time a patrol car notices the same driver breaking the rule, they will charge double the amount of money they requested the previous time on that particular road.
Borna is a lorry driver that managed to figure out this bribing pattern. As part of his job, he has to make stops in some towns all over Ruritania and he has to make these stops in a certain order. There are towns (enumerated from to ) in Ruritania and Borna's initial location is the capital city i.e. town . He happens to know which ones out of the roads in Ruritania are currently unidirectional, but he is unable to compute the least amount of money he needs to prepare for bribing the police. Help Borna by providing him with an answer and you will be richly rewarded.
The first line contains , the number of towns in Ruritania. The following lines contain information regarding individual roads between towns. A road is represented by a tuple of integers , which are separated with a single whitespace character. The numbers and represent the cities connected by this particular road, and is either or : means that the road is bidirectional, means that only the direction is legal. The next line contains , the number of stops Borna has to make. The final line of input contains positive integers : the towns Borna has to visit.
The output should contain a single number: the least amount of thousands of Ruritanian dinars Borna should allocate for bribes, modulo .
- for all roads
- for all roads
- for all
5 1 2 0 2 3 0 5 1 1 3 4 1 5 5 4 5 2 2
Borna first takes the route and has to pay 1000 dinars. After that, he takes the route and pays nothing this time. However, when he has to return via , he needs to prepare dinars. Afterwards, getting to via will cost him nothing. Finally, he doesn't even have to leave town to get to , so there is no need to prepare any additional bribe money. Hence he has to prepare dinars in total.