Travels
InterviewTime limit1sMemory limit1024 MB
Given cities joined by one-way rails from lower to higher numbers, with some tickets unsold, sum the pleasure of every trip of at least two cities, modulo 1e9+7.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Graph
- Solved
- No attempts yet
Problem
Martynas is a travel enthusiast who writes reviews on an Internet blog. Today he wants to evaluate trips offered by a travel agency called Rail Baitlandija.
Baitlandija has cities, numbered from 1 to . The cities are connected by one-way rails, so a train can go from city to city only when .
However, the agency does not sell every possible route. There are pairs with where tickets from city to city cannot be bought.
A trip is a sequence of cities with , where tickets from to are sold for every . Martynas wants to take each such trip exactly once.
Visiting city gives him pleasure, even if he has visited that city on an earlier trip. So the trip gives pleasure, and the pleasure from all trips is summed.
After finishing all the trips, he forgot the total. He needs your help to calculate it for his review.
Input
The first line contains integers and .
The second line contains integers , , ..., , where is the pleasure gained in city .
Each of the next lines contains integers and , meaning tickets from city to city are not sold.
Output
Print one non-negative integer: the total pleasure modulo .
Constraints
for all
Time limit: 1000 ms. Memory limit: 1024 MB.