//#include<bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<stack>
#include<vector>
//#include<conio.h>
#include<string>
#include<map>
#include<cstdlib>
#include<queue>
#include<math.h>
#include<time.h>
#include<set>
#include<cstdio>
#include<stdio.h>
#include<algorithm>
using namespace std;
using std::cin;
using std::cout;
using std::endl;
namespace IN{
    const int MAX_INPUT = 1000000;
    #define getc() (p1==p2&&(p2=(p1=buf)+inbuf->sgetn(buf,MAX_INPUT),p1==p2)?EOF:*p1++)
    char buf[MAX_INPUT],*p1,*p2;
    template<typename T>inline bool read(T &x) {
        static std::streambuf *inbuf=cin.rdbuf();
        x=0;
        register int f=0,flag=false;
        register char ch=getc();
        while(!isdigit(ch)){
            if (ch=='-') f=1;
        	ch=getc();
        }
        if(isdigit(ch)) x=x*10+ch-'0',ch=getc(),flag=true;
        while(isdigit(ch)) {
            x=x*10+ch-48;
            ch=getc();
        }
        x=f?-x:x;
        return flag;
    }
    template<typename T,typename ...Args>inline bool read(T& a,Args& ...args) {
       return read(a)&&read(args...);
    }
    #undef getc
}

