#include <bits/stdc++.h>
#define pb          emplace_back
#define ll          long long
#define fi          first
#define se          second
#define mp          make_pair
#define sz(x)       int(x.size())
//#define int         int64_t

using namespace std;

typedef pair<int, int> pii;
const int N = int(2e5) + 3;
const int logN = 18;

int n, p[N][logN + 1], d[N], sz[N], cnt[N];
int T, c[N], in[N], out[N], u, v;
ll sum[N]; bool vis[N];
vector<int> adj[N], col[N], g[N];

int lca(int u, int v) {
    if(d[u] < d[v]) swap(u, v);
    for(int i = logN; i >= 0; --i)
        if(d[u] - (1 << i) >= d[v]) u = p[u][i];
    if(u == v) return u;
    for(int i = logN; i >= 0; --i)
        if(p[u][i] != p[v][i]) u = p[u][i], v = p[v][i];
    return p[u][0];
}

void solve(int curcol) {
    if(sz(col[curcol]) < 1) {cout << "0\n"; return;}
    vector<pii> node; vector<int> st;
    for(int x: col[curcol]) node.pb(in[x], x);
    sort(node.begin(), node.end());
    for(int i = 1; i < sz(col[curcol]); ++i) {
        u = lca(node[i].se, node[i - 1].se);
        node.pb(in[u], u);
    }
    node.pb(in[1], 1);
    sort(node.begin(), node.end());
    node.erase(unique(node.begin(), node.end()), node.end());
    for(auto cur: node) {
        g[cur.se].clear();
        while(st.size() && out[st.back()] < cur.fi) st.pop_back();
        if(st.size()) g[st.back()].pb(cur.se);
        st.pb(cur.se);
    }
    ll res = 0;
    function<int(int, int)> jump = [&](int x, int k) {
        for(int i = logN; i >= 0; --i)
            if(d[x] - (1 << i) > k) x = p[x][i];
        return x;
    };
    function<void(int, int)> dfs = [&](int u, int out) {
       int org = out;
       for(int v: g[u]) {
           dfs(v, org);
           org -= sum[v];
           sum[u] += sum[v];
       }
       if(c[u] == curcol) {
          vector<int> val; int tmp;
          ll s = 0, cur = 0;
          for(int v: g[u]) {
              vis[tmp = jump(v, d[u])] = 1;
              val.pb(sz[tmp] - sum[v]);
              s += val.back();
          }
          for(int v: adj[u]) {
              if(!vis[v]) {
                  if(v == p[u][0]) val.pb(out - sz[u]);
                  else val.pb(sz[v] - sum[v]);
                  s += val.back();
              }
              vis[v] = 0;
          }
          for(int x: val) cur += 1ll * x * (s - x);
          cur >>= 1;
          cur += org;
          sum[u] = sz[u];
          res += cur;
       }
    }; dfs(1, n);
    for(auto cur: node) sum[cur.se] = 0;
    cout << res << '\n';
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    #define Task        "test"
    if(fopen(Task".inp", "r")) {
        freopen(Task".inp", "r", stdin);
        freopen(Task".out", "w", stdout);
    }
    cin >> n;
    for(int i = 1; i <= n; ++i) {
        cin >> c[i];
        col[c[i]].pb(i);
    }
    for(int i = 1; i < n; ++i) {
        cin >> u >> v;
        adj[u].pb(v), adj[v].pb(u);
    }
    function<void(int)> dfs = [&](int u) {
        in[u] = ++T; sz[u] = 1;
        for(int i = 1; i <= logN; ++i) p[u][i] = p[p[u][i - 1]][i - 1];
        for(int v: adj[u]) {
            if(v == p[u][0]) continue;
            d[v] = d[u] + 1;
            p[v][0] = u;
            dfs(v); sz[u] += sz[v];
        }
        out[u] = T;
    }; dfs(1);
    for(int i = 1; i <= n; ++i) solve(i);
}


Measured memory_bytes: 74320