1307_E. Cow and Treats
After a successful year of milk production, Farmer John is rewarding his cows with their favorite treat: tasty grass!

On the field, there is a row of n units of grass, each with a sweetness s_i. Farmer John has m cows, each with a favorite sweetness f_i and a hunger value h_i. He would like to pick two disjoint subsets of cows to line up on the left and right side of the grass row. There is no restriction on how many cows must be on either side. The cows will be treated in the following manner: 

  * The cows from the left and right side will take turns feeding in an order decided by Farmer John. 
  * When a cow feeds, it walks towards the other end without changing direction and eats grass of its favorite sweetness until it eats h_i units. 
  * The moment a cow eats h_i units, it will fall asleep there, preventing further cows from passing it from both directions. 
  * If it encounters another sleeping cow or reaches the end of the grass row, it will get upset. Farmer John absolutely does not want any cows to get upset. 



Note that grass does not grow back. Also, to prevent cows from getting upset, not every cow has to feed since FJ can choose a subset of them. 

Surprisingly, FJ has determined that sleeping cows are the most satisfied. If FJ orders optimally, what is the maximum number of sleeping cows that can result, and how many ways can FJ choose the subset of cows on the left and right side to achieve that maximum number of sleeping cows (modulo 10^9+7)? The order in which FJ sends the cows does not matter as long as no cows get upset. 

Input

The first line contains two integers n and m (1 ≤ n ≤ 5000, 1 ≤ m ≤ 5000) — the number of units of grass and the number of cows. 

The second line contains n integers s_1, s_2, …, s_n (1 ≤ s_i ≤ n) — the sweetness values of the grass.

The i-th of the following m lines contains two integers f_i and h_i (1 ≤ f_i, h_i ≤ n) — the favorite sweetness and hunger value of the i-th cow. No two cows have the same hunger and favorite sweetness simultaneously.

Output

Output two integers — the maximum number of sleeping cows that can result and the number of ways modulo 10^9+7. 

Examples

Input


5 2
1 1 1 1 1
1 2
1 3


Output


2 2


Input


5 2
1 1 1 1 1
1 2
1 4


Output


1 4


Input


3 2
2 3 2
3 1
2 1


Output


2 4


Input


5 1
1 1 1 1 1
2 5


Output


0 1

Note

In the first example, FJ can line up the cows as follows to achieve 2 sleeping cows: 

  * Cow 1 is lined up on the left side and cow 2 is lined up on the right side. 
  * Cow 2 is lined up on the left side and cow 1 is lined up on the right side. 



In the second example, FJ can line up the cows as follows to achieve 1 sleeping cow: 

  * Cow 1 is lined up on the left side. 
  * Cow 2 is lined up on the left side. 
  * Cow 1 is lined up on the right side. 
  * Cow 2 is lined up on the right side. 



In the third example, FJ can line up the cows as follows to achieve 2 sleeping cows: 

  * Cow 1 and 2 are lined up on the left side. 
  * Cow 1 and 2 are lined up on the right side. 
  * Cow 1 is lined up on the left side and cow 2 is lined up on the right side. 
  * Cow 1 is lined up on the right side and cow 2 is lined up on the left side. 



In the fourth example, FJ cannot end up with any sleeping cows, so there will be no cows lined up on either side.
```python
import sys
input = sys.stdin.readline
from itertools import accumulate

mod=10**9+7

n,m=map(int,input().split())
G=list(map(int,input().split()))

CP=[[0]*(n+1) for i in range(n+1)]
for i in range(m):
    f,e=map(int,input().split())
    CP[f][e]+=1
    
SUMCP=[]
for i in range(n+1):
    SUMCP.append(list(accumulate(CP[i])))

SUM=[0]*(n+1)
LAST=[]

for g in G:
    LAST.append(g)
    SUM[g]+=1

MAX=0
ANS=0
PLUS=1

for j in range(n+1):
    if SUM[j]>0 and SUMCP[j][SUM[j]]>0:
        PLUS=PLUS*SUMCP[j][SUM[j]]%mod
        MAX+=1

ANS=PLUS
MAX0=MAX

def PLUSVALUE(j):
    PLUS=1
    MAXC=0
    
    if cr[j]>=num_g:
        if SUMCP[j][cr[j]]>1:
            MAXC+=1
            PLUS=PLUS*(SUMCP[j][cr[j]]-1)%mod
    else:
        if SUMCP[j][cr[j]]>=1:
            MAXC+=1
            PLUS=PLUS*SUMCP[j][cr[j]]%mod
            
    return PLUS,MAXC
    

def OPLUSVALUE(j):    
    PLUS=1
    MAXC=0
    x,y=LEFT[j],cr[j]
    if x>y:
        x,y=y,x

    if SUMCP[j][x]==SUMCP[j][y]==1:
        MAXC+=1
        PLUS=PLUS*2%mod
        
    else:
        if SUMCP[j][x]>=1:
            MAXC+=2
            PLUS=PLUS*SUMCP[j][x]*(SUMCP[j][y]-1)%mod
                
        elif SUMCP[j][y]>=1:
            MAXC+=1
            PLUS=PLUS*SUMCP[j][y]%mod
            
    return PLUS,MAXC

LEFT=[0]*(n+1)

g=LAST[0]
LEFT[g]+=1
num_g=LEFT[g]

PLUS=CP[g][num_g]
OPLUS=1
if CP[g][num_g]==0:
    flag=0
    MAXC=0
else:
    flag=1
    MAXC=1

cr=SUM
cr[g]-=1

for j in range(n+1):
    if j==g:
        v,p_m=PLUSVALUE(g)
        
        PLUS=PLUS*v%mod
        MAXC+=p_m
    else:
        v,m=OPLUSVALUE(j)
        
        OPLUS=OPLUS*v%mod
        MAXC+=m

if MAXC>MAX and flag==1:
    MAX=MAXC
    ANS=PLUS*OPLUS%mod

elif MAXC==MAX and flag==1:
    ANS+=PLUS*OPLUS%mod

if flag==1:
    MAXC-=1+p_m
else:
    MAXC-=p_m


for i in range(1,n):
    #print("!",i,MAX,MAXC,ANS)
    g=LAST[i]
    past_g=LAST[i-1]
    v0,m0=OPLUSVALUE(past_g)
    v2,m2=OPLUSVALUE(g)
    
    OPLUS=OPLUS*v0*pow(v2,mod-2,mod)%mod
    MAXC+=m0-m2
    
    LEFT[g]+=1
    num_g=LEFT[g]
    cr[g]-=1

    #print(LEFT,cr,MAXC,PLUS,OPLUS)

    if CP[g][num_g]==0:
        continue
    else:
        PLUS=CP[g][num_g]
        MAXC+=1

    v,p_m=PLUSVALUE(g)

    PLUS=PLUS*v%mod
    MAXC+=p_m

    if MAXC>MAX:
        MAX=MAXC
        ANS=PLUS*OPLUS%mod

    elif MAXC==MAX:
        ANS+=PLUS*OPLUS%mod

    #print(LEFT,cr,MAXC,PLUS,OPLUS)
    MAXC-=1+p_m


print(MAX,ANS%mod)

        
                

            
                    

    

    
    
    

```