namespace OUT{
    template<typename T>inline void put(T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top=0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc('\n');
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc('\n');
    }
    inline void putc(const char ch){
        static std::streambuf *outbuf=cout.rdbuf();
        outbuf->sputc(ch);
    }
    inline void putstr(string s){
    	for(register int i=0;i<s.length();i++) putc(s[i]);
	}
    template<typename T>inline void put(const char ch,T x){
        static std::streambuf *outbuf=cout.rdbuf();
        static char stack[21];
        static int top = 0;
        if(x<0){
            outbuf->sputc('-');
            x=-x;
        }
        if(!x){
            outbuf->sputc('0');
            outbuf->sputc(ch);
            return;
        }
        while(x){
            stack[++top]=x%10+'0';
            x/=10;
        }
        while(top){
            outbuf->sputc(stack[top]);
            --top;
        }
        outbuf->sputc(ch);
    }
    template<typename T,typename ...Args> inline void put(T a,Args ...args){
        put(a);put(args...);
    }
    template<typename T,typename ...Args> inline void put(const char ch,T a,Args ...args){
        put(ch,a);put(ch,args...);
    }
}
using IN::read;
using OUT::put;
using OUT::putc;
using OUT::putstr;
int n,t[205],flag;
int main(int argc, char const *argv[]){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    read(n);
    for(int i=1;i<=n;i++) read(t[i]);
    sort(t+1,t+n+1);
    for(int i=2;i<=n;i++)
    	if(t[i]!=t[i-1]) return put('\n',t[i]),0;
    putstr("NO\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	ios::sync_with_stdio(false);
	int n,x,a[201],two = 2;
	memset(a,0,sizeof(a));
	cin >> n;
	for (int i = 1 ; i <= n ; i++) {
		cin >> x;
		a[x + 100]++;
	}
	for (int i = 0 ; i <= 200 ; i++) {
		if (a[i] == 0) continue;
		if (two == 1) {
			cout << i - 100;
			break;
		}
		else two--;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,s;
    cin>>n;
    int a[100001];
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    int fxt;
    fxt=a[1];
    while(s<n){
        s++;
        if(a[s]!=fxt){
            cout<<a[s]<<endl;
            return 0;
        }
    }
    cout<<"NO"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[1000]={101};
int main()
{
    int x=101,xx=101;
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    x=a[1];
    for(int i=2;i<=n;i++)
    {
        if(a[i]<x)
        {
            xx=x;
            x=a[i];
        }
        else
        {
            if(a[i]<xx&&a[i]!=x)
                xx=a[i];
        }
    }
    if(xx==101)
        cout<<"NO";
    else
        cout<<xx;
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int n,k=2,g,s=0,max=0;
    int a[10002]={0};
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>g;
        a[g]=1;
        if(g>max) max=g;
    }
    for(int i=1;i<=max;i++)
        if(a[i]==1)
        {
            s++;
            if(s==k)
            {
                cout<<i;
                return 0;
            }
        }
    cout<<"NO RESULT";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	register int n;
	scanf("%d",&n);
	set<int> s;
	for(register int i=1;i<=n;i++)
	{
		int p;
		cin>>p;#include<bits/stdc++.h>
using namespace std;
int main()
{
	register int n;
	scanf("%d",&n);
	set<int> s;
	for(register int i=1;i<=n;i++)
	{
		int p;
		cin>>p;
		s.insert(p);
	}
	set<int> ::iterator it;
	for(it=s.begin();it!=s.end();it++)
	{
		if(*it!=*s.begin())
		{
			printf("%d\n",*it);
			return 0;
		}
	}
}
		s.insert(p);
	}
	set<int> ::iterator it;
	for(it=s.begin();it!=s.end();it++)
	{
		if(*it!=*s.begin())
		{
			printf("%d\n",*it);
			return 0;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int n[101];
int main(){
	int a;
	cin>>a;
	int ans=a;
	for(int i=0;i<a;i++)cin>>n[i];
	sort(n,n+a);
	for(int i=1;i<a;i++){
		if(n[i]==n[i-1]){
			n[i-1]=-76897643;
			ans--;
		}
	}
	int gg=0;
	for(int i=0;i<a;i++) {if(n[i]!=-76897643)gg++;if(gg==2 and n[i]!=-76897643){cout<<n[i]<<endl;}}
	return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <queue>

using namespace std;

int main()
{

	int a;

	cin >> a;

	int s[a+2];

	for (int i = 1; i <= a; ++i)
	{
		cin >> s[i];
	}

	sort(s+1, s+a+1);

	for (int i = 1; i <= a; ++i)
	{
		if (s[i] > s[1])
		{
			cout << s[i];
			return 0;
		}
	}

	cout << "NO";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[10001],k,n,s;
int main()
{
    cin>>n;
    for(register int i=1;i<=n;i++)
    	cin>>a[i];
    sort(a+1,a+1+n);
    s=1;
    for(register int i=2;i<=n;i++)
	{
        if(a[i]!=a[i-1])
			s++;
        if(s==2)
		{
            cout<<a[i];
        	return 0;
   		}
    }
    cout<<"NO";
    return 0;
}


#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<iomanip>
using namespace std;
int main(){
	int n,a[110],mas=100000000,mass=100000000;
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)if(a[i]<mas)mas=a[i];
	for(int i=1;i<=n;i++)if(a[i]==mas)a[i]=10000000;
	for(int i=1;i<=n;i++)if(a[i]<mass&&a[i]>=0){
		mass=a[i];
	}
	if(mass==10000000)cout<<"NO";
	else
	cout<<mass;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int const N=100+10;
int n,i,a[N];
int main()
{
	cin>>n;
	for (i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	sort(a+1,a+n+1);
	for (i=1;i<n;i++)
	{
		if (a[i]!=a[i+1])
		{
			cout<<a[i+1]<<endl;
			return 0;
		}
	}
	cout<<"NO"<<endl;
	return 0;
}


#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
vector<int> a;
int main(){
    int n,s;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&s);
        a.push_back(s);
    }
    vector<int>::iterator qwq;
    sort(a.begin(),a.end());
    qwq=unique(a.begin(),a.end());
    a.erase(qwq,a.end());
    if(a.size()>=2)cout<<a[1];
        else cout<<"NO";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN=100+10;
int a[MAXN];
int b[MAXN];
int main()
{
	int minn=0;
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		minn=min(a[i],minn);
	}
	bool pd=true;
	int small=1001;
	for(int i=1;i<=n;i++){
		if(a[i]>minn&&a[i]<small){
		small=a[i];
		pd=false;
		break;
		}
	}
	if(pd)cout<<"NO"<<endl;
	else
	cout<<small<<endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,a[101];
	cin>>n;
	if(n<2)
	{
		cout<<"NO";
		return 0;
	}
	for(int i=1;i<=n;i++)
		cin>>a[i];
	sort(a+1,a+n+1);
	cout<<a[2];
	return 0;
}


#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
long long a,b,x[10001];
int main()
{
	cin>>a;
	for(int i=1;i<=a;i++)cin>>x[i];
	sort(x+1,x+a+1);
	for(int i=2;i<=a;i++)
	{
	if(a[i]!=a[i-1])
	{
	cout<<a[i]<<endl;
	return 0;
	}
	}
	cout<<"NO"<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int a[101],ll;
int main()
{
    int n,s=0,maxn=-99999999;cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    {
    	if(a[i]>maxn)s++,maxn=a[i];
    	if(s==2){cout<<a[i]<<endl;return 0;}
	}
}


#include<bits/stdc++.h>
#include<set>
using namespace std;
int main(){
	int n;
	set<int>s;
	set<int>::iterator it;
	cin>>n;
	while(n--){
		int p;
		cin>>p;
		s.insert(p);
	}
	if(s.size()==1){
		cout<<"NO"<<endl;
	}
	else{
	it=s.begin();
	cout<<*(++it)<<endl;
	}
}


#include<iostream>
#include<algorithm>
using namespace std;
long long n,k,a[1001],ans;
int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    unique(a,a+n)-a;
	if(a[1]==a[0])
		cout<<a[1];
	else
    	cout<<"NO\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[105];
bool check(int i){
	for (int j=i-1;j>=0;j--)
		if (a[j]!=0&&a[j]==a[i]) return false;
	return true;
}
int main(){
	int n;
	cin>>n;
	for (int i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	for(int i=0;i<n;i++)
		if (!check(i)) a[i]=0;
	for (int i=0;i<2;i++){
		if (a[i]==0) i--;
		if (i==1) cout<<a[i];
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[101];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
    }
    sort(a+1,a+n+1);
    cout<<a[2];
    return 0;
}


#include<iostream>
using namespace std;
int sum,ans,a,n;
int flag[110];
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a;
		flag[a]=999;
	}
	for(int i=-105;i<n;i++)
	{
		if(flag[i]==999)
			sum++;
		if(sum==2)
			{cout<<i;return 0;}
	}
	cout<<"NO";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
set<int>s;
int main()
{
	int n,x;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>x;
		s.insert(x);
	}
	s.erase(s.begin());
	if(s.empty()) cout<<"NO";
	else cout<<*s.begin();
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
    int a[2000],n,xq=0,z;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>z;
        a[z+200]=1;
    }
    for(int i=0;i<=2000;i++)
    {
        if(a[i]==1)
        {
            xq++;
            if(xq==2)
            {
                cout<<i-200<<endl;
                return 0;
            }
        }
    }
    cout<<"NO"<<endl;
    return 0;
}


#include<iostream>
#include<set>
using std::set;
using std::cin;
using std::cout;
set <int>a;
int main()
{
	int b;
	cin>>b;
	for(int i=1;i<=b;i++)
	{
		int c;
		cin>>c;
		a.insert(c);
	}
	int n=a.size();
	if(n==1) printf("NO");
	else
	a.erase(*a.begin());
	cout<<*a.begin();
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int n;
    cin>>n;
    int a[n+1],i;
    for (i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    i=1;
    int t;
    t=a[1];
    while (i<n){
        i++;
        if (a[i]!=t)
        {
            cout<<a[i]<<endl;
            return 0;
        }
    }
    cout<<"NO"<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,b[100],sb=1;
int main()
{
	cin>>n;
	int a[n];
	for(int i=1;i<=n;i++)
	    cin>>a[i];
		int ssb=1;
		sort(a+1,a+n+1);
		for(int i=2;i<=n;i++)
		{
			if(a[i]!=a[i-1])
			ssb++;
			if(ssb==2)
			{
				cout<<a[i];
				return 0;
			}
		}
	cout<<"NO";
   	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
	int n;
	int first;
	cin >> n;
	int a[n];
	for(int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort(a, a + n);
	first = a[0];
	int i = 0;
	for(; i < n; i++)
	{
		if(a[i] > first)
		{
			cout << a[i];
			return 0;
		}
	}
	cout << first;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
	return a<b;
}
int main(){
	int a;
	int c[200];
	scanf("%d",&a);
	for(int i=0;i<a;i++){
		scanf("%d",&c[i]);
	}
	sort(c,c+a,cmp);
	for(int i=1;;i++){
		if(c[i]!=c[i-1]){
			printf("%d",c[i]);
			return 0;
		}
	}
	return 0;
}


#include<iostream>
#include <algorithm>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	for(int i=0;i<n;i++)
	{
		if(a[i]>a[0])
		{
			cout<<a[i]<<endl;
			break;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[100001];
int main()
{
	int n,x;
	cin>>n;
	for(int i=1; i<=n; i++) cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=2; i<=n; i++)
	{
		if(a[i]!=a[i-1])
		{
			cout<<a[i]<<endl;
			return 0;
		}
	}
	cout<<"NO"<<endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n,x;
set<int> s;//不可重集合容器，插入元素后满足元素互异性

int main ()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        s.insert(x);//插入元素
    }
    s.erase(s.begin());//删除第一个也是最小的元素
    if(s.empty())
        puts("NO");
    else
        cout<<*s.begin()<<endl;//输出当前第一个也是当前最小的元素
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[110];
int main()
{
    cin>>n;
    for(int i=1;i<=n;++i)
    	cin>>a[i];
    sort(a+1,a+n+1);
    int x=0;
    a[n+1]=-0x3f3f3f3f;
    for(int i=n;i>0;--i)
    {
    	if(a[i]!=a[i+1])
    		x++;
    	if(x==2)
    	{
    		cout<<a[i]<<endl;
    		return 0;
		}
	}
	cout<<"NO"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
priority_queue <int,vector<int>,greater<int> > q;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;cin>>x;q.push(x);
	}
    while(1){
        n=q.top();
        q.pop();
        if(n!=q.top())  break;
    }
	cout<<n;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN=100+10;
int a[MAXN];
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	sort(a,a+n+1);
	for(int i=1;i<=n;i++){
		if(a[i]!=a[i+1]){
		cout<<a[i+1]<<endl;
		break;
		}
	}
	return 0;
}


#include<iostream>
using namespace std;
bool a[201];
int main(){
	int n,x;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x;
		if(x>=0)
		a[x+100]=1;
		else
		a[100+x]=1;
	}
	for(int i=0,ans=0;;i++){
		if(a[i])ans++;
		if(ans==2){
			if(i>=100)
				cout<<i-100;
			else
				cout<<i-100;
			return 0;
		}
	}

}


#include<bits/stdc++.h>
using namespace std;
int a[101];
int main()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; ++i)
		cin >> a[i];
	sort(a + 1, a + n + 1);
	int cnt = 1;
	for(int i = 2; i <= n; ++i)
	{
		if(a[i] != a[i - 1])
			++cnt;
		if(cnt == 2)
		{
			cout << a[i] << endl;
			return 0;
		}
	}
	cout << "NO" << endl;
	return 0;
}


#include<cstdio>
#include<set>
std::set<int> box;
int main()
{
	int n;
	scanf("%d",&n);
	for(int i = 0;i < n;i++)
	{
		int x;
		scanf("%d",&x);
		box.insert(x);
	}
	box.erase(box.begin());
	if(box.empty()) printf("NO");
	else printf("%d",*box.begin());
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n;
int a[110];
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=2;i<=n;i++)
		if(a[i]!=a[i-1]){cout<<a[i];return 0;}
	cout<<"NO";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,a[100],b[100],sb=1;
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	    cin>>a[i];
	if(n<2)
	cout<<"NO"<<endl;
	else
	{
		sort(a+1,a+n+1);
    for(int i=2;i<=n;i++)
        if(a[i]==a[i-1]) a[i-1]=101;
    for(int i=1;i<=n;i++)
        if(a[i]!=101)
	    {
	    	b[sb]=a[i];
	    	sb++;
	    }
	if(sb>=2)
    cout<<b[2]<<endl;
	}

   	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n+1],i;
    for (i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    i=1;
    int fxt;
    fxt=a[1];
    while (i<n)
    {
        i++;
        if (a[i]!=fxt)
        {
            cout<<a[i];
            return 0;
        }
    }
    cout<<"NO";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;
typedef long long ll;

vector <int> adj[N];
long long dp[N], ans[N];

long long mul(ll a, ll b)
{
	return (1LL * a * b) % mod;
}

void dfs_down(int v)
{
	for (auto u : adj[v])
	{
		dfs_down(u);
		dp[v] = mul(dp[v], dp[u] + 1);
	}
}

void dfs_up(int v)
{
	int s = adj[v].size();
	if(!s)	return;
	vector <int> l(s), r(s);
	l[0] = r[s - 1] = 1;
	for (int i = 1; i < s; i++)
		l[i] = mul(l[i - 1], dp[adj[v][i - 1]] + 1);
	for (int i = s - 2; ~i; i--)
		r[i] = mul(r[i + 1], dp[adj[v][i + 1]] + 1);
	for (int i = 0; i < s; i++)
	{
		int u = adj[v][i];
		ans[u] = (1LL * (1LL * l[i] * r[i]) % mod) * 1LL * ans[v] + 1 % mod;
		dfs_up(u);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int v;
		cin >> v;
		v--;
		adj[v].push_back(i);
	}
	for (int i = 0; i < n; i++)	dp[i] = ans[i] = 1;
	dfs_down(0);
	dfs_up(0);
	for (int i = 0; i < n; i++)
		cout << mul(dp[i], ans[i]) << " ";
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;
typedef long long ll;

vector <int> adj[N];
int dp[N], ans[N];

int mul(ll a, ll b)
{
	return (1LL * a * b) % mod;
}

void dfs_down(int v)
{
	for (auto u : adj[v])
	{
		dfs_down(u);
		dp[v] = mul(dp[v], dp[u] + 1);
	}
}

void dfs_up(int v)
{
	int s = adj[v].size();
	if(!s)	return;
	vector <int> l(s), r(s);
	l[0] = r[s - 1] = 1;
	for (int i = 1; i < s; i++)
		l[i] = mul(l[i - 1], dp[adj[v][i - 1]] + 1);
	for (int i = s - 2; ~i; i--)
		r[i] = mul(r[i + 1], dp[adj[v][i + 1]] + 1);
	for (int i = 0; i < s; i++)
	{
		int u = adj[v][i];
		ans[u] = ((1LL * l[i] * r[i]) % mod) * ans[v] + 1 % mod;
		dfs_up(u);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int v;
		cin >> v;
		v--;
		adj[v].push_back(i);
	}
	for (int i = 0; i < n; i++)	dp[i] = ans[i] = 1;
	dfs_down(0);
	dfs_up(0);
	for (int i = 0; i < n; i++)
		cout << mul(dp[i], ans[i]) << " ";
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;
typedef long long ll;

vector <int> adj[N];
long long dp[N], ans[N];

long long mul(ll a, ll b)
{
	return (1LL * a * b) % mod;
}

void dfs_down(int v)
{
	for (auto u : adj[v])
	{
		dfs_down(u);
		dp[v] = mul(dp[v], dp[u] + 1);
	}
}

void dfs_up(int v)
{
	int s = adj[v].size();
	if(!s)	return;
	vector <int> l(s), r(s);
	l[0] = r[s - 1] = 1;
	for (int i = 1; i < s; i++)
		l[i] = mul(l[i - 1], dp[adj[v][i - 1]] + 1);
	for (int i = s - 2; ~i; i--)
		r[i] = mul(r[i + 1], dp[adj[v][i + 1]] + 1);
	for (int i = 0; i < s; i++)
	{
		int u = adj[v][i];
		ans[u] = (1LL * (1LL * l[i] * r[i]) % mod) * ans[v] + 1 % mod;
		dfs_up(u);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int v;
		cin >> v;
		v--;
		adj[v].push_back(i);
	}
	for (int i = 0; i < n; i++)	dp[i] = ans[i] = 1;
	dfs_down(0);
	dfs_up(0);
	for (int i = 0; i < n; i++)
		cout << mul(dp[i], ans[i]) << " ";
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;
typedef long long ll;

vector <int> adj[N];
long long dp[N], ans[N];

void dfs_down(int v)
{
	for (auto u : adj[v])
	{
		dfs_down(u);
		dp[v] = 1LL * dp[v] * (dp[u] + 1) % mod;
	}
}

void dfs_up(int v)
{
	int s = adj[v].size();
	if(!s)	return;
	vector <ll> l(s), r(s);
	l[0] = r[s - 1] = 1;
	for (int i = 1; i < s; i++)
		l[i] = 1LL * l[i - 1] * (dp[adj[v][i - 1]] + 1) % mod;
	for (int i = s - 2; ~i; i--)
		r[i] = 1LL * r[i + 1] * (dp[adj[v][i + 1]] + 1) % mod;
	for (int i = 0; i < s; i++)
	{
		int u = adj[v][i];
		ans[u] = (1LL * (1LL * (l[i] * r[i]) % mod) * ans[v] + 1) % mod;
		dfs_up(u);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int v;
		cin >> v;
		v--;
		adj[v].push_back(i);
	}
	for (int i = 0; i < n; i++)	dp[i] = ans[i] = 1;
	dfs_down(0);
	dfs_up(0);
	for (int i = 0; i < n; i++)
		cout << 1LL * dp[i] * ans[i] % mod << " ";
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;
typedef long long ll;

vector <int> adj[N];
long long dp[N], ans[N];

void dfs_down(int v)
{
	for (auto u : adj[v])
	{
		dfs_down(u);
		dp[v] = 1LL * dp[v] * (dp[u] + 1) % mod;
	}
}

void dfs_up(int v)
{
	int s = adj[v].size();
	if(!s)	return;
	vector <ll> l(s), r(s);
	l[0] = r[s - 1] = 1;
	for (int i = 1; i < s; i++)
		l[i] = 1LL * l[i - 1] * (dp[adj[v][i - 1]] + 1) % mod;
	for (int i = s - 2; ~i; i--)
		r[i] = 1LL * r[i + 1] * (dp[adj[v][i + 1]] + 1) % mod;
	for (int i = 0; i < s; i++)
	{
		int u = adj[v][i];
		ans[u] = 1LL * l[i] * r[i] % mod * ans[v] + 1 % mod;
		dfs_up(u);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int v;
		cin >> v;
		v--;
		adj[v].push_back(i);
	}
	for (int i = 0; i < n; i++)	dp[i] = ans[i] = 1;
	dfs_down(0);
	dfs_up(0);
	for (int i = 0; i < n; i++)
		cout << 1LL * dp[i] * ans[i] % mod << " ";
	cout << "\n";
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;
typedef long long ll;

vector <int> adj[N];
long long dp[N], ans[N];

long long mul(ll a, ll b)
{
	return (1LL * a * b) % mod;
}

void dfs_down(int v)
{
	for (auto u : adj[v])
	{
		dfs_down(u);
		dp[v] = mul(dp[v], dp[u] + 1);
	}
}

void dfs_up(int v)
{
	int s = adj[v].size();
	if(!s)	return;
	vector <int> l(s), r(s);
	l[0] = r[s - 1] = 1;
	for (int i = 1; i < s; i++)
		l[i] = mul(l[i - 1], dp[adj[v][i - 1]] + 1);
	for (int i = s - 2; ~i; i--)
		r[i] = mul(r[i + 1], dp[adj[v][i + 1]] + 1);
	for (int i = 0; i < s; i++)
	{
		int u = adj[v][i];
		ans[u] = ((1LL * l[i] * r[i]) % mod) * ans[v] + 1 % mod;
		dfs_up(u);
	}
}

int main()
{
	int n;
	cin >> n;
	for (int i = 1; i < n; i++)
	{
		int v;
		cin >> v;
		v--;
		adj[v].push_back(i);
	}
	for (int i = 0; i < n; i++)	dp[i] = ans[i] = 1;
	dfs_down(0);
	dfs_up(0);
	for (int i = 0; i < n; i++)
		cout << mul(dp[i], ans[i]) << " ";
	cout << "\n";
}


#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

typedef long long int ll;

ll in[200001], out[200001];

vector<int> adj[200001];

ll fast_pow(ll a, ll b){
    ll ans = 1;
    while(b){
        if(b & 1) ans = (ans * a) % mod;
        b /= 2;
        a = (a * a) % mod;
    }
    return ans;
}

inline ll inverse(ll a){
    return fast_pow(a, mod-2);
}

void dfs(int src, int par){
    ll ans = 1;
    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;
        dfs(cur, src);
        ans = (ans * (in[cur] + 1)) % mod;
    }
    in[src] = ans;
}

void dfs_out(int src, int par){
    ll tot = 1;
    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;
        tot = (tot * (in[cur] + 1) % mod) % mod;
    }

    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;

        ll val = (tot * inverse(in[cur] + 1) % mod) % mod;
        val = (val * out[src]) % mod;
        out[cur] = (val + 1) % mod;

        dfs_out(cur, src);
    }
}

int main(){
    int n;
    cin >> n;
    for(int j = 2; j <= n; j++){
        int x;
        cin >> x;
        adj[x].push_back(j);
    }

    dfs(1, 0);
    out[1] = 1;
    dfs_out(1, 0);

    for(int j = 1; j <= n; j++)
        cout << (in[j] * out[j]) % mod << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

typedef long long int ll;

ll in[200001], out[200001];

vector<int> adj[200001];

ll fast_pow(ll a, ll b){
    ll ans = 1;
    while(b){
        if(b & 1) ans = (ans * a) % mod;
        b /= 2;
        a = (a * a) % mod;
    }
    return ans;
}

inline ll inverse(ll a){
    return fast_pow(a, mod-2);
}

void dfs(int src, int par){
    ll ans = 1;
    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;
        dfs(cur, src);
        ans = (ans * (in[cur] + 1)) % mod;
    }
    in[src] = ans;
}

void dfs_out(int src, int par){
    ll tot = 1;
    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;
        tot = (tot * (in[cur] + 1)) % mod;
    }

    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;

        ll val = (tot * inverse(in[cur] + 1)) % mod;
        val = (val * out[src]) % mod;
        out[cur] = (val + 1) % mod;

        dfs_out(cur, src);
    }
}

int main(){
    int n;
    cin >> n;
    for(int j = 2; j <= n; j++){
        int x;
        cin >> x;
        adj[x].push_back(j);
    }

    dfs(1, 0);
    out[1] = 1;
    dfs_out(1, 0);

    for(int j = 1; j <= n; j++)
        cout << (in[j] * out[j]) % mod << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
#define mod 1000000007

using namespace std;

typedef long long int ll;

ll in[200001], out[200001];

vector<int> adj[200001];

ll fast_pow(ll a, ll b){
    ll ans = 1;
    while(b){
        if(b & 1) ans = (ans * a) % mod;
        b /= 2;
        a = (a * a) % mod;
    }
    return ans;
}

inline ll inverse(ll a){
    return fast_pow(a, mod-2);
}

void dfs(int src, int par){
    ll ans = 1;
    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;
        dfs(cur, src);
        ans = (ans * (in[cur] + 1)) % mod;
    }
    in[src] = ans;
}

void dfs_out(int src, int par){
    ll tot = 1;
    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;
        tot = (tot * (in[cur] + 1)) % mod;
    }

    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        if(cur == par) continue;

        ll val = (tot * inverse(in[cur] + 1)) % mod;
        val = (val * out[src]) % mod;
        out[cur] = (val + 1) % mod;

        dfs_out(cur, src);
    }
}

void dfs_out(int src){
    int cnt = adj[src].size();
    ll pre[cnt + 2], post[cnt + 2], ind = 0;
    pre[0] = 1, post[cnt + 1] = 1;

    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        ind++;
        pre[ind] = (pre[ind - 1] * (in[cur] + 1)) % mod;
    }
    ind = cnt;
    for(int j = adj[src].size() - 1; j >= 0; j--){
        int cur = adj[src][j];
        post[ind] = (post[ind + 1] * (in[cur] + 1)) % mod;
        ind--;
    }

    for(int j = 0; j < (int)adj[src].size(); j++){
        int cur = adj[src][j];
        out[cur] = (pre[j] * post[j+2]) % mod;
        out[cur] = (out[src] * out[cur]) % mod;
        out[cur] = (out[cur] + 1) % mod;
        dfs_out(cur);
    }
}

int main(){
    int n;
    cin >> n;
    for(int j = 2; j <= n; j++){
        int x;
        cin >> x;
        adj[x].push_back(j);
    }

    dfs(1, 0);
    out[1] = 1;
    dfs_out(1);

    for(int j = 1; j <= n; j++)
        cout << (in[j] * out[j]) % mod << " ";
    cout << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef int64_t lld;
const int N = 200000 + 5;
const int MOD = 1e9 + 7;

lld dp[N], ans[N];
unordered_map<int,lld> no[N];
vector<int> G[N];

void dfs1(int);
void dfs2(int,lld);

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for(int i=2;i<=n;i++){
		int x; cin >> x;
		G[x].push_back(i);
	}
	dfs1(1); dfs2(1, 0);
	for(int i=1;i<=n;i++) cout << ans[i] << " \n"[i==n];
	// for(int i=1;i<=n;i++) {
	// 	cout << i << ": " << dp[i][0] << ", " << dp[i][1] << '\n';
	// }
	return 0;
}

void dfs1(int u) {
	for(int v: G[u]) dfs1(v);
	dp[u] = 1;
	for(int v: G[u]) dp[u] = dp[u]*(dp[v]+1)%MOD;
	vector<int> pre, pos;
	for(int v: G[u]) pre.push_back(dp[v]+1);
	for(int v: G[u]) pos.push_back(dp[v]+1);
	for(int i=1;i<(int)G[u].size();i++) pre[i] = pre[i]*pre[i-1]%MOD;
	for(int i=(int)G[u].size()-2;i>=0;i--) pos[i] = pos[i]*pos[i+1]%MOD;
	for(int i=0;i<(int)G[u].size();i++){
		no[u][G[u][i]] = (i-1>=0?pre[i-1]:1) * (i+1<(int)G[u].size()?pos[i+1]:1);
		no[u][G[u][i]] %= MOD;
	}
}
void dfs2(int u, lld a) {
	ans[u] = dp[u]*(a+1)%MOD;
	for(int v: G[u]) dfs2(v, (a+1)*no[u][v]%MOD);
}


#include <bits/stdc++.h>
using namespace std;
typedef int64_t lld;
const int N = 200000 + 5;
const int MOD = 1e9 + 7;

lld dp[N], ans[N], no[N];
vector<int> G[N];

void dfs1(int);
void dfs2(int,lld);

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for(int i=2;i<=n;i++){
		int x; cin >> x;
		G[x].push_back(i);
	}
	dfs1(1); dfs2(1, 0);
	for(int i=1;i<=n;i++) cout << ans[i] << " \n"[i==n];
	return 0;
}

void dfs1(int u) {
	for(int v: G[u]) dfs1(v);
	dp[u] = 1;
	for(int v: G[u]) dp[u] = dp[u]*(dp[v]+1)%MOD;
	vector<lld> pre, pos;
	for(int v: G[u]) pre.push_back(dp[v]+1);
	for(int v: G[u]) pos.push_back(dp[v]+1);
	for(int i=1;i<(int)G[u].size();i++) pre[i] = pre[i]*pre[i-1]%MOD;
	for(int i=(int)G[u].size()-2;i>=0;i--) pos[i] = pos[i]*pos[i+1]%MOD;
	for(int i=0;i<(int)G[u].size();i++){
		no[G[u][i]] = (i-1>=0?pre[i-1]:1) * (i+1<(int)G[u].size()?pos[i+1]:1);
		no[G[u][i]] %= MOD;
	}
}
void dfs2(int u, lld a) {
	ans[u] = dp[u]*(a+1)%MOD;
	for(int v: G[u]) dfs2(v, (a+1)*no[v]%MOD);
}


#include <bits/stdc++.h>
using namespace std;
typedef int64_t lld;
const int N = 200000 + 5;
const int MOD = 1e9 + 7;

lld dp[N], ans[N];
unordered_map<int,lld> no[N];
vector<int> G[N];

void dfs1(int);
void dfs2(int,lld);

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin >> n;
	for(int i=2;i<=n;i++){
		int x; cin >> x;
		G[x].push_back(i);
	}
	dfs1(1); dfs2(1, 0);
	for(int i=1;i<=n;i++) cout << ans[i] << " \n"[i==n];
	return 0;
}

