// Problem: E - Unique Color
// Contest: AtCoder - AtCoder Beginner Contest 198
// URL: https://atcoder.jp/contests/abc198/tasks/abc198_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define int long long
#define ld long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a,b,sizeof(a))
#define vi vector<int>
#define vii vector<pair<int,int>>
#define setbits(x) __builtin_popcountll(x)
#define w(x)  int x; cin>>x; while(x--)
#define in(a) for(auto &x: a)cin>>x;
#define out(a) for(auto x: a){cout<<x<<'\n';}
#define inf 1000000000
//#define mm 1000000007 998244353
void print(bool n){if(n)cout<<"YES";else cout<<"NO";}
vector<vi> adj;
vi c;
vi ans;
multiset <int, greater <int>> col;
void dfs(int v,int par){
	if(col.empty() || col.find(c[v])==col.end())ans.pb(v+1);
	col.insert(c[v]);
	for(auto u: adj[v]){
		if(u==par)continue;
		dfs(u,v);
	}
	col.erase(col.find(c[v]));
}
signed main(){
	fastio
	int n;
	cin>>n;
	c.resize(n);
	in(c);
	adj.resize(n);
	rep(i,0,n-1){
		int x,y;
		cin>>x>>y;
		--x;--y;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	dfs(0,-1);
	sort(all(ans));
	out(ans);
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int mod=998244353;
int n;
int a[200005];
int b[200005];
int vis[200005];

int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        b[a[i]]++;
    }
    int tmp=1;
    for (int i=1;i<=n;i++)
    {
        if (vis[i]==0 && b[i]>0)
        {
            vis[i]=i;
            int x=a[i];
            while (vis[x]==0)
            {
                vis[x]=i;
                x=a[x];
            }
            if (vis[x]==i) tmp=tmp*2%mod;
        }
    }
    cout<<tmp-1<<endl;
    return 0;
}
