There are plenty of problems that could be solved by finding the set of strongly connected components of a directed graph ..

In this topic I will explain what is a Strongly Connected Component? and how to find the set of all Strongly Connected Components of a directed graph.

In this topic I will explain what is a Strongly Connected Component? and how to find the set of all Strongly Connected Components of a directed graph.

### Strong Connected Components

A Strongly connected graph is a graph where each vertex could be visited from each other vertex of the graph, for example the following graph has 5 strongly connected components.

### Algorithm to find SCC

One of the most famous algorithms that is used to find strongly connected components of a graph is known by the

**Kosaraju's algorithm**. And It works as follows:- DFS all nodes and save visited nodes in a stack
**S**, push node only when finishing visit to all connected nodes. - Inverse the original graph by reversing all arcs E(U, V) to be E(V, U) instead
- Pop each vertex
**V**in**S**and DFS to get the strongly connected component that contains**V**.

### Problem

### To try this problem before reading my solution please visit CodeForces.

Your city has

To ensure the security, you have to build some police checkposts. Checkposts can only be built in a junction. A checkpost at junction

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.

*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.

#### Input

#### In the first line, you will be given an integer

*n*, number of junctions (1 ≤

*n*≤ 10

^{5}). In the next line,

*n*space-separated integers will be given. The

*i*

^{th}integer is the cost of building checkpost at the

*i*

^{th}junction (costs will be non-negative and will not exceed 10

^{9}).

The next line will contain an integer

*m*(0 ≤

*m*≤ 3·10

^{5}). And each of the next

*m*lines contains two integers

*u*

_{i}and

*v*

_{i}(1 ≤

*u*

_{i},

*v*

_{i}≤

*n*;

*u*≠

*v*). A pair

*u*

_{i},

*v*

_{i}means, that there is a one-way road which goes from

*u*

_{i}to

*v*

_{i}. There will not be more than one road between two nodes in the same direction.

#### Output

^{9}+ 7).