void dfs1(int u) {
	for(int v: G[u]) dfs1(v);
	dp[u] = 1;
	for(int v: G[u]) dp[u] = dp[u]*(dp[v]+1)%MOD;
	vector<lld> pre, pos;
	for(int v: G[u]) pre.push_back(dp[v]+1);
	for(int v: G[u]) pos.push_back(dp[v]+1);
	for(int i=1;i<(int)G[u].size();i++) pre[i] = pre[i]*pre[i-1]%MOD;
	for(int i=(int)G[u].size()-2;i>=0;i--) pos[i] = pos[i]*pos[i+1]%MOD;
	for(int i=0;i<(int)G[u].size();i++){
		no[u][G[u][i]] = (i-1>=0?pre[i-1]:1) * (i+1<(int)G[u].size()?pos[i+1]:1);
		no[u][G[u][i]] %= MOD;
	}
}
void dfs2(int u, lld a) {
	ans[u] = dp[u]*(a+1)%MOD;
	for(int v: G[u]) dfs2(v, (a+1)*no[u][v]%MOD);
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 500, mod = (int)1e9 + 7;
typedef long long ll;

int n,a;
vector<int> G[N];
ll dp[2][N];
ll res[N];
bool vis[N];

ll fp(ll x, int k){
    if(k == 0) return 1;
    ll hf = fp(x, k/2);
    return k % 2 ? hf*hf%mod*x%mod : hf*hf%mod;
}

void dfs(int v){
    vis[v] = true;
    ll p1 = 1, p2 = 1;
    for(int nxt : G[v]){
        if(!vis[nxt]){
            dfs(nxt);
            p1 *= dp[0][nxt];
            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
            p1 %= mod, p2 %= mod;
        }
    }
    dp[0][v] = p1;
    dp[1][v] = (p2 - p1 + mod) % mod;
}

//void dfs2(ll dp0, ll dp1, int v){
//    vis[v] = true;
//    ll p1 = dp0;
//    ll p2 = (dp0 * 2 + dp1) % mod;
//
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            p1 *= dp[0][nxt];
//            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
//            p1 %= mod, p2 %= mod;
//        }
//    }
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            ll np1 = p1 * fp(dp[0][nxt], mod - 2) % mod;
//            ll np2 = p2 * fp((2 * dp[0][nxt] + dp[1][nxt]) % mod, mod - 2) % mod;
//            dfs2(np1, np2 - np1, nxt);
//        }
//    }
//    res[v] = p2;
//}

void dfs2(ll p1, ll p2, int v){
    vis[v] = true;

    ll dp0 = p1, dp1 = (p2 - p1 + mod) % mod;
    p1 = dp0, p2 = (dp0 * 2 + dp1) % mod;
    for(int nxt : G[v]){
        if(!vis[nxt]){
            p1 *= dp[0][nxt];
            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
            p1 %= mod, p2 %= mod;
        }
    }
    for(int nxt : G[v]){
        if(!vis[nxt]){
            ll np1 = p1 * fp(dp[0][nxt], mod - 2) % mod;
            ll np2 = p2 * fp((2 * dp[0][nxt] + dp[1][nxt]) % mod, mod - 2) % mod;
            dfs2(np1, np2, nxt);
        }
    }
    res[v] = p2;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie();

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> a;
        a--;
        G[a].push_back(i);
        G[i].push_back(a);
    }
    fill(vis, vis+n, false);
    dfs(0);
    fill(vis, vis+n, false);
    dfs2(1, 0, 0);
    for(int i = 0; i < n; i++) printf("%I64d ", res[i]);
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 500, mod = (int)1e9 + 7;
typedef long long ll;

int n,a;
vector<int> G[N];
ll dp[2][N];
ll res[N];
bool vis[N];

ll fp(ll x, int k){
    if(k == 0) return 1;
    ll hf = fp(x, k/2);
    return k % 2 ? hf*hf%mod*x%mod : hf*hf%mod;
}

ll dfs(int v){
    vis[v] = true;
    ll p1 = 1, p2 = 1;
    for(int nxt : G[v]){
        if(!vis[nxt]){
            dfs(nxt);
            p1 *= dp[0][nxt];
            p2 *= (2 * dp[0][nxt] + dp[1][nxt]);
            p1 %= mod, p2 %= mod;
        }
    }
    dp[0][v] = p1;
    dp[1][v] = (p2 - p1 + mod) % mod;
}

ll dfs2(ll dp0, ll dp1, int v){
    vis[v] = true;
    ll p1 = dp0;
    ll p2 = (dp0 * 2 + dp1) % mod;

    for(int nxt : G[v]){
        if(!vis[nxt]){
            p1 *= dp[0][nxt];
            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
            p1 %= mod, p2 %= mod;
        }
    }
    for(int nxt : G[v]){
        if(!vis[nxt]){
            ll np1 = p1 * fp(dp[0][nxt], mod - 2) % mod;
            ll np2 = p2 * fp((2 * dp[0][nxt] + dp[1][nxt]) % mod, mod - 2) % mod;
            dfs2(np1, np2 - np1, nxt);
        }
    }
    res[v] = p2;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie();

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> a;
        a--;
        G[a].push_back(i);
        G[i].push_back(a);
    }
    fill(vis, vis+n, false);
    dfs(0);
    fill(vis, vis+n, false);
    dfs2(1, -1, 0);
    for(int i = 0; i < n; i++) printf("%I64d ", res[i]);
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 500, mod = (int)1e9 + 7;
typedef long long ll;

int n,a;
vector<int> G[N];
ll dp[N];
ll res[N], now[N];
bool vis[N];

ll fp(ll x, int k){
    if(k == 0) return 1;
    ll hf = fp(x, k/2);
    return k % 2 ? hf*hf%mod*x%mod : hf*hf%mod;
}

void dfs(int v){
    vis[v] = true;
    dp[v] = 1;
    for(int nxt : G[v]){
        if(!vis[nxt]){
            dfs(nxt);
            dp[v] *= (dp[nxt] + 1);
            dp[v] %= mod;
        }
    }
}

void dfs2(ll p0, int v){
    vis[v] = true;
    p0 = (p0 + 1) % mod;
    ll np1 = p0;
    for(int nxt : G[v]){

        if(!vis[nxt]){
            now[nxt] = p0;
            for(int nxt2 : G[v]){
                if(!vis[nxt2] && nxt2 != nxt){
                    now[nxt] *= (dp[nxt2] + 1);
                    now[nxt] %= mod;
                }
            }
        }

    }
    for(int nxt : G[v]){
        if(!vis[nxt]){
            dfs2(now[nxt], nxt);
            np1 *= (dp[nxt] + 1);
            np1 %= mod;
        }
    }
    res[v] = np1;
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            ll np0 = p0 * fp(dp[nxt] + 1, mod - 2) % mod;
//            dfs2(np0, nxt);
//        }
//    }
//    res[v] = p0 % mod;
}

//void dfs2(ll dp0, ll dp1, int v){
//    vis[v] = true;
//    ll p1 = dp0;
//    ll p2 = (dp0 * 2 + dp1) % mod;
//
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            p1 *= dp[0][nxt];
//            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
//            p1 %= mod, p2 %= mod;
//        }
//    }
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            ll np1 = p1 * fp(dp[0][nxt], mod - 2) % mod;
//            ll np2 = p2 * fp((2 * dp[0][nxt] + dp[1][nxt]) % mod, mod - 2) % mod;
//            dfs2(np1, np2 - np1, nxt);
//        }
//    }
//    res[v] = p2;
//}

int main(){
    ios::sync_with_stdio(false);
    cin.tie();

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> a;
        a--;
        G[a].push_back(i);
        G[i].push_back(a);
    }
    fill(vis, vis+n, false);
    dfs(0);
    fill(vis, vis+n, false);
    dfs2(0, 0);

    for(int i = 0; i < n; i++) printf("%I64d ", res[i]);
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 500, mod = (int)1e9 + 7;
typedef long long ll;

int n,a;
vector<int> G[N];
ll dp[N];
ll res[N], now[N];
bool vis[N];

ll fp(ll x, int k){
    if(k == 0) return 1;
    ll hf = fp(x, k/2);
    return k % 2 ? hf*hf%mod*x%mod : hf*hf%mod;
}

void dfs(int v){
    vis[v] = true;
    dp[v] = 1;
    for(int nxt : G[v]){
        if(!vis[nxt]){
            dfs(nxt);
            dp[v] *= (dp[nxt] + 1);
            dp[v] %= mod;
        }
    }
}

void dfs2(ll p0, int v){
    vis[v] = true;
    p0 = (p0 + 1) % mod;
    ll np1 = p0;
    for(int nxt : G[v]){

        if(!vis[nxt]){
            now[nxt] = p0;
            for(int nxt2 : G[v]){
                if(!vis[nxt2] && nxt2 != nxt){
                    now[nxt] *= (dp[nxt2] + 1);
                    now[nxt] %= mod;
                }
            }
        }

    }
    for(int nxt : G[v]){
        if(!vis[nxt]){
            dfs2(now[nxt], nxt);
            np1 *= (dp[nxt] + 1);
            np1 %= mod;
        }
    }
    res[v] = np1;
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            ll np0 = p0 * fp(dp[nxt] + 1, mod - 2) % mod;
//            dfs2(np0, nxt);
//        }
//    }
//    res[v] = p0 % mod;
}

//void dfs2(ll dp0, ll dp1, int v){
//    vis[v] = true;
//    ll p1 = dp0;
//    ll p2 = (dp0 * 2 + dp1) % mod;
//
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            p1 *= dp[0][nxt];
//            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
//            p1 %= mod, p2 %= mod;
//        }
//    }
//    for(int nxt : G[v]){
//        if(!vis[nxt]){
//            ll np1 = p1 * fp(dp[0][nxt], mod - 2) % mod;
//            ll np2 = p2 * fp((2 * dp[0][nxt] + dp[1][nxt]) % mod, mod - 2) % mod;
//            dfs2(np1, np2 - np1, nxt);
//        }
//    }
//    res[v] = p2;
//}

int main(){
    ios::sync_with_stdio(false);
    cin.tie();

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> a;
        a--;
        G[a].push_back(i);
        G[i].push_back(a);
    }
    fill(vis, vis+n, false);
    dfs(0);
    fill(vis, vis+n, false);
    dfs2(0, 0);
    cout << dp[0] << endl;
    for(int i = 0; i < n; i++) printf("%I64d ", res[i]);
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 500, mod = (int)1e9 + 7;
typedef long long ll;

int n,a;
vector<int> G[N];
ll dp[N];
ll res[N];
ll pro[N];
bool vis[N];

void dfs(int v) {
    vis[v] = true;
    dp[v] = 1;
    vector<int> V;
    for (int nxt : G[v]) {
        if (!vis[nxt]) {
            dfs(nxt);
            V.push_back(nxt);
            pro[nxt] = dp[v];
            dp[v] *= (dp[nxt] + 1);
            dp[v] %= mod;
        }
    }
    reverse(V.begin(), V.end());
    ll p = 1;
    for (int nxt : V) {
        pro[nxt] *= p;
        p *= (dp[nxt] + 1);
        pro[nxt] % mod, p %= mod;
    }
}
void dfs2(ll p0, int v) {
    vis[v] = true;
    p0 = (p0 + 1) % mod;
    for (int nxt : G[v]) {
        if (!vis[nxt]) {
            dfs2(pro[nxt] * p0 % mod, nxt);
        }
    }
    res[v] = p0 * dp[v] % mod;

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie();

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> a;
        a--;
        G[a].push_back(i);
        G[i].push_back(a);
    }
    fill(vis, vis+n, false);
    dfs(0);
    fill(vis, vis+n, false);
    dfs2(0, 0);

    for(int i = 0; i < n; i++) printf("%I64d ", res[i]);
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 500, mod = (int)1e9 + 7;
typedef long long ll;

int n,a;
vector<int> G[N];
ll dp[2][N];
ll res[N];
bool vis[N];

ll fp(ll x, int k){
    if(k == 0) return 1;
    ll hf = fp(x, k/2);
    return k % 2 ? hf*hf%mod*x%mod : hf*hf%mod;
}

void dfs(int v){
    vis[v] = true;
    ll p1 = 1, p2 = 1;
    for(int nxt : G[v]){
        if(!vis[nxt]){
            dfs(nxt);
            p1 *= dp[0][nxt];
            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
            p1 %= mod, p2 %= mod;
        }
    }
    dp[0][v] = p1;
    dp[1][v] = (p2 - p1 + mod) % mod;
}

void dfs2(ll dp0, ll dp1, int v){
    vis[v] = true;
    ll p1 = dp0;
    ll p2 = (dp0 * 2 + dp1) % mod;

    for(int nxt : G[v]){
        if(!vis[nxt]){
            p1 *= dp[0][nxt];
            p2 *= (2 * dp[0][nxt] + dp[1][nxt]) % mod;
            p1 %= mod, p2 %= mod;
        }
    }
    for(int nxt : G[v]){
        if(!vis[nxt]){
            ll np1 = p1 * fp(dp[0][nxt], mod - 2) % mod;
            ll np2 = p2 * fp((2 * dp[0][nxt] + dp[1][nxt]) % mod, mod - 2) % mod;
            dfs2(np1, np2 - np1, nxt);
        }
    }
    res[v] = p2;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie();

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> a;
        a--;
        G[a].push_back(i);
        G[i].push_back(a);
    }
    fill(vis, vis+n, false);
    dfs(0);
    fill(vis, vis+n, false);
    dfs2(1, -1, 0);
    for(int i = 0; i < n; i++) printf("%I64d ", res[i]);
}


