Your city has *n* junctions. There are *m* one-way roads between the junctions. As a mayor of the city, you have to ensure the security of all the junctions.

To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction *i* can protect junction *j* if either *i* = *j* or the police patrol car can go to *j* from *i* and then come back to *i*.

Building checkposts costs some money. As some areas of the city are more expensive than others, building checkpost at some junctions might cost more money than other junctions.

You have to determine the minimum possible money needed to ensure the security of all the junctions. Also you have to find the number of ways to ensure the security in minimum price and in addition in minimum number of checkposts. Two ways are different if any of the junctions contains a checkpost in one of them and do not contain in the other.
In the first line, you will be given an integer *n*, number of junctions (1 ≤ *n* ≤ 10<sup class="upper-index">5</sup>). In the next line, *n* space-separated integers will be given. The *i*<sup class="upper-index">*th*</sup> integer is the cost of building checkpost at the *i*<sup class="upper-index">*th*</sup> junction (costs will be non-negative and will not exceed 10<sup class="upper-index">9</sup>).

The next line will contain an integer *m* (0 ≤ *m* ≤ 3·10<sup class="upper-index">5</sup>). And each of the next *m* lines contains two integers *u*<sub class="lower-index">*i*</sub> and *v*<sub class="lower-index">*i*</sub> (1 ≤ *u*<sub class="lower-index">*i*</sub>, *v*<sub class="lower-index">*i*</sub> ≤ *n*; *u* ≠ *v*). A pair *u*<sub class="lower-index">*i*</sub>, *v*<sub class="lower-index">*i*</sub> means, that there is a one-way road which goes from *u*<sub class="lower-index">*i*</sub> to *v*<sub class="lower-index">*i*</sub>. There will not be more than one road between two nodes in the same direction.
import sys, threading
import math
from os import path
from collections import deque, defaultdict, Counter
from bisect import *
from string import ascii_lowercase
from functools import cmp_to_key
from random import randint
import heapq
 
 
def readInts():
    x = list(map(int, (sys.stdin.readline().rstrip().split())))
    return x[0] if len(x) == 1 else x
 
 
def readList(type=int):
    x = sys.stdin.readline()
    x = list(map(type, x.rstrip('\n\r').split()))
    return x
 
 
def readStr():
    x = sys.stdin.readline().rstrip('\r\n')
    return x
 
 
write = sys.stdout.write
read = sys.stdin.readline
 
 
MAXN = 1123456


class mydict:
    def __init__(self, func=lambda: 0):
        self.random = randint(0, 1 << 32)
        self.default = func
        self.dict = {}
 
    def __getitem__(self, key):
        mykey = self.random ^ key
        if mykey not in self.dict:
            self.dict[mykey] = self.default()
        return self.dict[mykey]
 
    def get(self, key, default):
        mykey = self.random ^ key
        if mykey not in self.dict:
            return default
        return self.dict[mykey]
 
    def __setitem__(self, key, item):
        mykey = self.random ^ key
        self.dict[mykey] = item
 
    def getkeys(self):
        return [self.random ^ i for i in self.dict]
 
    def __str__(self):
        return f'{[(self.random ^ i, self.dict[i]) for i in self.dict]}'

 
def lcm(a, b):
    return (a*b)//(math.gcd(a,b))
 
 
def mod(n):
    return n%(1000000007) 


#trajan
stack = []
onStack = []
ids = []
low = []
vis = []
graph = mydict(list)
cost = mydict(list)
ans = mydict(list)
def dfs(node):

    global stack, onStack, ids, low, graph, cost, vis, ans
    dfs_stack = [(node)]
    id = 0

    while dfs_stack:

        node = dfs_stack[-1]
        vis[node] = True

        if not onStack[node]:
            stack.append(node)
            onStack[node] = True

        if ids[node] is None:
            low[node] = ids[node] = id
            id += 1   

        flag = False

        for child in graph[node]:
            if not vis[child]:
                dfs_stack.append(child)
                flag = True

        if not flag:
            for child in graph[node]:
                if onStack[child]:
                    low[node] = min(low[node], low[child])

        if dfs_stack[-1] != node:
            # print(node)
            continue

        if low[node] == ids[node]:
            rr = []
            mn = float('inf')
            # print(node)
            while onStack[node]:
                cur = stack.pop()
                onStack[cur] = False
                low[cur] = ids[node]
                rr.append(cur)
                mn = min(cost[cur], mn)

            cnt = 0
            for num in rr:
                if cost[num] == mn:
                    cnt += 1

            ans[mn].append(cnt)

        dfs_stack.pop()


def solve(t):
    # print(f'Case #{t}: ', end = '')
    n = readInts()
    ar = readList() 
    global cost, graph, ans, ids, onStack, low, vis
    for i in range(n):
        cost[i+1] = ar[i]

    m = readInts()
    for _ in range(m):
        u, v = readInts()
        graph[u].append(v)

    ids = [None for _ in range(n+1)]
    onStack = [False for _ in range(n+1)]
    low = [None for _ in range(n+1)]
    vis = [False for _ in range(n+1)]

    for i in range(1, n+1):
        if not vis[i]:
            dfs(i)

    mncost = 0
    ways = 1    
    for num in ans.getkeys():
        mncost += num*len(ans[num])
        for dup in ans[num]:
            ways = mod(mod(ways)*mod(dup))

    print(mncost, ways)


def main():
    t = 1
    if path.exists("F:/Comp Programming/input.txt"):
        sys.stdin = open("F:/Comp Programming/input.txt", 'r')
        sys.stdout = open("F:/Comp Programming/output1.txt", 'w')
    # sys.setrecursionlimit(100000) 
    # t = readInts()
    for i in range(t):
        solve(i+1)
 
 
if __name__ == '__main__':
    main()  