#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353,INF=1e18;
#define ll long long
#define pll pair<ll,ll>
#define x first
#define y second
#define p_all(arr) for(auto i:arr){cout<<i<<" ";}cout<<"\n";
vector<ll> par,sze;
ll root(ll a)
{
    if(a==par[a])
    return a;
    return par[a]=root(par[a]);
}
void union1(ll a,ll b)
{
    a=root(a),b=root(b);
    if(a==b)
    return ;
    if(sze[a]<sze[b])
    swap(a,b);
    sze[a]+=sze[b];
    par[b]=a;
}
ll power(ll a,ll b)
{
    ll res=1;
    while(b)
    {
        if(b&1)
        {
            res=(res*a)%mod;
        }
        a=(a*a)%mod;
        b>>=1;
    }
    return res;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    ll t=1;
    // cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        par.resize(n);
        sze.resize(n,1);
        for(int i=0;i<n;i++)
        par[i]=i;
        vector<vector<ll> > a(n);
        for(int i=0;i<n;i++)
        {
            ll u;
            cin>>u;
            u--;
            union1(u,i);
        }
        ll ans=0;
        for(int i=0;i<n;i++)
        if(root(i)==i)
        {
            ans++;
        }
        ans=power(2,ans);
        ans+=(mod-1);
        ans%=mod;
        cout<<ans<<"\n";
    }
    return 0;
}
using namespace std;
#include "bits/stdc++.h"

// add your library with double quotation #include"" here.
// #include "atcoder/all"
// using namespace atcoder;

// define your macros here.
// #define REP(a,b) for(long long a = 0;a < b;++a)

using namespace std;
typedef string::const_iterator State;
#define eps 1e-8L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef long double ld;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

#define int long long
vector<int> vertexs[300000];
int dists[300000];
int backs[300000];
int ans[300000];
int doned[300000];
int dfs(int now,int back){
    for(auto x:vertexs[now]){
        if(x == back) continue;
        backs[x] = now;
        dists[x] = dists[now] + 1;
        dfs(x,now);
    }
    return 0;
}
int value = 0;
int dfs2(int now,int back){
    value++;
    ans[now] = value;
    for(auto x:vertexs[now]){
        if(x == back) continue;
        dfs2(x,now);
    }
        value++;
    return 0;
}
void solve(){
    // write your solution here.
    int n;
    cin >> n;
    REP(i,n-1){
        int a,b;
        cin >> a >> b;
        a--;b--;
        vertexs[a].push_back(b);
        vertexs[b].push_back(a);
    }
    dfs(0,-1);
    pair<int,int> best = mp(0,0);
    REP(i,n){
        if(dists[i] > dists[best.first]){
            best.first = i;
        }
    }
    dists[best.first] = 0;
    backs[best.first] = -1; 
    dfs(best.first,-1);
    REP(i,n){
        if(dists[i] > dists[best.second]){
            best.second = i;
        }
    }
    while(true){
        doned[best.second] = 1;
        value++;
        ans[best.second] = value;
        for(auto x:vertexs[best.second]){
            if(doned[x] == true or x == backs[best.second]) continue;
            dfs2(x,best.second);
        }
        if(best.first == best.second) break;
        best.second = backs[best.second];
    }
    REP(i,n){
        if(i != 0) cout << " ";
        cout << ans[i];
    }
    cout << endl;
}
#undef int

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
    // Fasterize input/output script
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(100);
    // scanf/printf user should delete this fasterize input/output script

    int t = 1;
    //cin >> t; // comment out if solving multi testcase
    for(int testCase = 1;testCase <= t;++testCase){
        solve();
    }
    return 0;
}