#include <bits/stdc++.h>
using namespace std;

const int N = (int)2e5 + 500, mod = (int)1e9 + 7;
typedef long long ll;

int n,a;
vector<int> G[N];
ll dp[N];
ll res[N], pro[N];
bool vis[N];

void dfs(int v) {
    vis[v] = true;
    dp[v] = 1;
    vector<int> V;
    for (int nxt : G[v]) {
        if (!vis[nxt]) {
            dfs(nxt);
            V.push_back(nxt);
            pro[nxt] = dp[v];
            dp[v] *= (dp[nxt] + 1);
            dp[v] %= mod;
        }
    }
    reverse(V.begin(), V.end());
    ll p = 1;
    for (int nxt : V) {
        pro[nxt] *= p;
        p *= (dp[nxt] + 1);
        pro[nxt] %= mod, p %= mod;
    }
}
void dfs2(ll p0, int v) {
    vis[v] = true;
    p0 = (p0 + 1) % mod;
    for (int nxt : G[v]) {
        if (!vis[nxt]) {
            dfs2(pro[nxt] * p0 % mod, nxt);
        }
    }
    res[v] = p0 * dp[v] % mod;

}

int main(){
    ios::sync_with_stdio(false);
    cin.tie();

    cin >> n;
    for(int i = 1; i < n; i++){
        cin >> a;
        a--;
        G[a].push_back(i);
        G[i].push_back(a);
    }
    fill(vis, vis+n, false);
    dfs(0);
    fill(vis, vis+n, false);
    dfs2(0, 0);

    for(int i = 0; i < n; i++) printf("%I64d ", res[i]);
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n;
long long f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
	for(int y:ad[x])
	if(y!=par)
	{
		down(y,x);
		d[x]=d[x]*(d[y]+1)%Mod;
	}
}
void up(int x, int par=-1)
{
	for(int i=0; i<ad[x].size(); i++)f[i]=1;
	for(int i=0; i<ad[x].size(); i++)
	{
		f[i]=1;
		if(ad[x][i]==par)continue;
		for(int j=0; j<ad[x].size(); j++)
		if(i!=j && ad[x][j]!=par)
		{
			f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
		}
	}
	for(int i=0; i<ad[x].size(); i++)
	{
		int y=ad[x][i];
		if(y==par)continue;
		u[y]=u[y]*(u[x]*f[i]+1)%Mod;
		if(ad[y].size()>1)up(y,x);
	}
}
int main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	//else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2; i<=n; i++)
	{
		int x; cin>>x;
		ad[x].push_back(i);
		ad[i].push_back(x);
	}
	for(int i=1; i<=n; i++)u[i]=d[i]=1;
	down(1);
	up(1);
	for(int i=1; i<=n; i++)
	{
		cout<<d[i]*u[i]%Mod<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n;
int f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
  for(int y:ad[x])
  if(y!=par)
  {
    down(y,x);
    d[x]=d[x]*(d[y]+1)%Mod;
  }
}
void up(int x, int par=-1)
{
  f[ad[x].size()]=1;
  int cur=1;
  for(int i=ad[x].size()-1; i>=0; i--)
  {
    int y=ad[x][i];
    if(y==par)f[i]=f[i+1];
    else f[i]=f[i+1]*(d[y]+1)%Mod;
  }
  for(int i=0; i<ad[x].size(); i++)
  {
    int y=ad[x][i];
    if(y==par)continue;
    u[y]=(u[x]*cur%Mod*f[i+1]+1)%Mod;
    cur=cur*(d[y]+1)%Mod;
  }
  for(int y:ad[x])
  {
    if(y==par)continue;
    up(y,x);
  }
}
 main()
{
  if(fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  //else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin>>n;
  for(int i=2; i<=n; i++)
  {
    int x; cin>>x;
    ad[x].push_back(i);
    ad[i].push_back(x);
  }
  for(int i=1; i<=n; i++)u[i]=d[i]=1;
  down(1);
  up(1);
  for(int i=1; i<=n; i++)
  {
    cout<<d[i]*u[i]%Mod<<" ";
  }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=2e5+5;
int n;
int f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
  for(int y:ad[x])
  if(y!=par)
  {
    down(y,x);
    d[x]=d[x]*(d[y]+1)%Mod;
  }
}
void up(int x, int par=-1)
{
  f[ad[x].size()]=1;
  int cur=1;
  for(int i=ad[x].size()-1; i>=0; i--)
  {
    int y=ad[x][i];
    if(y==par)f[i]=f[i+1];
    else f[i]=f[i+1]*(d[y]+1)%Mod;
  }
  for(int i=0; i<ad[x].size(); i++)
  {
    int y=ad[x][i];
    if(y==par)continue;
    u[y]=(u[x]*cur%Mod*f[i+1]+1)%Mod;
    cur=cur*(d[y]+1)%Mod;
  }
  for(int y:ad[x])
  {
    if(y==par)continue;
    up(y,x);
  }
}
 main()
{
  if(fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  //else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin>>n;
  for(int i=2; i<=n; i++)
  {
    int x; cin>>x;
    ad[x].push_back(i);
    ad[i].push_back(x);
  }
  for(int i=1; i<=n; i++)u[i]=d[i]=1;
  down(1);
  up(1);
  for(int i=1; i<=n; i++)
  {
    cout<<d[i]*u[i]%Mod<<" ";
  }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n;
int f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
	for(int y:ad[x])
	if(y!=par)
	{
		down(y,x);
		d[x]=d[x]*(d[y]+1)%Mod;
	}
}
void up(int x, int par=-1)
{
	for(int i=0; i<ad[x].size(); i++)f[i]=1;
	for(int i=0; i<ad[x].size(); i++)
	{
		f[i]=1;
		if(ad[x][i]==par)continue;
		for(int j=0; j<ad[x].size(); j++)
		if(i!=j && ad[x][j]!=par)
		{
			f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
		}
	}
	for(int i=0; i<ad[x].size(); i++)
	{
		int y=ad[x][i];
		if(y==par)continue;
		u[y]=(u[y]*u[x]%Mod*f[i]+1)%Mod;
		if(ad[y].size()>1)up(y,x);
	}
}
 main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	//else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2; i<=n; i++)
	{
		int x; cin>>x;
		ad[x].push_back(i);
		ad[i].push_back(x);
	}
	for(int i=1; i<=n; i++)u[i]=d[i]=1;
	down(1);
	up(1);
	for(int i=1; i<=n; i++)
	{
		cout<<d[i]*u[i]%Mod<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n;
int f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
  for(int y:ad[x])
  if(y!=par)
  {
    down(y,x);
    d[x]=d[x]*(d[y]+1)%Mod;
  }
}
void up(int x, int par=-1)
{
  f[ad[x].size()]=1;
  int cur=1;
  for(int i=ad[x].size()-1; i>=0; i--)
  {
    int y=ad[x][i];
    if(y==par)f[i]=f[i+1];
    else f[i]=f[i+1]*(d[y]+1)%Mod;
  }
  for(int i=0; i<ad[x].size(); i++)
  {
    int y=ad[x][i];
    if(y==par)continue;
    u[y]=(u[x]*cur%Mod*f[i+1]+1)%Mod;
    cur=cur*(d[y]+1)%Mod;
    up(y,x);
  }
}
 main()
{
  if(fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  //else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin>>n;
  for(int i=2; i<=n; i++)
  {
    int x; cin>>x;
    ad[x].push_back(i);
    ad[i].push_back(x);
  }
  for(int i=1; i<=n; i++)u[i]=d[i]=1;
  down(1);
  up(1);
  for(int i=1; i<=n; i++)
  {
    cout<<d[i]*u[i]%Mod<<" ";
  }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n, f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
	for(int y:ad[x])
	if(y!=par)
	{
		down(y,x);
		d[x]=d[x]*(d[y]+1)%Mod;
	}
}
void up(int x, int par=-1)
{
	for(int i=0; i<ad[x].size(); i++)f[i]=1;
	for(int i=0; i<ad[x].size(); i++)
	{
		f[i]=1;
		if(ad[x][i]==par)continue;
		for(int j=0; j<ad[x].size(); j++)
		if(i!=j && ad[x][j]!=par)
		{
			f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
		}
	}
	for(int i=0; i<ad[x].size(); i++)
	{
		int y=ad[x][i];
		if(y==par)continue;
		u[y]=u[y]*(u[x]*f[i]+1)%Mod;
		if(ad[y].size()>1)up(y,x);
	}
}
 main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2; i<=n; i++)
	{
		int x; cin>>x;
		ad[x].push_back(i);
		ad[i].push_back(x);
	}
	for(int i=1; i<=n; i++)u[i]=d[i]=1;
	down(1);
	up(1);
	for(int i=1; i<=n; i++)
	{
		cout<<d[i]*u[i]%Mod<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n;
long long f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
	for(int y:ad[x])
	if(y!=par)
	{
		down(y,x);
		d[x]=d[x]*(d[y]+1)%Mod;
	}
}
void up(int x, int par=-1)
{
	for(int i=0; i<ad[x].size(); i++)f[i]=1;
	for(int i=0; i<ad[x].size(); i++)
	{
		f[i]=1;
		if(ad[x][i]==par)continue;
		for(int j=0; j<ad[x].size(); j++)
		if(i!=j && ad[x][j]!=par)
		{
			f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
		}
	}
	for(int i=0; i<ad[x].size(); i++)
	{
		int y=ad[x][i];
		if(y==par)continue;
		u[y]=u[y]*(u[x]*f[i]+1)%Mod;

	}
        for(int y:ad[x])
        {
           if(y==par)continue;
           up(y,x);
         }
}
int main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	//else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2; i<=n; i++)
	{
		int x; cin>>x;
		ad[x].push_back(i);
		ad[i].push_back(x);
	}
	for(int i=1; i<=n; i++)u[i]=d[i]=1;
	down(1);
	up(1);
	for(int i=1; i<=n; i++)
	{
		cout<<d[i]*u[i]%Mod<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n;
int f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
  for(int y:ad[x])
  if(y!=par)
  {
    down(y,x);
    d[x]=d[x]*(d[y]+1)%Mod;
  }
}
void up(int x, int par=-1)
{
  for(int i=0; i<ad[x].size(); i++)f[i]=1;
  for(int i=0; i<ad[x].size(); i++)
  {
    f[i]=1;
    if(ad[x][i]==par)continue;
    for(int j=0; j<ad[x].size(); j++)
    if(i!=j && ad[x][j]!=par)
    {
      f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
    }
  }
  for(int i=0; i<ad[x].size(); i++)
  {
    int y=ad[x][i];
    if(y==par)continue;
    u[y]=(u[y]*u[x]%Mod*f[i]+1)%Mod;
    up(y,x);
  }
}
 main()
{
  if(fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  //else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin>>n;
  for(int i=2; i<=n; i++)
  {
    int x; cin>>x;
    ad[x].push_back(i);
    ad[i].push_back(x);
  }
  for(int i=1; i<=n; i++)u[i]=d[i]=1;
  down(1);
  up(1);
  for(int i=1; i<=n; i++)
  {
    cout<<d[i]*u[i]%Mod<<" ";
  }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n;
int f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
	for(int y:ad[x])
	if(y!=par)
	{
		down(y,x);
		d[x]=d[x]*(d[y]+1)%Mod;
	}
}
void up(int x, int par=-1)
{
	for(int i=0; i<ad[x].size(); i++)f[i]=1;
	for(int i=0; i<ad[x].size(); i++)
	{
		f[i]=1;
		if(ad[x][i]==par)continue;
		for(int j=0; j<ad[x].size(); j++)
		if(i!=j && ad[x][j]!=par)
		{
			f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
		}
	}
	for(int i=0; i<ad[x].size(); i++)
	{
		int y=ad[x][i];
		if(y==par)continue;
		u[y]=u[y]*(u[x]*f[i]+1)%Mod;
		if(ad[y].size()>1)up(y,x);
	}
}
 main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	//else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2; i<=n; i++)
	{
		int x; cin>>x;
		ad[x].push_back(i);
		ad[i].push_back(x);
	}
	for(int i=1; i<=n; i++)u[i]=d[i]=1;
	down(1);
	up(1);
	for(int i=1; i<=n; i++)
	{
		cout<<d[i]*u[i]%Mod<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=2e5+5;
int n;
long long f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
	for(int y:ad[x])
	if(y!=par)
	{
		down(y,x);
		d[x]=d[x]*(d[y]+1)%Mod;
	}
}
void up(int x, int par=-1)
{
	for(int i=0; i<ad[x].size(); i++)f[i]=1;
	for(int i=0; i<ad[x].size(); i++)
	{
		f[i]=1;
		if(ad[x][i]==par)continue;
		for(int j=0; j<ad[x].size(); j++)
		if(i!=j && ad[x][j]!=par)
		{
			f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
		}
	}
	for(int i=0; i<ad[x].size(); i++)
	{
		int y=ad[x][i];
		if(y==par)continue;
		u[y]=u[y]*(u[x]*f[i]+1)%Mod;
		if(ad[y].size()>1)up(y,x);
	}
}
int main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2; i<=n; i++)
	{
		int x; cin>>x;
		ad[x].push_back(i);
		ad[i].push_back(x);
	}
	for(int i=1; i<=n; i++)u[i]=d[i]=1;
	down(1);
	up(1);
	for(int i=1; i<=n; i++)
	{
		cout<<d[i]*u[i]%Mod<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
//#define ii pair<int,int>
//#define fi first
//#define se second
//#define pb push_back
const int oo=1e9;
const int Mod=1e9+7;
const long long OO=1e18;
const int N=1e5+5;
int n, f[N], d[N], u[N];
vector<int>ad[N];
void down(int x, int par=-1)
{
	for(int y:ad[x])
	if(y!=par)
	{
		down(y,x);
		d[x]=d[x]*(d[y]+1)%Mod;
	}
}
void up(int x, int par=-1)
{
	for(int i=0; i<ad[x].size(); i++)f[i]=1;
	for(int i=0; i<ad[x].size(); i++)
	{
		f[i]=1;
		if(ad[x][i]==par)continue;
		for(int j=0; j<ad[x].size(); j++)
		if(i!=j && ad[x][j]!=par)
		{
			f[i]=f[i]*(d[ad[x][j]]+1)%Mod;
		}
	}
	for(int i=0; i<ad[x].size(); i++)
	{
		int y=ad[x][i];
		if(y==par)continue;
		u[y]=u[y]*(u[x]*f[i]+1)%Mod;
		if(ad[y].size()>1)up(y,x);
	}
}
int main()
{
	if(fopen("trash.inp", "r"))
		freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	//else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for(int i=2; i<=n; i++)
	{
		int x; cin>>x;
		ad[x].push_back(i);
		ad[i].push_back(x);
	}
	for(int i=1; i<=n; i++)u[i]=d[i]=1;
	down(1);
	up(1);
	for(int i=1; i<=n; i++)
	{
		cout<<d[i]*u[i]%Mod<<" ";
	}
}


import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) {
		InputReader in = new InputReader (System.in);
		PrintWriter out = new PrintWriter (System.out);
		TaskD solver = new TaskD();
		solver.solve( 1, in, out);
		out.close();
	}
}

class TaskD {
	static final int mod = (int)1e9 + 7;

	public void solve(int TestNumber, InputReader in, PrintWriter out) {
		int n = in.nextInt();
		int[] p = new int[n];
		for (int i = 1; i < n; ++i) {
			p[i] = in.nextInt()-1;
		}
		int[] ways = new int[n];
		int[] eqMod = new int[n];
		Arrays.fill(ways, 1);
		for (int i = n - 1; i > 0; --i) {
			int j = p[i];
			if (eqMod[i] == 0 && ways[i] + 1 == mod) {
				++eqMod[j];
			}
			else {
				ways[j] = (int)(ways[j] * (long)((eqMod[i] == 0 ? ways[i] : 0)+1) % mod);
			}
		}
		if (eqMod[0] == 0) out.print(ways[0]);
		else out.print(0);

		for (int i = 1; i < n; ++i) {
			int j = p[i];
			int way = ways[j];
			int eq = eqMod[j];
			if (eqMod[i] == 0 && ways[i] + 1 == mod) {
				--eq;
			}
			else {
				way = (int) (way * (long)inv((eqMod[i] == 0 ? ways[i] : 0)+1) % mod);
			}
			if (eq > 0) way = 0;
			if (eq == 0 && way + 1 == mod) {
				++eqMod[i];
			}
			else {
				ways[i] = (int) (ways[i] * (long)((eq == 0 ? way : 0) +1) % mod);
			}
			out.print(' ');
			if (eqMod[i] == 0) out.print(ways[i]);
			else out.print(0);
		}
		out.println();
	}

	private int inv(int x) {
		return qsm(x,mod-2);
	}

	private int qsm(int a, int b) {
		int ret = 1;
		while (b > 0) {
			if ((b&1)>0) ret = (int)(ret * (long)a % mod);
			a = (int)(a * (long)a % mod);
			b >>= 1;
		}
		return ret;
	}
}

class InputReader {
	BufferedReader reader;
	StringTokenizer tokenizer;

	public InputReader (InputStream stream) {
		reader = new BufferedReader (new InputStreamReader (stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer (reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException (e);
			}
		}
		return tokenizer.nextToken();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final long MOD = (long) (1e9 + 7);
    private static final BigInteger BI_MOD = BigInteger.valueOf(MOD);

    private void solve() throws IOException {
        int n = nextInt();
        init(n, n - 1);
        for (int i = 0; i < n - 1; i++) {
            addEdge(nextInt() - 1, i + 1);
        }
        dd = new long[m];
        mm = new long[n];
        dfs1(0, -1);
        dfs2(0, -1, -1);
        for (int i = 0; i < n; i++) {
            System.out.println(mm[i]);
        }
    }

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    private int dfs1(int x, int p) {
        int j = head[x];
        mm[x] = 1;
        while (j >= 0) {
            int y = dst[j];
            if (y != p) {
                dfs1(y, x);
                dd[j] = (1 + mm[y]) % MOD;
                mm[x] = (mm[x] * dd[j]) % MOD;
            }
            j = nx[j];
        }
        return 0;
    }

    private int dfs2(int x, int p, int e) {
        if (p >= 0) {
            int j = head[x];
            while (j >= 0) {
                int y = dst[j];
                if (y == p) {
                    dd[j] = 1 + mm[p] * inv(dd[e]);
                    dd[j] %= MOD;
                    mm[x] = (mm[x] * dd[j]) % MOD;
                }
                j = nx[j];
            }
        }
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            if (y != p) {
                dfs2(y, x, j);
            }
            j = nx[j];
        }
        return 0;
    }

    private long inv(long l) {
        return BigInteger.valueOf(l).modInverse(BI_MOD).longValue();
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    int last;
    long[] dd;
    long[] mm;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    PrintWriter out = new PrintWriter(System.out);

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @author Pavel Mavrin
 */
public class D {

    private static final long MOD = (long) (1e9 + 7);
    private static final BigInteger BI_MOD = BigInteger.valueOf(MOD);

    private void solve() throws IOException {
        int n = nextInt();
        init(n, n - 1);
        for (int i = 0; i < n - 1; i++) {
            addEdge(nextInt() - 1, i + 1);
        }
        dd = new long[m];
        mm = new long[n];
        mp = new long[m];
        ms = new long[m];
        pp = new long[n];
        cc = new int[n];
        dfs1(0, -1);
        dfs2(0, -1, -1);
        for (int i = 0; i < n; i++) {
            System.out.println(mm[i] * pp[i] % MOD);
        }
    }

    void init(int n, int m) {
        m *= 2;
        this.n = n;
        this.m = m;
        last = 0;
        head = new int[n];
        nx = new int[m];
        dst = new int[m];
        Arrays.fill(head, -1);
    }

    void addEdge(int x, int y) {
        nx[last] = head[x];
        dst[last] = y;
        head[x] = last;
        last++;
        nx[last] = head[y];
        dst[last] = x;
        head[y] = last;
        last++;
    }

    int[] cc;

    private int dfs1(int x, int p) {
        int j = head[x];
        mm[x] = 1;
        while (j >= 0) {
            int y = dst[j];
            if (y != p) {
                dfs1(y, x);
                dd[j] = (1 + mm[y]) % MOD;
                mm[x] = (mm[x] * dd[j]) % MOD;
            }
            j = nx[j];
        }
        j = head[x];
        int n = 0;
        while (j >= 0) {
            int y = dst[j];
            if (y != p) {
                cc[n++] = j;
            }
            j = nx[j];
        }
        if (n > 0) {
            mp[cc[0]] = 1;
            for (int i = 0; i < n - 1; i++) {
                mp[cc[i + 1]] = mp[cc[i]] * dd[cc[i]] % MOD;
            }
            ms[cc[n - 1]] = 1;
            for (int i = n - 1; i >= 1; i--) {
                ms[cc[i - 1]] = ms[cc[i]] * dd[cc[i]] % MOD;
            }
//            for (int i = 0; i < n; i++) {
//                System.out.println(mp[cc[i]] + " " + ms[cc[i]]);
//            }
        }
        return 0;
    }

    private int dfs2(int x, int p, int e) {
        pp[x] = 1;
        if (p >= 0) {
            int j = head[x];
            while (j >= 0) {
                int y = dst[j];
                if (y == p) {
                    dd[j] = mp[e] * ms[e];
                    dd[j] %= MOD;
                    dd[j] *= pp[p];
                    dd[j] %= MOD;
                    dd[j]++;
                    dd[j] %= MOD;
                    pp[x] = dd[j];
                }
                j = nx[j];
            }
        }
        int j = head[x];
        while (j >= 0) {
            int y = dst[j];
            if (y != p) {
                dfs2(y, x, j);
            }
            j = nx[j];
        }
        return 0;
    }

    private long inv(long l) {
        return BigInteger.valueOf(l).modInverse(BI_MOD).longValue();
    }

    int n, m;
    int[] head;
    int[] nx;
    int[] dst;
    int last;
    long[] dd;
    long[] pp;
    long[] mm;
    long[] mp;
    long[] ms;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st;
    PrintWriter out = new PrintWriter(System.out);

    String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public static void main(String[] args) throws IOException {
        new D().run();
    }

    private void run() throws IOException {
        solve();
        out.close();
    }

}


import java.util.*;
import java.io.*;
public class D
{
	public static final long MOD = 1000000007;

	public static ArrayList<Integer>[] adj;
	public static long[] count;
	public static long[] modPow;
	public static long[] result;

	public static void main(String[] args) throws Exception
	{
		FastScanner in = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = in.nextInt();

		adj = new ArrayList[n];
		adj[0] = new ArrayList<Integer>();
		for(int x = 1; x < n; x++)
		{
			adj[x] = new ArrayList<Integer>();
			adj[in.nextInt() - 1].add(x);
		}

		count = new long[adj.length];
		modPow = new long[adj.length];
		getCount(0);

//		System.out.println(Arrays.toString(count));

		result = new long[adj.length];
		getResult(0, 1);

		for(int y = 0; y < result.length; y++)
		{
			if(y > 0)
			{
				out.print(" ");
			}

			out.print(result[y]);
		}

		out.println();
		out.close();
	}

	public static long getCount(int node)
	{
		count[node] = 1;
		for(int next : adj[node])
		{
			long val = getCount(next);

			if(modPow[next] == 0)
			{
				if(val == 0)
				{
					modPow[node]++;
				}
				else
				{
					count[node] *= val;
					count[node] %= MOD;
				}
			}
		}

		return (count[node] + 1) % MOD;
	}

	public static void getResult(int node, long prevCount)
	{
		result[node] = (count[node] * prevCount) % MOD;
		if(modPow[node] > 0)
		{
			result[node] = 0;
		}

		for(int next : adj[node])
		{
			long nextCount = count[node];
			if(modPow[node] == 1 && (count[next] + 1) % MOD == 0)
			{
				nextCount = count[node];
			}
			else if(modPow[node] >= 1)
			{
				nextCount = 0;
			}
			else if(modPow[next] == 0)
			{
				long inv = (((extendedEuclid((count[next] + 1) % MOD, MOD)[0] % MOD) + MOD) % MOD);
				nextCount *= inv;
				nextCount %= MOD;
			}

			nextCount *= prevCount;
			nextCount %= MOD;

			getResult(next, (nextCount + 1) % MOD);
		}
	}

	public static long[] extendedEuclid(long a, long b)
	{
		if(b == 0)
		{
			return new long[] {1, 0};
		}
		else
		{
			long q = a / b;
			long r = a % b;
			long[] prev = extendedEuclid(b, r);

			return new long[] {prev[1], prev[0] - (q * prev[1])};
		}
	}

	static class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(InputStream input)
		{
			br = new BufferedReader(new InputStreamReader(input));
			st = new StringTokenizer("");
		}

		public String next() throws IOException
		{
			if(st.hasMoreTokens())
			{
				return st.nextToken();
			}
			else
			{
				st = new StringTokenizer(br.readLine());
				return next();
			}
		}

		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
	}
}


import java.util.*;
import java.io.*;
public class D
{
	public static final long MOD = 1000000007;

	public static ArrayList<Integer>[] adj;
	public static long[] count;
	public static long[] result;

	public static void main(String[] args) throws Exception
	{
		FastScanner in = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = in.nextInt();

		adj = new ArrayList[n];
		adj[0] = new ArrayList<Integer>();
		for(int x = 1; x < n; x++)
		{
			adj[x] = new ArrayList<Integer>();
			adj[in.nextInt() - 1].add(x);
		}

		count = new long[adj.length];
		getCount(0);

//		System.out.println(Arrays.toString(count));

		result = new long[adj.length];
		getResult(0, 1);

		for(int y = 0; y < result.length; y++)
		{
			if(y > 0)
			{
				out.print(" ");
			}

			out.print(result[y]);
		}

		out.println();
		out.close();
	}

	public static long getCount(int node)
	{
		count[node] = 1;
		for(int next : adj[node])
		{
			count[node] *= getCount(next);
			count[node] %= MOD;
		}

		return (count[node] + 1) % MOD;
	}

	public static void getResult(int node, long prevCount)
	{
		result[node] = (count[node] * prevCount) % MOD;

		for(int next : adj[node])
		{
			long nextCount = (count[node] * (((extendedEuclid((count[next] + 1) % MOD, MOD)[0] % MOD) + MOD) % MOD)) % MOD;
			nextCount *= prevCount;
			nextCount %= MOD;

			getResult(next, (nextCount + 1) % MOD);
		}
	}

	public static long[] extendedEuclid(long a, long b)
	{
		if(b == 0)
		{
			return new long[] {1, 0};
		}
		else
		{
			long q = a / b;
			long r = a % b;
			long[] prev = extendedEuclid(b, r);

			return new long[] {prev[1], prev[0] - (q * prev[1])};
		}
	}

	static class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(InputStream input)
		{
			br = new BufferedReader(new InputStreamReader(input));
			st = new StringTokenizer("");
		}

		public String next() throws IOException
		{
			if(st.hasMoreTokens())
			{
				return st.nextToken();
			}
			else
			{
				st = new StringTokenizer(br.readLine());
				return next();
			}
		}

		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
	}
}


import java.util.*;
import java.io.*;
public class D
{
	public static final long MOD = 1000000007;

	public static ArrayList<Integer>[] adj;
	public static long[] count;
	public static long[] modPow;
	public static long[] result;

	public static void main(String[] args) throws Exception
	{
		FastScanner in = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);

		int n = in.nextInt();

		adj = new ArrayList[n];
		adj[0] = new ArrayList<Integer>();
		for(int x = 1; x < n; x++)
		{
			adj[x] = new ArrayList<Integer>();
			adj[in.nextInt() - 1].add(x);
		}

		count = new long[adj.length];
		modPow = new long[adj.length];
		getCount(0);

//		System.out.println(Arrays.toString(count));

		result = new long[adj.length];
		getResult(0, 1);

		for(int y = 0; y < result.length; y++)
		{
			if(y > 0)
			{
				out.print(" ");
			}

			out.print(result[y]);
		}

		out.println();
		out.close();
	}

	public static long getCount(int node)
	{
		count[node] = 1;
		for(int next : adj[node])
		{
			long val = getCount(next);

			if(val == 0)
			{
				modPow[node]++;
			}
			else
			{
				if(modPow[next] == 0)
				{
					count[node] *= val;
					count[node] %= MOD;
				}
			}
		}

		return (count[node] + 1) % MOD;
	}

	public static void getResult(int node, long prevCount)
	{
		result[node] = (count[node] * prevCount) % MOD;
		if(modPow[node] > 0)
		{
			result[node] = 0;
		}

		for(int next : adj[node])
		{
			long nextCount = count[node];
			if(modPow[node] == 1 && (count[next] + 1) % MOD == 0)
			{
				nextCount = count[node];
			}
			else if(modPow[node] >= 1)
			{
				nextCount = 0;
			}
			else if(modPow[next] == 0)
			{
				long inv = (((extendedEuclid((count[next] + 1) % MOD, MOD)[0] % MOD) + MOD) % MOD);
				nextCount *= inv;
				nextCount %= MOD;
			}

			nextCount *= prevCount;
			nextCount %= MOD;

			getResult(next, (nextCount + 1) % MOD);
		}
	}

	public static long[] extendedEuclid(long a, long b)
	{
		if(b == 0)
		{
			return new long[] {1, 0};
		}
		else
		{
			long q = a / b;
			long r = a % b;
			long[] prev = extendedEuclid(b, r);

			return new long[] {prev[1], prev[0] - (q * prev[1])};
		}
	}

	static class FastScanner
	{
		BufferedReader br;
		StringTokenizer st;

		public FastScanner(InputStream input)
		{
			br = new BufferedReader(new InputStreamReader(input));
			st = new StringTokenizer("");
		}

		public String next() throws IOException
		{
			if(st.hasMoreTokens())
			{
				return st.nextToken();
			}
			else
			{
				st = new StringTokenizer(br.readLine());
				return next();
			}
		}

		public int nextInt() throws IOException
		{
			return Integer.parseInt(next());
		}
	}
}


import java.util.*;
import java.math.*;
import java.io.*;

public class D
{
   public static void main(String[] args)
   {
      new D(new FastScanner());
   }

   int N, MODO = (int)1e9+7;
   ArrayList<Integer>[] adj;
   int[] res, ways;
   void dfs(int i, int par)
   {
      ways[i] = 1;
      for (int j : adj[i]) if (j != par)
      {
         dfs(j, i);
         long rr = ((ways[j]+1L)*ways[i])%MODO;
         ways[i] = (int)rr;
      }
   }

   void go(int i, int par, int waysAbove)
   {
      long prod = ((1L+waysAbove)*ways[i])%MODO;
      res[i] = (int)prod;
      for (int j : adj[i]) if (j != par)
      {
         long wa = (prod*modInv(ways[j]+1, MODO))%MODO;
         go(j, i, (int)wa);
      }
   }

   long modInv(long a, long m)
   {
      return BigInteger.valueOf(a).modInverse(BigInteger.valueOf(m)).longValue();
   }

   public D(FastScanner in)
   {
      N = in.nextInt();
      adj = new ArrayList[N];
      for (int i=0; i<N; i++)
         adj[i] = new ArrayList<Integer>();
      for (int i=1; i<N; i++)
      {
         int j = in.nextInt()-1;
         adj[i].add(j);
         adj[j].add(i);
      }
      res = new int[N];
      ways = new int[N];
      dfs(0, 0);
      go(0, 0, 0);
      StringBuilder sb = new StringBuilder();
      for (int r : res)
      {
         sb.append(r);
         sb.append(' ');
      }
      System.out.println(sb.toString().trim());
   }
}

class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner()
    {
        stream = System.in;
    }

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    boolean isSpaceChar(int c)
    {
        return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
    }

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}


