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 
import collections

sys.setrecursionlimit(200000)


n = int(sys.stdin.readline())
costs = list(map(int, (sys.stdin.readline().split(" "))))
m = int(sys.stdin.readline())

adj_list = collections.defaultdict(list)
ral = collections.defaultdict(list)

# adj_list, ral = [[] for _ in range(n)], [[] for _ in range(n)]
for _ in range(m):
    u, v = tuple(map(int, (sys.stdin.readline().split(" "))))
    adj_list[u-1].append(v-1)
    ral[v - 1].append(u-1)

visited = [False for _ in range(n)]
visited_order = []

def dfs(node):
    visited[node] = True
    for neighbor in adj_list[node]:
        if not visited[neighbor]:
            dfs(neighbor)
    visited_order.append(node)

def dfs2(node):
    visited[node] = True
    mc, uw = (costs[node], 1)
    for neighbor in ral[node]:
        if not visited[neighbor]:
            neighbor_min_cost, neighbor_min_cost_count = dfs2(neighbor)
            if neighbor_min_cost < mc:
                mc = neighbor_min_cost
                uw = neighbor_min_cost_count
            elif neighbor_min_cost == mc:
                uw += neighbor_min_cost_count
    return (mc, uw)

for i in range(n):
    if not visited[i]:
        dfs(i)

visited = [False for _ in range(n)]
unique_ways = 1
min_cost = 0
while visited_order:
    last_visited_in_order = visited_order.pop()
    if not visited[last_visited_in_order]:
        mc, uw = dfs2(last_visited_in_order)
        min_cost += mc
        unique_ways = unique_ways*uw % 1000000007

print(min_cost, unique_ways % 1000000007)

