require 'set'
require 'tsort'

n = gets.to_i
contribute = [0] + gets.split.map(&:to_i)

g = {}
(1..n).map {|i| g[i] = Set.new}
m = gets.to_i
neighbour = (0..n).map { Set.new }
m.times do
    u, v = gets.split.map(&:to_i)
    g[v] << u if contribute[u] <= contribute[v]
    g[u] << v if contribute[u] >= contribute[v]
    neighbour[u] << v
    neighbour[v] << u
end

(1..n).each do |c|
    neighbour[c].sort_by{|v| contribute[v]}.each_cons(2) do |u, v|
        if contribute[u] == contribute[v]
            g[v] << u
            g[u] << v
        else
            g[v] << u
        end
    end
end


class << g
    include TSort
    alias tsort_each_node each_key

    def tsort_each_child(u)
        self[u].each {|v| yield v}
    end
end

scc_size = Hash.new(1)
g.each_strongly_connected_component do |scc|
    next if scc.size == 1
    rep = scc[0]
    scc_size[rep] = scc.size
    set = Set[*scc[1..-1]]
    set.each do |u|
        next if u == rep
        g[u].each do |v|
            g[rep] << v if v != rep
        end
        g.delete(u)
        g.each do |v, a|
            next if v == rep
            if a.include?(u)
                a.delete(u)
                a << rep
            end
        end
        g[rep].delete(u)
    end

end

salary = {}
g.tsort.each do |u|
    salary[u] = (g[u].map {|v| salary[v]}.max || 0) + 1
end

p g.keys.map {|v| salary[v] * scc_size[v]}.inject(:+)