import java.util.*;
import java.math.*;
import java.io.*;

public class D
{
   public static void main(String[] args)
   {
      new D(new FastScanner());
   }

   int N, MODO = (int)1e9+7;
   ArrayList<Integer>[] adj;
   int[] res, ways;
   void dfs(int i, int par)
   {
      ways[i] = 1;
      for (int j : adj[i]) if (j != par)
      {
         dfs(j, i);
         long rr = ((ways[j]+1L)*ways[i])%MODO;
         ways[i] = (int)rr;
      }
   }

   void go(int i, int par, int waysAbove)
   {
      long prod = ((1L+waysAbove)*ways[i])%MODO;
      res[i] = (int)prod;
      long preProd = 1L+waysAbove, suffProd = 1L;
      ArrayDeque<Integer> stk = new ArrayDeque<Integer>();
      for (int j : adj[i]) if (j != par)
      {
         stk.push((int)suffProd);
         suffProd *= (ways[j]+1);
         suffProd %= MODO;
      }
      Collections.reverse(adj[i]);
      for (int j : adj[i]) if (j != par)
      {
         long wa = (preProd*stk.pop())%MODO;
         go(j, i, (int)wa);
         preProd *= (ways[j]+1);
         preProd %= MODO;
      }
   }

   long modInv(long a, long m)
   {
      if (a == 0) return 1L;
      return BigInteger.valueOf(a).modInverse(BigInteger.valueOf(m)).longValue();
   }

