Under construction

Still being built. Things may change

Travels

Interview

Time limit1sMemory limit1024 MB

Summary
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 NN cities, numbered from 1 to NN. The cities are connected by one-way rails, so a train can go from city ii to city jj only when i<ji < j.

However, the agency does not sell every possible route. There are MM pairs (ai,bi)(a_i, b_i) with ai<bia_i < b_i where tickets from city aia_i to city bib_i cannot be bought.

A trip is a sequence of cities (a1,a2,...,ak)(a_1, a_2, ..., a_k) with k>1k > 1, where tickets from aia_i to ai+1a_{i+1} are sold for every 1≤i≤k−11 \le i \le k-1. Martynas wants to take each such trip exactly once.

Visiting city ii gives him mim_i pleasure, even if he has visited that city on an earlier trip. So the trip (a1,...,ak)(a_1, ..., a_k) gives ma1+ma2+...+makm_{a_1} + m_{a_2} + ... + m_{a_k} 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 NN and MM.

The second line contains integers m1m_1, m2m_2, ..., mNm_N, where mim_i is the pleasure gained in city ii.

Each of the next MM lines contains integers aia_i and bib_i, meaning tickets from city aia_i to city bib_i are not sold.

Output

Print one non-negative integer: the total pleasure modulo 109+710^9 + 7.

Constraints

1≤N,M≤2000001 \le N, M \le 200000

0≤mi≤10000000 \le m_i \le 1000000 for all 1≤i≤N1 \le i \le N

Time limit: 1000 ms. Memory limit: 1024 MB.

Examples1

  1. Example 1

    Input
    4 2
    10 5 8 1
    2 3
    1 4
    
    Expected output
    83