   public D(FastScanner in)
   {
      N = in.nextInt();
      adj = new ArrayList[N];
      for (int i=0; i<N; i++)
         adj[i] = new ArrayList<Integer>();
      for (int i=1; i<N; i++)
      {
         int j = in.nextInt()-1;
         adj[i].add(j);
         adj[j].add(i);
      }
      res = new int[N];
      ways = new int[N];
      dfs(0, 0);
      go(0, 0, 0);
      StringBuilder sb = new StringBuilder();
      for (int r : res)
      {
         sb.append(r);
         sb.append(' ');
      }
      System.out.println(sb.toString().trim());
   }
}

class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner()
    {
        stream = System.in;
    }

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    boolean isSpaceChar(int c)
    {
        return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
    }

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}


import java.util.*;
import java.math.*;
import java.io.*;

public class D
{
   public static void main(String[] args)
   {
      new D(new FastScanner());
   }

   int N, MODO = (int)1e9+7;
   ArrayList<Integer>[] adj;
   int[] res, ways;
   void dfs(int i, int par)
   {
      ways[i] = 1;
      for (int j : adj[i]) if (j != par)
      {
         dfs(j, i);
         long rr = ((ways[j]+1L)*ways[i])%MODO;
         ways[i] = (int)rr;
      }
   }

   void go(int i, int par, int waysAbove)
   {
      long prod = ((1L+waysAbove)*ways[i])%MODO;
      res[i] = (int)prod;
      for (int j : adj[i]) if (j != par)
      {
         long wa = (prod*modInv(ways[j]+1, MODO))%MODO;
         go(j, i, (int)wa);
      }
   }

   long modInv(long a, long m)
   {
      if (a == 0) return 0;
      return BigInteger.valueOf(a).modInverse(BigInteger.valueOf(m)).longValue();
   }

   public D(FastScanner in)
   {
      N = in.nextInt();
      adj = new ArrayList[N];
      for (int i=0; i<N; i++)
         adj[i] = new ArrayList<Integer>();
      for (int i=1; i<N; i++)
      {
         int j = in.nextInt()-1;
         adj[i].add(j);
         adj[j].add(i);
      }
      res = new int[N];
      ways = new int[N];
      dfs(0, 0);
      go(0, 0, 0);
      StringBuilder sb = new StringBuilder();
      for (int r : res)
      {
         sb.append(r);
         sb.append(' ');
      }
      System.out.println(sb.toString().trim());
   }
}

class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner()
    {
        stream = System.in;
    }

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    boolean isSpaceChar(int c)
    {
        return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
    }

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}


import java.util.*;
import java.io.*;

public class B
{
   public static void main(String[] args)
   {
      new B(new FastScanner());
   }

   public B(FastScanner in)
   {
      int N = in.nextInt();
      int K = in.nextInt();
      boolean[][] isLand = new boolean[N][N];
      for (int i=0; i<N; i++)
         for (int j=0; j<N; j++)
            if (((i+j)&1) == 0 && K > 0)
            {
               isLand[i][j] = true;
               K--;
            }
      if (K > 0)
      {
         System.out.println("NO");
         return;
      }

      System.out.println("YES");
      for (int i=0; i<N; i++)
      {
         StringBuilder sb = new StringBuilder();
         for (int j=0; j<N; j++)
         {
            sb.append(isLand[i][j] ? 'L' : 'S');
         }
         System.out.println(sb);
      }
   }
}

class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner()
    {
        stream = System.in;
    }

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    boolean isSpaceChar(int c)
    {
        return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
    }

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}


import java.util.*;
import java.math.*;
import java.io.*;

public class D
{
   public static void main(String[] args)
   {
      new D(new FastScanner());
   }

   int N, MODO = (int)1e9+7;
   ArrayList<Integer>[] adj;
   int[] res, ways;
   void dfs(int i, int par)
   {
      ways[i] = 1;
      for (int j : adj[i]) if (j != par)
      {
         dfs(j, i);
         long rr = ((ways[j]+1L)*ways[i])%MODO;
         ways[i] = (int)rr;
      }
   }

   void go(int i, int par, int waysAbove)
   {
      long prod = ((1L+waysAbove)*ways[i])%MODO;
      res[i] = (int)prod;
      for (int j : adj[i]) if (j != par)
      {
         long wa = (prod*modInv((ways[j]+1)%MODO, MODO))%MODO;
         go(j, i, (int)wa);
      }
   }

   long modInv(long a, long m)
   {
      if (a == 0) return 1L;
      return BigInteger.valueOf(a).modInverse(BigInteger.valueOf(m)).longValue();
   }

   public D(FastScanner in)
   {
      N = in.nextInt();
      adj = new ArrayList[N];
      for (int i=0; i<N; i++)
         adj[i] = new ArrayList<Integer>();
      for (int i=1; i<N; i++)
      {
         int j = in.nextInt()-1;
         adj[i].add(j);
         adj[j].add(i);
      }
      res = new int[N];
      ways = new int[N];
      dfs(0, 0);
      go(0, 0, 0);
      StringBuilder sb = new StringBuilder();
      for (int r : res)
      {
         sb.append(r);
         sb.append(' ');
      }
      System.out.println(sb.toString().trim());
   }
}

class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner()
    {
        stream = System.in;
    }

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    boolean isSpaceChar(int c)
    {
        return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
    }

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}


import java.util.*;
import java.math.*;
import java.io.*;

public class D
{
   public static void main(String[] args)
   {
      new D(new FastScanner());
   }

   int N, MODO = (int)1e9+7;
   ArrayList<Integer>[] adj;
   int[] res, ways;
   void dfs(int i, int par)
   {
      ways[i] = 1;
      for (int j : adj[i]) if (j != par)
      {
         dfs(j, i);
         long rr = ((ways[j]+1L)*ways[i])%MODO;
         ways[i] = (int)rr;
      }
   }

   void go(int i, int par, int waysAbove)
   {
      long prod = ((1L+waysAbove)*ways[i])%MODO;
      res[i] = (int)prod;
      for (int j : adj[i]) if (j != par)
      {
         long wa = (prod*modInv((ways[j]+1)%MODO, MODO))%MODO;
         go(j, i, (int)wa);
      }
   }

   long modInv(long a, long m)
   {
      if (a == 0) return 0;
      return BigInteger.valueOf(a).modInverse(BigInteger.valueOf(m)).longValue();
   }

   public D(FastScanner in)
   {
      N = in.nextInt();
      adj = new ArrayList[N];
      for (int i=0; i<N; i++)
         adj[i] = new ArrayList<Integer>();
      for (int i=1; i<N; i++)
      {
         int j = in.nextInt()-1;
         adj[i].add(j);
         adj[j].add(i);
      }
      res = new int[N];
      ways = new int[N];
      dfs(0, 0);
      go(0, 0, 0);
      StringBuilder sb = new StringBuilder();
      for (int r : res)
      {
         sb.append(r);
         sb.append(' ');
      }
      System.out.println(sb.toString().trim());
   }
}

class FastScanner{
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;

    public FastScanner()
    {
        stream = System.in;
    }

    int read()
    {
        if (numChars == -1)
            throw new InputMismatchException();
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read(buf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (numChars <= 0)
                return -1;
        }
        return buf[curChar++];
    }

    boolean isSpaceChar(int c)
    {
        return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
    }

    boolean isEndline(int c)
    {
        return c=='\n'||c=='\r'||c==-1;
    }

    int nextInt()
    {
        return Integer.parseInt(next());
    }

    long nextLong()
    {
        return Long.parseLong(next());
    }

    double nextDouble()
    {
        return Double.parseDouble(next());
    }

    String next(){
        int c = read();
        while (isSpaceChar(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isSpaceChar(c));
        return res.toString();
    }

    String nextLine(){
        int c = read();
        while (isEndline(c))
            c = read();
        StringBuilder res = new StringBuilder();
        do{
            res.appendCodePoint(c);
            c = read();
        }while(!isEndline(c));
        return res.toString();
    }
}


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * #
 *
 * @author pttrung
 */
public class D_Round_302_Div1 {

	public static long MOD = 1000000007;

	public static void main(String[] args) throws FileNotFoundException {
		// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
		// "output.txt")));
		PrintWriter out = new PrintWriter(System.out);
		Scanner in = new Scanner();
		int n = in.nextInt();
		ArrayList<Integer>[] map = new ArrayList[n];
		for (int i = 0; i < n; i++) {
			map[i] = new ArrayList();
		}
		for (int i = 0; i < n - 1; i++) {
			int v = in.nextInt() - 1;
			map[v].add(i + 1);
			map[i + 1].add(v);
		}
		BigInteger[] count = new BigInteger[n];
		BigInteger[]result = new BigInteger[n];
		cal1(0,0,count, map);
		//System.out.println(Arrays.toString(count));
		cal(0,0,count, result, map);
		for(BigInteger i : result){
			out.print(i.mod(BigInteger.valueOf(MOD)).longValue() + " ");
		}
		out.close();
	}

	public static BigInteger cal1(int node, int pa, BigInteger[] count,
			ArrayList<Integer>[] map) {
		count[node] = BigInteger.ONE;
		for (int i : map[node]) {
			if (i != pa) {
				count[node] =count[node].multiply( (cal1(i, node, count, map).add(BigInteger.ONE)));

			}
		}
		return count[node];
	}

	public static void cal(int node, int pa, BigInteger[]count, BigInteger[]re, ArrayList<Integer>[]map){

		if(node == pa){
			re[node] = count[node];
		}else{
			//System.out.println(re[node] + " " + (count[node] + 1));
			BigInteger x = re[pa].divide(count[node].add(BigInteger.ONE)).add(BigInteger.ONE);

			re[node] = count[node].multiply(x);

		}
		for(int i : map[node]){
			if(i != pa){
				cal(i, node, count, re, map);
			}
		}

	}

	public static int[] KMP(String val) {
		int i = 0;
		int j = -1;
		int[] result = new int[val.length() + 1];
		result[0] = -1;
		while (i < val.length()) {
			while (j >= 0 && val.charAt(j) != val.charAt(i)) {
				j = result[j];
			}
			j++;
			i++;
			result[i] = j;
		}
		return result;

	}

	public static boolean nextPer(int[] data) {
		int i = data.length - 1;
		while (i > 0 && data[i] < data[i - 1]) {
			i--;
		}
		if (i == 0) {
			return false;
		}
		int j = data.length - 1;
		while (data[j] < data[i - 1]) {
			j--;
		}
		int temp = data[i - 1];
		data[i - 1] = data[j];
		data[j] = temp;
		Arrays.sort(data, i, data.length);
		return true;
	}

	public static int digit(long n) {
		int result = 0;
		while (n > 0) {
			n /= 10;
			result++;
		}
		return result;
	}

	public static double dist(long a, long b, long x, long y) {
		double val = (b - a) * (b - a) + (x - y) * (x - y);
		val = Math.sqrt(val);
		double other = x * x + a * a;
		other = Math.sqrt(other);
		return val + other;

	}

	public static class Point implements Comparable<Point> {

		int x, y;

		public Point(int start, int end) {
			this.x = start;
			this.y = end;
		}

		@Override
		public int hashCode() {
			int hash = 5;
			hash = 47 * hash + this.x;
			hash = 47 * hash + this.y;
			return hash;
		}

		@Override
		public boolean equals(Object obj) {
			if (obj == null) {
				return false;
			}
			if (getClass() != obj.getClass()) {
				return false;
			}
			final Point other = (Point) obj;
			if (this.x != other.x) {
				return false;
			}
			if (this.y != other.y) {
				return false;
			}
			return true;
		}

		@Override
		public int compareTo(Point o) {
			return x - o.x;
		}
	}

	public static class FT {

		long[] data;

		FT(int n) {
			data = new long[n];
		}

		public void update(int index, long value) {
			while (index < data.length) {
				data[index] += value;
				index += (index & (-index));
			}
		}

		public long get(int index) {
			long result = 0;
			while (index > 0) {
				result += data[index];
				index -= (index & (-index));
			}
			return result;

		}
	}

	public static long gcd(long a, long b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	public static long pow(long a, long b) {
		if (b == 0) {
			return 1;
		}
		if (b == 1) {
			return a;
		}
		long val = pow(a, b / 2);
		if (b % 2 == 0) {
			return val * val % MOD;
		} else {
			return val * (val * a % MOD) % MOD;

		}
	}

	static class Scanner {

		BufferedReader br;
		StringTokenizer st;

		public Scanner() throws FileNotFoundException {
			// System.setOut(new PrintStream(new
			// BufferedOutputStream(System.out), true));
			br = new BufferedReader(new InputStreamReader(System.in));
			// br = new BufferedReader(new InputStreamReader(new
			// FileInputStream(new File("input.txt"))));
		}

		public String next() {

			while (st == null || !st.hasMoreTokens()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
					throw new RuntimeException();
				}
			}
			return st.nextToken();
		}

		public long nextLong() {
			return Long.parseLong(next());
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String nextLine() {
			st = null;
			try {
				return br.readLine();
			} catch (Exception e) {
				throw new RuntimeException();
			}
		}

		public boolean endLine() {
			try {
				String next = br.readLine();
				while (next != null && next.trim().isEmpty()) {
					next = br.readLine();
				}
				if (next == null) {
					return true;
				}
				st = new StringTokenizer(next);
				return st.hasMoreTokens();
			} catch (Exception e) {
				throw new RuntimeException();
			}
		}
	}
}