#include<stdio.h>
#include<string.h>
char a[100005],b[100000],c[100000];
int lena,lenb,lenc;
int temp1,temp2;
void forward()
{
	int i,temp=0,flag=0;
	for(i=0;i<lena;i++)
	{
		int j;
		for(j=0;j<lenb;j++)
		{
			if(a[i+j]!=b[j])
				break;
		}
		if(j==lenb)
		{flag=1;i+=lenb;break;}
	}
	if(flag==0)
		return;
	temp=0;
	flag=0;
	while(i<lena)
	{
		int j;
		for(j=0;j<lenc;j++)
		{
			if(a[i+j]!=c[j])
				break;
		}
		if(j==lenc)
		{temp1=1;break;}
		i++;
	}
}
void backward()
{
	int i=lena-1,flag=0;
	for(;i>=0;i--)
	{
		int j;
		for(j=0;j<lenb;j++)
		{
			if(a[i-j]==b[j])
				continue;
			else
				break;
		}
		if(j==lenb)
		{flag=1;i-=lenb;break;}
	}
	if(flag==0)
		return;
	while(i>=0)
	{
		int j;
		for(j=lenc-1;j>=0;j--)
		{
			if(a[i-j]==c[j])
				continue;
			else
				break;
		}
		if(j==-1)
		{temp2=1;break;}
		i--;
	}
}
int main()
{
	scanf("%s %s %s",a,b,c);
	lena=strlen(a);
	lenb=strlen(b);
	lenc=strlen(c);
	forward();
	backward();
	if(temp1==1 && temp2==0)
		printf("forward\n");
	if(temp1==0 && temp2==1)
		printf("backward\n");
	if(temp1==1 && temp2==1)
		printf("both\n");
	if(temp1==0 && temp2==0)
		printf("fantasy\n");
}


#include<stdio.h>
#include<string.h>
char a[100005],b[100000],c[100000];
int lena,lenb,lenc;
int temp1,temp2;
void forward()
{
	int i,temp=0,flag=0;
	for(i=0;i<lena;i++)
	{
		if(temp==lenb )
		{
			flag=1;
			break;
		}
		if(a[i]==b[temp])
			temp++;
	}
	if(flag==0)
		return;
	temp=0;
	flag=0;
	while(i<lena)
	{
		if(temp==lenc)
		{
			if(c[temp-1]==a[i-1])
			{
				temp1=1;
				flag=1;
			}
			break;
		}
		if(a[i++]==c[temp])
			temp++;
	}
	if(temp==lenc)
	{
		if(c[temp-2]==a[i-1])
		{
			temp1=1;
		}
		return;
	}
}
void backward()
{
	int i=lena-1,temp=lenb-1,flag=0;
	for(;i>=0;i--)
	{
		if(temp==-1)
		{
			flag=1;
			break;
		}
		if(a[i]==b[temp])
			temp--;
	}
	if(flag==0)
		return;
	temp=lenc-1;
	while(i>=0)
	{
		if(temp==-1)
		{
			temp2=1;
			return;
		}
		if(a[i]==c[temp])
			temp--;
		i--;
	}
	if(temp==-1)
	{
		temp2=1;
		return;
	}
}
int main()
{
	scanf("%s %s %s",a,b,c);
	lena=strlen(a);
	lenb=strlen(b);
	lenc=strlen(c);
	forward();
	backward();
	if(temp1==1 && temp2==0)
		printf("forward\n");
	if(temp1==0 && temp2==1)
		printf("backward\n");
	if(temp1==1 && temp2==1)
		printf("both\n");
	if(temp1==0 && temp2==0)
		printf("fantasy\n");
}


#include<stdio.h>
#include<string.h>
int main()
{
    long long i=0,j=0,k,flag=0,flag2=0,flag3=0,flag4=0,x,y,z;
    char T[100020],A[120],B[120];
    gets(T); //citim datele
    gets(A);
    gets(B);
    x=strlen(T); //lungimile sirurilor
    y=strlen(A);
    z=strlen(B);
    for(i=0;i<x;i++)
    {
        k = i;
        for(j=0;j<y&&flag==0&&T[k]!='\0';j++,k++)
        { //pentru j<lungimea lui A si flag=0 si sirul T nu a ajuns la sfarsit
            if(A[j] != T[k])
            {
                flag = 0;
                break;
            }
            if(j == y - 1)
            {
                flag = 1;
            }
        }
        k = i +y;
        for(j = 0; j <z&& flag2 == 0 && flag == 1&& T[k] != '\0'; j++)
        {
            if(B[j] != T[k])
            {
                flag2 = 0;
                break;
            }
            if(j == z - 1)
            {
                flag2 = 1;
            }
            k++;
        }
    }
    for(i =x- 1; i > -1; i--)
    {
        k = i;
        for(j = 0; j <y&& flag3 == 0 && k > -1; j++)
        {
            if(A[j] != T[k])
            {
                flag3 = 0;
                break;
            }
            if(j ==y- 1)
            {
                flag3 = 1;
            }
            k--;
        }
        k = i -y;
        for(j = 0; j <z&& flag4 == 0 && flag3 == 1 && k > -1; j++)
        {
            if(B[j] != T[k])
            {
                flag4 = 0;
                break;
            }
            if(j == z- 1)
            {
                flag4 = 1;
            }
            k--;
        }
    }
    if(flag == 1 && flag2 == 1 && flag3 == 1 && flag4 == 1)
    {
        printf("both");
    }
    else if(flag == 1 && flag2 == 1)
    {
        printf("forward");
    }
    else if(flag3 == 1 && flag4 == 1)
    {
        printf("backward");
    }
    else
    {
        printf("fantasy");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
    char str[100001],stt1[200],stt2[200];
    scanf("%s",&str);
    scanf("%s",&stt1);
    scanf("%s",&stt2);
    int f1 = 0,f2 = 0,i,j,t,len = strlen(str);
    char *p;
    if((strstr(str,stt1) != NULL) && (strstr((strstr(str,stt1) + strlen(stt1)),stt2) != NULL))
    {
        f1 = 1;
    }
    for(i = 0,j = len-1;i < len/2;i++,j--)
    {
        t = str[i];
        str[i] = str[j];
        str[j] = t;
    }
    if((strstr(str,stt1) != NULL) && (strstr((strstr(str,stt1) + strlen(stt1)),stt2) != NULL))
    {
        f2 = 1;
    }
    if(f1 == 1 && f2 == 1)
    {
        printf("both\n");
    }
    else if(f1 == 1)
    {
        printf("forward\n");
    }
    else if(f2 == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }
    return 0;
}


include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char v[100000], a1[100], a2[100], nv[100000];
    scanf("%s", &v);
    strcpy(nv, v);
    strrev(nv);
    scanf("%s", &a1);
    scanf("%s", &a2);
    if(strstr(v, a1) != NULL && strstr(v, a2) != NULL && strlen(v)>=(strlen(a1)+strlen(a2))){
        if(strstr(v, a1)>=strstr(v,a2) && strstr(nv, a1)>=strstr(nv, a2))
            printf("both");
        else if(strstr(v, a1)>strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <string.h>

int main(){
    char v[100005], a1[105], a2[105];
    scanf("%s %s %s", &v, &a1, &a2);
    int s, f=0, b=0, lv=strlen(v), la1=strlen(a1), la2=strlen(a2);
    for(int i=0; i<=lv-la1; i++){
        if(f==0){
            s=0;
            for(int j=0; j<la1; j++)
                if(v[i+j]!=a1[j]){
                    s=1;
                    break;
                }
            if(s==0){
                for(int j=i+la1; j<=lv-la2; j++){
                    s=0;
                    for(int k=0; k<la2; k++)
                        if(v[j+k]!=a2[k]){
                            s=1;
                            break;
                        }
                    if(s==0){
                        f=1;
                        break;
                    }
                }
            }
        }else break;
    }
    for(int i=lv; i>=la1-1; i--){
        if(b==0){
            s=0;
            for(int j=0; j<la1; j++)
                if(v[i-j]!=a1[j]){
                    s=1;
                    break;
                }
            if(s==0){
                for(int j=i-la1; j>=la2-1; j--){
                    s=0;
                    for(int k=0; k<la2; k++)
                        if(v[j-k]!=a2[k]){
                            s=1;
                            break;
                        }
                    if(s==0){
                        b=1;
                        break;
                    }
                }
            }
        }else break;
    }
    if(f==1 && b==1)
        printf("both");
    else if(f==1)
        printf("forward");
    else if(b==1)
        printf("backward");
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *rstrstr(char *__restrict s1, char *__restrict s2)
{
  size_t  s1len = strlen(s1);
  size_t  s2len = strlen(s2);
  char *s;

  if (s2len > s1len)
    return NULL;
  for (s = s1 + s1len - s2len; s >= s1; --s)
    if (strncmp(s, s2, s2len) == 0)
      return s;
  return NULL;
}

int main()
{
    char v[100111], a1[111], a2[111];
    scanf("%s", &v);
    scanf("%s", &a1);
    scanf("%s", &a2);
    if(strstr(v, a1) != NULL && strstr(v, a2) != NULL && strlen(v)>=(strlen(a1)+strlen(a2)) && (v-strstr(v,a1)>=0 || v-strstr(v,a2)>=0) && ((strstr(v,a1)-strstr(v,a2)>=0) || (strstr(v,a2)-strstr(v,a1))>=0)){
        if(strstr(v, a1)>=strstr(v,a2) && rstrstr(v, a1)<=rstrstr(v, a2) && (strstr(v,a1)!=rstrstr(v,a1) || strstr(v,a2)!=rstrstr(v,a2)))
            printf("both");
        else if(strstr(v, a1)>strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char v[100000], a1[100], a2[100], nv[100000];
    scanf("%s", &v);
    strcpy(nv, v);
    strrev(nv);
    scanf("%s", &a1);
    scanf("%s", &a2);
    if(strstr(v, a1) != NULL && strstr(v, a2) != NULL && strlen(v)>=(strlen(a1)+strlen(a2))){
        if(strstr(v, a1)>=strstr(v,a2) && strstr(nv, a1)>=strstr(nv, a2))
            printf("both");
        else if(strstr(v, a1)>strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *rstrstr(char *__restrict s1, char *__restrict s2)
{
  size_t  s1len = strlen(s1);
  size_t  s2len = strlen(s2);
  char *s;

  if (s2len > s1len)
    return NULL;
  for (s = s1 + s1len - s2len; s >= s1; --s)
    if (strncmp(s, s2, s2len) == 0)
      return s;
  return NULL;
}

int main()
{
    char v[100000], a1[100], a2[100];
    scanf("%s", &v);
    scanf("%s", &a1);
    scanf("%s", &a2);
    if(strstr(v, a1) != NULL && strstr(v, a2) != NULL && strlen(v)>=(strlen(a1)+strlen(a2))){
        if(strstr(v, a1)>=strstr(v,a2) && rstrstr(v, a1)<=rstrstr(v, a2))
            printf("both");
        else if(strstr(v, a1)>=strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<=strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{

    //AB and BA routes:
    char BA[100001];
    char AB[100001];

    //What he saw when he was awake:
    char string1[101];
    char string2[101];

    //My fake booleans:
    int fantasy = 0;
    int isAB = 0;
    int isBA = 0;

    //Pointers:
    char *point1 = NULL;
    char *point2 = NULL;

    //Geting the input:
    scanf("%s", BA);
    strcpy(AB, BA);
    strrev(BA);
    scanf("%s", string1);
    scanf("%s", string2);

    //Scan for AB:
    if(strstr(AB, string1) != NULL)
    {
        point1 = strstr(AB, string1);
    }
    else ++fantasy;
    if(strstr(AB, string2) != NULL)
    {
        point2 = strstr(AB, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 < point2) ++isAB;
    }

    //Resest pointers:
    point1 = NULL;
    point2 = NULL;

    //Scan for BA:
    if(strstr(BA, string1) != NULL)
    {
        point1 = strstr(BA, string1);
    }
    else ++fantasy;

    if(strstr(BA, string2) != NULL)
    {
        point2 = strstr(BA, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 < point2) ++isBA;
    }

    //Finding the result:
    if(fantasy == 4) printf("fantasy");
    else if(isBA == 1 && isAB == 1) printf("both");
    else if(isAB == 1) printf("forward");
    else if(isBA == 1) printf("backward");
    else printf("ERROR");

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//Last ocurance strstr function:
char* strstr_last(const char* str1, const char* str2)
{
char* strp;
int len1, len2;

len2 = strlen(str2);
if(len2==0)
return (char*)str1;

len1 = strlen(str1);
if(len1 - len2 <= 0)
return 0;

strp = (char*)(str1 + len1 - len2);
while(strp != str1)
{
if(*strp == *str2)
{
if(strncmp(strp,str2,len2)==0)
return strp;
}
strp--;
}
return 0;
}

int main()
{

    //AB and BA routes:
    char BA[100001];
    char AB[100001];

    //What he saw when he was awake:
    char string1[101];
    char string2[101];

    //My fake booleans:
    int fantasy = 0;
    int isAB = 0;
    int isBA = 0;

    //Pointers:
    char *point1 = NULL;
    char *point2 = NULL;

    //Geting the input:
    scanf("%s", BA);
    strcpy(AB, BA);
    strrev(BA);
    scanf("%s", string1);
    scanf("%s", string2);

    //Scan for AB:
    if(strstr(AB, string1) != NULL)
    {
        point1 = strstr(AB, string1);
    }
    else ++fantasy;
    if(strstr_last(AB, string2) != NULL)
    {
        point2 = strstr_last(AB, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 <= point2) ++isAB;
    }

    //Resest pointers:
    point1 = NULL;
    point2 = NULL;

    //Scan for BA:
    if(strstr(BA, string1) != NULL)
    {
        point1 = strstr(BA, string1);
    }
    else ++fantasy;

    if(strstr_last(BA, string2) != NULL)
    {
        point2 = strstr_last(BA, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 <= point2) ++isBA;
    }

    //Finding the result:
    if(fantasy == 4) printf("fantasy");
    else if(isBA == 1 && isAB == 1) printf("both");
    else if(isAB == 1) printf("forward");
    else if(isBA == 1) printf("backward");
    else printf("fantasy");

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//Last ocurance strstr function:
char* strstr_last(const char* str1, const char* str2)
{
char* strp;
int len1, len2;

len2 = strlen(str2);
if(len2==0)
return (char*)str1;

len1 = strlen(str1);
if(len1 - len2 <= 0)
return 0;

strp = (char*)(str1 + len1 - len2);
while(strp != str1)
{
if(*strp == *str2)
{
if(strncmp(strp,str2,len2)==0)
return strp;
}
strp--;
}
return 0;
}

int main()
{

    //AB and BA routes:
    char BA[100001];
    char AB[100001];

    //What he saw when he was awake:
    char string1[101];
    char string2[101];

    //My fake booleans:
    int fantasy = 0;
    int isAB = 0;
    int isBA = 0;

    //Pointers:
    char *point1 = NULL;
    char *point2 = NULL;

    //Geting the input:
    scanf("%s", BA);
    strcpy(AB, BA);
    strrev(BA);
    scanf("%s", string1);
    scanf("%s", string2);

    //Scan for AB:
    if(strstr(AB, string1) != NULL)
    {
        point1 = strstr(AB, string1);
    }
    else ++fantasy;
    if(strstr_last(AB, string2) != NULL)
    {
        point2 = strstr_last(AB, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 < point2) ++isAB;
    }

    //Resest pointers:
    point1 = NULL;
    point2 = NULL;

    //Scan for BA:
    if(strstr(BA, string1) != NULL)
    {
        point1 = strstr(BA, string1);
    }
    else ++fantasy;

    if(strstr_last(BA, string2) != NULL)
    {
        point2 = strstr_last(BA, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 < point2) ++isBA;
    }

    //Finding the result:
    if(strlen(AB)<strlen(string1)+strlen(string2)) printf("fantasy");
    else if(fantasy == 4) printf("fantasy");
    else if(isBA == 1 && isAB == 1) printf("both");
    else if(isAB == 1) printf("forward");
    else if(isBA == 1) printf("backward");
    else printf("fantasy");

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{

    //AB and BA routes:
    char BA[100001];
    char AB[100001];

    //What he saw when he was awake:
    char string1[101];
    char string2[101];

    //My fake booleans:
    int fantasy = 0;
    int isAB = 0;
    int isBA = 0;

    //Pointers:
    char *point1 = NULL;
    char *point2 = NULL;

    //Geting the input:
    scanf("%s", BA);
    strcpy(AB, BA);
    strrev(BA);
    scanf("%s", string1);
    scanf("%s", string2);

    //Scan for AB:
    if(strstr(AB, string1) != NULL)
    {
        point1 = strstr(AB, string1);
    }
    else ++fantasy;
    if(strstr(AB, string2) != NULL)
    {
        point2 = strstr(AB, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 < point2) ++isAB;
    }

    //Resest pointers:
    point1 = NULL;
    point2 = NULL;

    //Scan for BA:
    if(strstr(BA, string1) != NULL)
    {
        point1 = strstr(BA, string1);
    }
    else ++fantasy;

    if(strstr(BA, string2) != NULL)
    {
        point2 = strstr(BA, string2);
    }
    else ++fantasy;

    if(point1!=NULL && point2!=NULL)
    {
        if(point1 < point2) ++isBA;
    }

    //Finding the result:
    if(fantasy == 4) printf("fantasy");
    else if(isBA == 1 && isAB == 1) printf("both");
    else if(isAB == 1) printf("forward");
    else if(isBA == 1) printf("backward");
    else printf("both");

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	start3 = strstr(copy, color2);
	position3 = start3 - message;
	if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

bool check2(){
	message[position1] = '?';
	message[position2] = '?';
	if(strstr(message, color1)!=NULL || strstr(message, color2)!=NULL){
		return true;
	}
	else{
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else{
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1] = '?';
	//printf("%s\n", copy);
	start3 = strstr(copy, color2);
	position3 = start3 - copy;
	//printf("position3 - %d   position1 - %d", position3, position1);

	if(position3<0 || position3>1000000){
		return false;
	}
	else if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

bool check2(){
	message[position1] = '?';
	message[position2] = '?';
	if(strstr(message, color1)!=NULL || strstr(message, color2)!=NULL){
		return true;
	}
	else{
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else{
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, position4, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL, *start4 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1] = '?';
	//printf("%s\n", copy);
	start3 = strstr(copy, color2);
	position3 = start3 - copy;
	//printf("position3 - %d   position1 - %d", position3, position1);

	if(position3<0 || position3>1000000){
		return false;
	}
	else if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

bool check2(){

	if(position1==position2){
		return true;
	}

	message[position1] = '?';
	message[position2] = '?';

	start3 = strstr(message, color1);
	position3 = start3 - message;

	start4 = strstr(message, color2);
	position4 = start4 - message;

	//printf("%d %d", position3, position4);

	if(position1<position2 && strstr(message, color1)!=NULL && position3>position4 && position4>=0){
		//printf("wai");
		return true;
	}
	else if(position2<position1 && ( strstr(message, color2)!=NULL && position4>position3 && position3>=0))
	{
		//printf("wai2");
		return true;
	}
	else{
		//printf("Este !");
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else if(position1 < position2){
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, position4, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL, *start4 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1] = '?';
	//printf("%s\n", copy);
	start3 = strstr(copy, color2);
	position3 = start3 - copy;
	//printf("position3 - %d   position1 - %d", position3, position1);

	if(position3<0 || position3>1000000){
		return false;
	}
	else if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

bool check2(){

	if(position1==position2){
		return true;
	}

	message[position1] = '?';
	message[position2] = '?';

	start3 = strstr(message, color1);
	position3 = start3 - message;

	start4 = strstr(message, color2);
	position4 = start4 - message;

	if(position1<position2 && strstr(message, color1)!=NULL && start3>start4 && start4>=0){
		//printf("wai");
		return true;
	}
	else if(position2<position1 && ( strstr(message, color2)!=NULL && start4>start3 && start3>=0))
	{
		//printf("wai2");
		return true;
	}
	else{
		//printf("Este !");
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else if(position1 < position2){
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1] = '?';
	//printf("%s\n", copy);
	start3 = strstr(copy, color2);
	position3 = start3 - copy;
	//printf("position3 - %d   position1 - %d", position3, position1);

	if(position3<0 || position3>1000000){
		return false;
	}
	else if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

bool check2(){
	if(position1==position2){
		return false;
	}
	message[position1] = '?';
	message[position2] = '?';
	if(strstr(message, color1)!=NULL || strstr(message, color2)!=NULL){
		return true;
	}
	else{
		//printf("Este !");
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else if(position1 < position2){
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>

int main()
{
	char s[100001],t[100001],x[101],y[101],*p,*q;
	int i,l;
  	scanf("%s%s%s",s,x,y);
  	for(i=0,l=strlen(s);i<l;i++)
  	{
  		t[i]=s[l-i-1];
	}
  	p=strstr(s,x);if(p)p=strstr(p+strlen(x),y);
  	q=strstr(t,x);if(q)q=strstr(q+strlen(x),y);
  	if(p){
    if(q)puts("both");
    else puts("forward");
  }else{
    if(q)puts("backward");
    else puts("fantasy");
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char color1[100];
char color2[100];
int position1, position2, count1, count2;
char *start = NULL, *start2 = NULL;

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		return true;
	}else{
		return false;
	}
}

bool check2(){
	message[position1] = '?';
	message[position2] = '?';
	if(strstr(message, color1)!=NULL || strstr(message, color2)!=NULL){
		return true;
	}
	else{
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		start = strstr(message, color1);
		position1 = start - message;
		start2 = strstr(message, color2);
		position2 = start2 - message;

		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else{
			printf("forward");
		}
	}

	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, position4, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL, *start4 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1] = '?';
	start3 = strstr(copy, color2);
	position3 = start3 - copy;
	if(position3<0 || position3>1000000){
		return false;
	}
	else if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

int max(int a, int b){
	if(a>b){
		return a;
	}
	else{
		return b;
	}
}

int min(int a, int b){
	if(a<b){
		return a;
	}
	else{
		return b;
	}
}

bool check2(){

	if(position1==position2){
		return true;
	}

	message[position1] = '?';
	message[position2] = '?';

	start3 = strstr(message, color1);
	position3 = start3 - message;

	start4 = strstr(message, color2);
	position4 = start4 - message;

//	printf("%d %d", position1, position2);
//	printf("%s\n", message);
//	printf("%d %d\n", position3, position4);

	if(position3>min(position1, position2)&&position3<max(position1, position2)){
		return false;
	}
	else if(position4>min(position1, position2)&&position4<max(position1, position2)){
		return false;
	}
	else if(position3<0 || position4<0){
		return false;
	}

//	if(position1<position2 && strstr(message, color1)!=NULL && position3>position4 && position3>=0){
//		printf("wai");
//		return true;
//	}
//	else if(position2<position1 && ( strstr(message, color2)!=NULL && position4>position3 && position4>=0))
//	{
//		printf("wai2");
//		return true;
//	}
	else{
		printf("Este !");
		return true;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else if(position1 < position2){
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, position4, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL, *start4 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1] = '?';
	start3 = strstr(copy, color2);
	position3 = start3 - copy;
	if(position3<0 || position3>1000000){
		return false;
	}
	else if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

int max(int a, int b){
	if(a>b){
		return a;
	}
	else{
		return b;
	}
}

int min(int a, int b){
	if(a<b){
		return a;
	}
	else{
		return b;
	}
}

bool check2(){

	if(position1==position2){
		return true;
	}

	message[position1] = '?';
	message[position2] = '?';

	start3 = strstr(message, color1);
	position3 = start3 - message;

	start4 = strstr(message, color2);
	position4 = start4 - message;

//	printf("%d %d", position1, position2);
//	printf("%s\n", message);
//	printf("%d %d\n", position3, position4);

	if(position3>min(position1, position2)&&position3<max(position1, position2)){
		return false;
	}
	else if(position4>min(position1, position2)&&position4<max(position1, position2)){
		return false;
	}
	else if(position3<0 && position4<0){
		return false;
	}

//	if(position1<position2 && strstr(message, color1)!=NULL && position3>position4 && position3>=0){
//		printf("wai");
//		return true;
//	}
//	else if(position2<position1 && ( strstr(message, color2)!=NULL && position4>position3 && position4>=0))
//	{
//		printf("wai2");
//		return true;
//	}
	else{
		//printf("Este !");
		return true;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else if(position1 < position2){
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char color1[100];
char color2[100];
int position1, position2, count1, count2;
char *start = NULL, *start2 = NULL;

bool check(){
	if(strcmp(color1, color2)==0){
		return false;
	}
	else if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		return true;
	}else{
		return false;
	}
}

bool check2(){
	message[position1] = '?';
	message[position2] = '?';
	if(strstr(message, color1)!=NULL || strstr(message, color2)!=NULL){
		return true;
	}
	else{
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		start = strstr(message, color1);
		position1 = start - message;
		start2 = strstr(message, color2);
		position2 = start2 - message;

		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else{
			printf("forward");
		}
	}

	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1]='?';
	start3 = strstr(copy, color2);
	position3 = start3 - message;
	if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return false;
		}
		return true;
	}
		return true;
	}else{
		return false;
	}
}

bool check2(){
	message[position1] = '?';
	message[position2] = '?';
	if(strstr(message, color1)!=NULL || strstr(message, color2)!=NULL){
		return true;
	}
	else{
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else{
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>

char message[100000];
char copy[100000];
char color1[100];
char color2[100];
int position1, position2, position3, count1, count2;
char *start = NULL, *start2 = NULL, *start3 = NULL;

bool check3(){
	strcpy(copy, message);
	copy[position1] = '?';
	copy[position1+1] = '?';
	//printf("%s\n", copy);
	start3 = strstr(copy, color2);
	position3 = start3 - copy;
	//printf("position3 - %d   position1 - %d", position3, position1);

	if(position3<0 || position3>1000000){
		return false;
	}
	else if(position3!=position1){
		return true;
	}
	else{
		return false;
	}
}

bool check(){
	if(strstr(message, color1)!=NULL && strstr(message, color2)!=NULL){
		if(strcmp(color1, color2)==0){
		if(check3()){
			return true;
		}
		return false;
	}
		return true;
	}else{
		return false;
	}
}

bool check2(){
	message[position1] = '?';
	message[position2] = '?';
	if(strstr(message, color1)!=NULL || strstr(message, color2)!=NULL){
		return true;
	}
	else{
		//printf("Este !");
		return false;
	}
}

int main(){
	scanf("%s\n", message);
	scanf("%s\n", color1);
	scanf("%s", color2);
	start = strstr(message, color1);
	position1 = start - message;
	start2 = strstr(message, color2);
	position2 = start2 - message;

	if(!check()){
		printf("fantasy");
		return 0;
	}else{
		if(check2()){
			printf("both");
		}
		else if(position1 > position2){
			printf("backward");
		}
		else if(position1 < position2){
			printf("forward");
		}
	}
	return 0;
}


#include <stdio.h>
#include <stdlib.h>

// returns length of string
long len(char *str)
{
    long i = 0;
    while (str[i] != '\0')
    {
        i++;
    }
    return i;
}

// returns true if X and Y are same
int compare(char *X, char *Y)
{
    while (*X && *Y)
    {
        if (*X != *Y)
            return 0;

        X++;
        Y++;
    }

    return (*Y == '\0');
}

// Function to implement strstr() function
long pos(char *X, char *Y)
{
    long i = 0;
    while (X[i] != '\0')
    {
        if ((X[i] == Y[0]) && compare(X + i, Y))
            return i;
        i++;
    }

    return -1;
}

// reverses a string
void reverse(char s[])
{
    long length = len(s);
    long c, i, j;

    for (i = 0, j = length - 1; i < j; i++, j--)
    {
        c = s[i];
        s[i] = s[j];
        s[j] = c;
    }
}

int main()
{
    char base[100001];
    char first[101];
    char second[101];

    scanf("%s", base);
    scanf("%s", first);
    scanf("%s", second);

    int forward = 0, backward = 0;

    long forwardFirstPos = pos(base, first);
    long forwardSecondPos = pos(base + forwardFirstPos + len(first), second);
    if (forwardSecondPos >= 0)
    {
        forwardSecondPos += forwardFirstPos + len(first);
    }
    if (forwardFirstPos >= 0 && forwardSecondPos >= 0 && forwardFirstPos < forwardSecondPos)
    {
        forward = 1;
    }

    reverse(base);

    long backwardFirstPos = pos(base, first);
    long backwardSecondPos = pos(base + backwardFirstPos + len(first), second);
    if (backwardSecondPos >= 0)
    {
        backwardSecondPos += backwardFirstPos + len(first);
    }
    if (backwardFirstPos >= 0 && backwardSecondPos >= 0 && backwardFirstPos < backwardSecondPos)
    {
        backward = 1;
    }

    if (forward == 1 && backward == 1)
    {
        printf("both\n");
    }
    else if (forward == 1)
    {
        printf("forward\n");
    }
    else if (backward == 1)
    {
        printf("backward\n");
    }
    else
    {
        printf("fantasy\n");
    }

    return 0;
}


#include <stdio.h>
#include <string.h>

char s[123456];
char a[123];
char b[123];

void reverse(char *s){
	char r, *t;

	for(t = s + strlen(s) - 1; s < t ; s++, t--){
		r = *s, *s = *t, *t = r;
	}
}

int find(char *s, char *a, char *b){
	return strstr(s, a) != NULL && strstr(s + strlen(a), b) != NULL;
}

int main(){

	scanf("%s%s%s", s, a, b);

	int f = 0, bb = 0;

	f = find(s, a, b);
	reverse(s);
	bb = find(s, a, b);

	if(f + bb == 2){
		printf("both\n");
	}

	else if(f == 1){
		printf("forward\n");
	}

	else if(bb == 1){
		printf("backward\n");
	}

	else{
		printf("fantasy\n");
	}

	return 0;
}


#include <stdio.h>
#include <string.h>

char s[123456];
char a[123];
char b[123];

void reverse(char *s){
	char r, *t;

	for(t = s + strlen(s) - 1; s < t ; s++, t--){
		r = *s, *s = *t, *t = r;
	}
}

int find(char *s, char *a, char *b){
	return (s = strstr(s, a)) != NULL && strstr(s + strlen(a), b) != NULL;
}

int main(){

	scanf("%s%s%s", s, a, b);

	int f = find(s, a, b);
	reverse(s);
	int bb = find(s, a, b);

	if(f + bb == 2){
		printf("both\n");
	}

	else if(f == 1){
		printf("forward\n");
	}

	else if(bb == 1){
		printf("backward\n");
	}

	else{
		printf("fantasy\n");
	}

	return 0;
}


#include <stdio.h>
#include <string.h>

void reverse(char *x)
{
	char *p=x;
	while(p && *p) ++p;
	for(--p;x<p;++x,--p)
		*x=*x^*p, *p=*x^*p, *x=*x^*p;
}
int func(char *str,char* a,char *b, int dir)
{
	if(dir){
		reverse(b);
		reverse(a);

		char *c=strstr(str,b);
		if(c==NULL) return 0;

		if(strstr(c+strlen(b),a)==NULL) return 0;
		else return 2;
	}
	else {
		char *c=strstr(str,a);
		if(c==NULL) return 0;

		if(strstr(c+strlen(a),b)==NULL) return 0;
		else return 1;
	}
}
int main(void)
{
	char stat[100001],seq1[101],seq2[101];

	scanf("%s",stat);
	scanf("%s",seq1);
	scanf("%s",seq2);

	int result=func(stat,seq1,seq2,0)+func(stat,seq1,seq2,1);

	if(result==0) printf("fantasy");
	else if(result==1) printf("forward");
	else if(result==2) printf("backward");
	else printf("both");
	return 0;
}


#include<stdio.h>
#include<string.h>

int main()
{
   int i,l1,l2,l,j,f=0,b=0;

  char AB[100];
  char ab[100];
  char x[100];
  char y[100];
  char ba[100];
  gets(AB);
  gets(ab);
  gets(ba);

  strcpy(x,AB);
  strcpy(y,AB);
    l1=strlen(ab);
   l2=strlen(ba);
   l=strlen(y);
   //printf("%d",l);

  for(i=0;ab[i]!='\0';i++)
  {  for(j=0;x[j]!='\0';j++)
         {
             if(ab[i]!=x[j])

              {

               x[j]='X';

              }
             else if (ab[i]==x[j])
             {
                 f++;
                  x[j]='X';
                  break;
             }
         }

  }

   for(i=0;ba[i]!='\0';i++)
  {  for(j=l;j>=0;j--)
         {
             if(ba[i]!=y[j])

              {

               y[j]='X';

              }
             else if (ba[i]==y[j])
             {
                 b++;
                  y[j]='X';
                  break;
             }
         }

  }

//printf("%d %d %d %d",f,b,l1,l2);

  if(f==l1 && b==l2)
  {
      printf("both");
  }

 else if(f==l1)
  {
      printf("forward");
  }

  else if(b==l2)
  {
      printf("backward");
  }

   else
  {
      printf("fantasy");
  }
}


#include<stdio.h>
#include<string.h>
int main()
{
    char *p,*q,s[1000],x[10000],y[1000],t[1000];

    gets(s);
    gets(x);
    gets(y);

    p=strstr(s,x);

    if(p)
        p=strstr(p+strlen(x),y);

     strrev(s);

   q=strstr(s,x);

    if(q)
        q=strstr(q+strlen(x),y);

        if(p)
        {
            if(q)
                puts("both");
            else
                puts("forward");

        }

    else if(q)
         {
             puts("backward");
         }
    else
      puts("fantasy");

}


#include<stdio.h>
#include<string.h>
int main()
{
    char *p,*q,s[100000],x[10000],y[1000],t[1000];

    gets(s);
    gets(x);
    gets(y);

    p=strstr(s,x);

    if(p)
        p=strstr(p+strlen(x),y);

     strrev(s);

   q=strstr(s,x);

    if(q)
        q=strstr(q+strlen(x),y);

        if(p)
        {
            if(q)
                puts("both");
            else
                puts("forward");

        }

    else if(q)
         {
             puts("backward");
         }
    else
      puts("fantasy");

}


main(i,l){
  char s[100001],t[100001],x[101],y[101],*p,*q;
  scanf("%s%s%s",s,x,y);
  {for(i=0,l=strlen(s);i<l;i++)t[i]=s[l-i-1];}
  p=strstr(s,x);if(p)p=strstr(p+strlen(x),y);
  q=strstr(t,x);if(q)q=strstr(q+strlen(x),y);
  if(p){
    if(q)puts("both");
    else puts("forward");
  }else{
    if(q)puts("backward");
    else puts("fantasy");
  }
  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char path[1000000],fr[101],sc[101];
int result=0;
gets(path);
gets(fr);
gets(sc);

int i,is,flag;
if(strlen(path)>=strlen(fr)+strlen(sc)){
for(i = 0; path[i] != '\0'; i++){
		if(path[i] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[i + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = i;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1){
                result++;
                break;
            }
        }

	}
	strrev(path);
	flag=0;
	for(is = 0; path[is] != '\0'; is++){
		if(path[is] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[is + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = is;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1 && result==0){
                printf("backward");
                result=-3;
                break;
            }
            else if(flag==1 && result==1 ){
                result++;
            }
        }

	}
	if(result==1){
        printf("forward");
	}
	else if(result ==2){
        printf("both");
	}
	else if(result==0){
        printf("fantasy");
	}
}
else{
    printf("fantasy");
}
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char path[100000],fr[101],sc[101];
int result=0;
gets(path);
gets(fr);
gets(sc);

int i,is,flag;
if(strlen(path)>strlen(fr)+strlen(sc)){
for(i = 0; path[i] != '\0'; i++){
		if(path[i] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[i + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = i;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1){
                result++;
                break;
            }
        }

	}
	strrev(path);
	flag=0;
	for(is = 0; path[is] != '\0'; is++){
		if(path[is] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[is + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = is;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1 && result==0){
                printf("backward");
                break;
            }
            else if(flag==1 && result==1 ){
                result++;
            }
        }

	}
	if(result==1){
        printf("forward");
	}
	else if(result ==2){
        printf("both");
	}
	else{
        printf("fantasy");
	}
}
else{
    printf("fantasy");
}
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char path[100000],fr[101],sc[101];
int result=0;
gets(path);
gets(fr);
gets(sc);

int i,is,flag;
if(strlen(path)>=strlen(fr)+strlen(sc)){
for(i = 0; path[i] != '\0'; i++){
		if(path[i] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[i + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = i;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1){
                result++;
                break;
            }
        }

	}
	strrev(path);
	flag=0;
	for(is = 0; path[is] != '\0'; is++){
		if(path[is] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[is + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = is;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1 && result==0){
                printf("backward");
                result=-3;
                break;
            }
            else if(flag==1 && result==1 ){
                result++;
            }
        }

	}
	if(result==1){
        printf("forward");
	}
	else if(result ==2){
        printf("both");
	}
	else if(result==0){
        printf("fantasy");
	}
}
else{
    printf("fantasy");
}
return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    char *p,*q,path[100000],fr[10000],sc[1000],t[1000];

    gets(path);
    gets(fr);
    gets(sc);
    p=strstr(path,fr);
    if(p){
        p=strstr(p+strlen(fr),sc);
    }
   strrev(path);
   q=strstr(path,fr);
    if(q){
        q=strstr(q+strlen(fr),sc);
        }
    if(p){
            if(q)
                puts("both");
            else
                puts("forward");
        }

    else if(q)
         {
             puts("backward");
         }
    else
      puts("fantasy");

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char path[100000],fr[101],sc[101];
int result=0;
gets(path);
gets(fr);
gets(sc);
int i,is,flag;
for(i = 0; path[i] != '\0'; i++){
		if(path[i] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[i + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = i;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1){
                result++;
                break;
            }
        }

	}
	strrev(path);
	flag=0;
	for(is = 0; path[is] != '\0'; is++){
		if(path[is] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[is + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = is;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1 && result==0){
                printf("backward");
                break;
            }
            else if(flag==1 && result==1 ){
                result++;
            }
        }

	}
	if(result==1){
        printf("forward");
	}
	else if(result ==2){
        printf("both");
	}
	else{
        printf("fantasy");
	}

return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char path[1000000],fr[100000],sc[100000];
int result=0;
gets(path);
gets(fr);
gets(sc);

int i,is,flag;
if(strlen(path)>=strlen(fr)+strlen(sc)){
for(i = 0; path[i] != '\0'; i++){
		if(path[i] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[i + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = i;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1){
                result++;
                break;
            }
        }

	}
	strrev(path);
	flag=0;
	for(is = 0; path[is] != '\0'; is++){
		if(path[is] == fr[0]){
        flag=1;
			for(int j = 0; fr[j] != '\0'; j++)
			{
				if(path[is + j] != fr[j])
				{
					flag = 0;
					break;
				}
			}

		}
		if(flag == 1)
		{
		    break;
		}
	}
	if(flag){
            flag=0;
            for(int w = is;path[w] != '\0';w++){
                if(path[w]==sc[0]){
                        flag=1;
                        for(int j = 0; sc[j] != '\0'; j++){
                            if(path[w + j] != sc[j]){
                                flag = 0;
                                break;
                            }
                    }
            }
             if(flag==1 && result==0){
                printf("backward");
                result=-3;
                break;
            }
            else if(flag==1 && result==1 ){
                result++;
            }
        }

	}
	if(result==1){
        printf("forward");
	}
	else if(result==2){
        printf("both");
	}
	else if(result==0){
        printf("fantasy");
	}
}
else{
    printf("fantasy");
}
return 0;
}


#include<stdio.h>
#include<string.h>

char s[100005],sa[105],sb[105];
int main()
{
    while(scanf("%s%s%s",s,sa,sb)!=EOF)
    {
        int l=strlen(s);
        int la=strlen(sa);
        int lb=strlen(sb);
        int flag1=0,pos=0,flag2=0,flag3=0,flag4=0;
        int i,j;
        for(i=0;i<l;i++)
        {
            flag1=1;
            for( j=0;j<la;j++)
            {
                if(s[i+j]!=sa[j])
                {
                    flag1=0;
                    break;
                }
            }
            if(flag1==1)
            {
                pos=i+la;
                break;
            }
        }
        if(flag1==1)
        {
            for(i=pos;i<l;i++)
            {
                flag2=1;
                for(j=0;j<lb;j++)
                {
                    if(s[i+j]!=sb[j])
                    {
                        flag2=0;
                        break;
                    }
                }
                if(flag2==1)
                break;
            }
        }
        //???
        for(i=l-1;i>=0;i--)
        {
            flag3=1;
            for(j=0;j<la;j++)
            {
                if(s[i-j]!=sa[j])
                {
                    flag3=0;
                    break;
                }
            }
            if(flag3==1)
            {
                pos=i-la;
                break;
            }
        }
        for(i=pos;i>=0;i--)
        {
            flag4=1;
            for(j=0;j<lb;j++)
            {
                if(s[i-j]!=sb[j])
                {
                    flag4=0;
                    break;
                }
            }
            if(flag4==1)
            break;
        }
        if(flag2==1 && flag4==1)
        {
            printf("both\n");
        }
        else if(flag2==1)
        {
            printf("forward\n");
        }
        else if(flag4==1)
        {
            printf("backward\n");
        }
        else
        printf("fantasy\n");
    }
    return 0;
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define x first
#define y second
//#define int ll
#define NAME ""

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using ull = unsigned long long;

const int maxn = 1e6;
const int mod = 998244353;
const int M = 1e9 + 7;
const int inf = 1e9;
const ll inf64 = 1e18L;
const ld pi = acos((ld)-1.0);
const ld eps = (ld)1e-6;

int n, a[maxn];
ll s[maxn], S;
vi g[maxn];

void dfs(int v, int p) {
    s[v] = a[v];
    for(int to : g[v]) if(to != p) dfs(to, v), s[v] += s[to];
}

ll pw(ll a, ll b) {
    if(!b) return 1;
    if(b & 1) return a * pw(a, b - 1) % M;
    a = pw(a, b >> 1);
    return a * a % M;
}

ll fac[maxn + 1];

ll cnk(ll n, ll k) {
    return fac[n] * pw((fac[k] * fac[n - k]) % M, M - 2) % M;
}

main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen(NAME".in", "r", stdin); freopen(NAME".out", "w", stdout);
    //freopen("in.txt", "r", stdin);
    fac[0] = 1;
    for(int i = 0; i < maxn; i++) fac[i + 1] = fac[i] * (i + 1) % M;

    int hi = 0;
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], S += a[i], hi = max(hi, a[i]);
    for(int i = 1; i < n; i++) {
        int j; cin >> j; j--;
        g[j].pb(i);
    }
    dfs(0, -1);
    vll d;
    for(ll f = 1; f * f <= S; f++) if(S % f == 0) d.pb(f), d.pb(S / f);
    if(sz(d) > 1 && d[sz(d) - 2] == d[sz(d) - 1]) d.pop_back();
    sort(all(d));
    //for(ll x : d) cout << x << ' '; cout << endl;
    vi cnt(sz(d), 0);
    for(int j = 0; j < sz(d); j++) for(int i = 0; i < n; i++) cnt[j] += (s[i] % d[j] == 0);
    vll dp(sz(d));
    for(int j = 0; j < sz(d); j++) {
        //cout << cnt[j] << ' ';
        if(d[j] < hi) continue;
        dp[j] = cnk(cnt[j], S / d[j]);
        if(dp[j]) for(int k = 0; k < j; k++) if(d[j] % d[k] == 0) dp[j] = (dp[j] + dp[k]) % M;
    }
    //cout << endl;
    cout << dp[sz(d) - 1];
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define x first
#define y second
//#define int ll
#define NAME ""

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using ull = unsigned long long;

const int maxn = 1e6;
const int mod = 998244353;
const int M = 1e9 + 7;
const int inf = 1e9;
const ll inf64 = 1e18L;
const ld pi = acos((ld)-1.0);
const ld eps = (ld)1e-6;

int n, a[maxn];
ll s[maxn], S;
vi g[maxn];

void dfs(int v, int p) {
    s[v] = a[v];
    for(int to : g[v]) if(to != p) dfs(to, v), s[v] += s[to];
}

ll pw(ll a, ll b) {
    if(!b) return 1;
    if(b & 1) return a * pw(a, b - 1) % M;
    a = pw(a, b >> 1);
    return a * a % M;
}

ll fac[maxn + 1];

inline ll cnk(ll n, ll k) {
    if(n < k) return 0;
    return fac[n] * pw((fac[k] * fac[n - k]) % M, M - 2) % M;
}

main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen(NAME".in", "r", stdin); freopen(NAME".out", "w", stdout);
    //freopen("in.txt", "r", stdin);
    fac[0] = 1;
    for(int i = 0; i < maxn; i++) fac[i + 1] = fac[i] * (i + 1) % M;

    int hi = 0;
    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], S += a[i], hi = max(hi, a[i]);
    for(int i = 1; i < n; i++) {
        int j; cin >> j; j--;
        g[j].pb(i);
    }
    dfs(0, -1);
    vll d;
    for(ll f = 1; f * f <= S; f++) if(S % f == 0) d.pb(f), d.pb(S / f);
    if(sz(d) > 1 && d[sz(d) - 2] == d[sz(d) - 1]) d.pop_back();
    sort(all(d));
    //for(ll x : d) cout << x << ' '; cout << endl;
    vi cnt(sz(d), 0);
    vll als(n);
    for(int i = 0; i < n; i++) als[i] = s[i];
    sort(all(als));
    reverse(all(als));
    for(int j = 0; j < sz(d); j++) if(hi <= d[j]) for(int i = 0; i < n && als[i] >= d[j]; i++) cnt[j] += (als[i] % d[j] == 0);
    vll dp(sz(d));
    for(int j = 0; j < sz(d); j++) {
        //cout << cnt[j] << ' ';
        if(d[j] < hi) continue;
        dp[j] = cnk(cnt[j], S / d[j]);
        if(dp[j]) for(int k = 0; k < j; k++) if(d[j] % d[k] == 0) dp[j] = (dp[j] + dp[k]) % M;
    }
    //cout << endl;
    cout << dp[sz(d) - 1];
}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define x first
#define y second
//#define int ll
#define NAME ""

using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using ull = unsigned long long;

const int maxn = 1e6;
const int mod = 998244353;
const int M = 1e9 + 7;
const int inf = 1e9;
const ll inf64 = 1e18L;
const ld pi = acos((ld)-1.0);
const ld eps = (ld)1e-6;

int n, a[maxn];
ll s[maxn], S;
vi g[maxn];

void dfs(int v, int p) {
    s[v] = a[v];
    for(int to : g[v]) if(to != p) dfs(to, v), s[v] += s[to];
}

ll pw(ll a, ll b) {
    if(!b) return 1;
    if(b & 1) return a * pw(a, b - 1) % M;
    a = pw(a, b >> 1);
    return a * a % M;
}

ll fac[maxn + 1];

ll cnk(ll n, ll k) {
    return fac[n] * pw(fac[k] * fac[n - k], M - 2) % M;
}

main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    //freopen(NAME".in", "r", stdin); freopen(NAME".out", "w", stdout);
    //freopen("in.txt", "r", stdin);
    fac[0] = 1;
    for(int i = 0; i < maxn; i++) fac[i + 1] = fac[i] * (i + 1) % M;

    cin >> n;
    for(int i = 0; i < n; i++) cin >> a[i], S += a[i];
    for(int i = 1; i < n; i++) {
        int j; cin >> j; j--;
        g[j].pb(i);
    }
    dfs(0, -1);
    vll d;
    for(ll f = 1; f * f <= S; f++) if(S % f == 0) d.pb(f), d.pb(S / f);
    if(sz(d) > 1 && d[sz(d) - 2] == d[sz(d) - 1]) d.pop_back();
    sort(all(d));
    //for(ll x : d) cout << x << ' '; cout << endl;
    vi cnt(sz(d), 0);
    for(int j = 0; j < sz(d); j++) for(int i = 0; i < n; i++) cnt[j] += (s[i] % d[j] == 0);
    vll dp(sz(d));
    for(int j = 0; j < sz(d); j++) {
        //cout << cnt[j] << ' ';
        dp[j] = cnk(cnt[j], S / d[j]);
        if(dp[j]) for(int k = 0; k < j; k++) if(d[j] % d[k] == 0) dp[j] = (dp[j] + dp[k]) % M;
    }
    //cout << endl;
    cout << dp[sz(d) - 1];
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <iomanip>
using namespace std;

const long long MOD = (long long) 1e9 + 7;

template<typename T>
T gcd(T a, T b) {
  while (b > 0) {
    swap(a, b);
    b %= a;
  }
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> p(n);
  for (int i = 1; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = n - 1; i >= 1; i--) {
    a[p[i]] += a[i];
  }
  vector<int> cnt(n + 1, 0);
  for (int i = 0; i < n; i++) {
    int tmp = (int)(a[0] / gcd(a[0], a[i]));
    if (tmp <= n) {
      cnt[tmp]++;
    }
  }
  vector<int> v(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      v[j] += cnt[i];
    }
  }
  vector<long long> dp(n + 1);
  long long ans = 0ll;
  dp[1] = 1ll;
  for (int i = 1; i <= n; i++) {
    if (v[i] != i) {
      continue;
    }
    ans = (ans + dp[i]) % MOD;
    for (int j = i + i; j <= n; j += i) {
      dp[j] = (dp[j] + dp[i]) % MOD;
    }
  }
  cout << ans << '\n';
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <iomanip>
using namespace std;

const long long MOD = (long long) 1e9 + 7;

template<typename T>
T gcd(T a, T b) {
  if (b > a) {
    swap(a, b);
  }
  while (b > 0) {
    swap(a, b);
    b %= a;
  }
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> p(n);
  for (int i = 1; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = n - 1; i >= 1; i--) {
    a[p[i]] += a[i];
  }
  vector<int> cnt(n + 1, 0);
  for (int i = 0; i < n; i++) {
    int tmp = (int)(a[0] / gcd(a[0], a[i]));
    if (tmp <= n) {
      cnt[tmp]++;
    }
  }
  vector<int> v(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      v[j] += cnt[i];
    }
  }
  vector<long long> dp(n + 1, 0ll);
  long long ans = 0ll;
  dp[1] = 1ll;
  for (int i = 1; i <= n; i++) {
    if (v[i] < i) {
      continue;
    }
    ans = (ans + dp[i]) % MOD;
    for (int j = i * 2; j <= n; j += i) {
      dp[j] = (dp[j] + dp[i]) % MOD;
    }
  }
  cout << ans << '\n';
  return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <iomanip>
using namespace std;

const long long MOD = (long long) 1e9 + 7;

template<typename T>
T gcd(T a, T b) {
  if (b > a) {
    swap(a, b);
  }
  while (b > 0) {
    swap(a, b);
    b %= a;
  }
  return a;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<int> p(n);
  for (int i = 1; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = n - 1; i >= 1; i--) {
    a[p[i]] += a[i];
  }
  vector<int> cnt(n + 1, 0);
  for (int i = 0; i < n; i++) {
    long long tmp = a[0] / gcd(a[0], a[i]);
    if (tmp <= n) {
      cnt[tmp]++;
    }
  }
  vector<int> v(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) {
      v[j] += cnt[i];
    }
  }
  vector<long long> dp(n + 1, 0ll);
  long long ans = 0ll;
  dp[1] = 1ll;
  for (int i = 1; i <= n; i++) {
    if (v[i] < i) {
      continue;
    }
    ans = (ans + dp[i]) % MOD;
    for (int j = i * 2; j <= n; j += i) {
      dp[j] = (dp[j] + dp[i]) % MOD;
    }
  }
  cout << ans << '\n';
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1000005,P=1e9+7;
int n,m,a[maxn],fa[maxn],cnt[maxn],f[maxn],ans;
int gcd(int a,int b){ return b?gcd(b,a%b):a; }
int main(){
  scanf("%d",&n);
  for(int i=1;i<=n;i++) scanf("%d",&a[i]);
  for(int i=2;i<=n;i++) scanf("%d",&fa[i]);
  for(int i=n;i>=2;i--) a[fa[i]]+=a[i];
  for(int i=1;i<=n;i++){
    int t=a[1]/gcd(a[i],a[1]);
    if(t<=n) cnt[t]++;
  }
  for(int i=n;i>=1;i--)
    for(int j=i+i;j<=n;j+=i) cnt[j]+=cnt[i];
  f[1]=1;
  for(int i=1;i<=n;i++)
    if(cnt[i]==i){
      (ans+=f[i])%P;
      for(int j=i+i;j<=n;j+=i) (f[j]+=f[i])%=P;
    }
  printf("%d\n",ans);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1000005,P=1e9+7;
int n,m,fa[maxn],f[maxn],ans;
LL a[maxn],cnt[maxn];
LL gcd(LL a,LL b){ return b?gcd(b,a%b):a; }
int main(){
  scanf("%d",&n);
  for(int i=1;i<=n;i++) scanf("%d",&a[i]);
  for(int i=2;i<=n;i++) scanf("%d",&fa[i]);
  for(int i=n;i>=2;i--) a[fa[i]]+=a[i];
  for(int i=1;i<=n;i++){
    LL t=a[1]/gcd(a[i],a[1]);
    if(t<=n) cnt[t]++;
  }
  for(int i=n;i>=1;i--)
    for(int j=i+i;j<=n;j+=i) cnt[j]+=cnt[i];
  f[1]=1;
  for(int i=1;i<=n;i++)
    if(cnt[i]==i){
      (ans+=f[i])%P;
      for(int j=i+i;j<=n;j+=i) (f[j]+=f[i])%=P;
    }
  printf("%d\n",ans);
  return 0;
}


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

using ll = long long;
const int MAXN = 1.1e6;
int N;
ll A[MAXN];
int P[MAXN];
int C[MAXN];
int T[MAXN];
ll dp[MAXN];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 1; i < N; i++) cin >> P[i];
  for (int i = N-1; i > 0; i--) A[P[i]-1] += A[i];
  for (int i = 0; i < N; i++) {
    ll d = A[0]/__gcd(A[0], A[i]);
    if (d <= N) C[d] ++;
  }
  dp[1]++;
  for (int d = 1; d <= N; d++) {
    for (int i = d; i <= N; i += d) T[i] += C[d];
    if (T[d]==d) {
      ll v = dp[d]%(ll(1e9)+7);
      for (int i = 0; i <= N; i += d) dp[i] += v;
    }
  }
  cout << dp[0] << '\n';
}


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

using ll = long long;
const ll MOD = ll(1e9)+7;

const int MAXN = 1.1e6;
int N;
ll A[MAXN];
int P[MAXN];
int C[MAXN];
int T[MAXN];
int dp[MAXN];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 1; i < N; i++) cin >> P[i], P[i] --;
  for (int i = N-1; i > 0; i--) {
    A[P[i]] += A[i];
  }
  for (int i = 0; i < N; i++) {
    ll d = A[0]/__gcd(A[0], A[i]);
    if (d <= N) C[d] ++;
  }
  ll res = 0;
  dp[1]++;
  for (int d = 1; d <= N; d++) {
    for (int i = d; i <= N; i += d) T[i] += C[d];
    if (T[d]!=d) continue;
    dp[d]%=MOD;
    res+=dp[d];
    for (int i = d+d; i <= N; i += d) dp[i] += dp[d];
  }
  cout << res%MOD << '\n';
}


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

using ll = long long;
const ll MOD = ll(1e9)+7;

const int MAXN = 1.1e6;
int N;
ll A[MAXN];
int P[MAXN];
int C[MAXN];
int dp[MAXN];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 1; i < N; i++) cin >> P[i], P[i] --;
  for (int i = N-1; i > 0; i--) {
    A[P[i]] += A[i];
  }
  for (int i = 0; i < N; i++) {
    ll d = A[0]/__gcd(A[0], A[i]);
    if (d <= N) C[d] ++;
  }
  ll res = 0;
  dp[1]++;
  for (int d = 1; d <= N; d++) {
    for (int i = d+d; i <= N; i += d) C[i] += C[d];
    if (C[d]!=d) continue;
    dp[d]%=MOD;
    res+=dp[d];
    for (int i = d+d; i <= N; i += d) dp[i] += dp[d];
  }
  cout << res%MOD << '\n';
}


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

using ll = long long;
const ll MOD = ll(1e9)+7;

const int MAXN = 1.1e6;
int N;
ll A[MAXN];
int P[MAXN];
int C[MAXN];
int dp[MAXN];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i], tot += A[i];
  for (int i = 1; i < N; i++) cin >> P[i], P[i] --;
  for (int i = N-1; i >= 0; i--) {
    A[P[i]] += A[i];
  }
  for (int i = 0; i < N; i++) {
    ll d = A[0]/__gcd(A[0], A[i]);
    if (d <= N) C[d] ++;
  }
  ll res = 0;
  dp[1]++;
  for (int d = 1; d <= N; d++) {
    for (int i = d+d; i <= N; i += d) C[i] += C[d];
    if (C[d]!=d) continue;
    dp[d]%=MOD;
    res+=dp[d];
    for (int i = d+d; i <= N; i += d) dp[i] += dp[d];
  }
  cout << res%MOD << '\n';
}


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

using ll = long long;
const ll MOD = ll(1e9)+7;

const int MAXN = 1.1e6;
int N;
ll A[MAXN];
int P[MAXN];
int C[MAXN];
int dp[MAXN];

int main() {
  ios_base::sync_with_stdio(0);
  cin >> N;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 1; i < N; i++) cin >> P[i], P[i] --;
  for (int i = N-1; i >= 0; i--) {
    A[P[i]] += A[i];
  }
  for (int i = 0; i < N; i++) {
    ll d = A[0]/__gcd(A[0], A[i]);
    if (d <= N) C[d] ++;
  }
  ll res = 0;
  dp[1]++;
  for (int d = 1; d <= N; d++) {
    for (int i = d+d; i <= N; i += d) C[i] += C[d];
    if (C[d]!=d) continue;
    dp[d]%=MOD;
    res+=dp[d];
    for (int i = d+d; i <= N; i += d) dp[i] += dp[d];
  }
  cout << res%MOD << '\n';
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1000005;
int n,a[maxn],S,hsh[maxn],s[maxn],d[maxn],ans,pd[maxn];
int lnk[maxn],son[maxn],nxt[maxn],tot;
int gcd(int x,int y){if (y==0) return x; return gcd(y,x%y);}
void adde(int x,int y){son[++tot]=y,nxt[tot]=lnk[x],lnk[x]=tot;}
void dfs(int x,int fa){
    s[x]=1ll*a[x];
    for (int j=lnk[x];j;j=nxt[j])
        if (son[j]!=fa) {
            dfs(son[j],x);
            s[x]+=s[son[j]];
        }
}
signed main(){
    scanf("%lld",&n);
    for (int i=1;i<=n;i++) scanf("%lld",&a[i]),S+=a[i];
    for (int i=1;i<n;i++){
        int x; scanf("%lld",&x);
        adde(x,i+1);
    }
    dfs(1,0);
    //for (int i=1;i<=n;i++) printf("!%lld ",S/gcd(s[i],S)); puts("");
    for (int i=1;i<=n;i++) hsh[S/gcd(s[i],S)]++;
    //for (int i=1;i<=n;i++) printf("!%lld ",hsh[i]); puts("");
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n/i;j++)
            d[i*j]+=hsh[i];
    for (int i=1;i<=n;i++) if (d[i]==i) pd[i]=1,d[i]=1; else d[i]=0;
    for (int i=2;i<=n;i++) if (S%i==0) d[i]=0;
    //for (int i=1;i<=n;i++) printf("!!%lld ",pd[i]); puts("");
    for (int i=1;i<=n;i++) if (pd[i]){
            for (int j=2;j<=n/i;j++) if (pd[i*j]) d[i*j]+=d[i];
        ans+=d[i];
    }
    //for (int i=1;i<=n;i++) printf("FF%lld ",d[i]); puts("");
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1000005;
int n,a[maxn],S,hsh[maxn],s[maxn],d[maxn],ans,pd[maxn];
int lnk[maxn],son[maxn],nxt[maxn],tot;
int gcd(int x,int y){if (y==0) return x; return gcd(y,x%y);}
void adde(int x,int y){son[++tot]=y,nxt[tot]=lnk[x],lnk[x]=tot;}
void dfs(int x,int fa){
    s[x]=1ll*a[x];
    for (int j=lnk[x];j;j=nxt[j])
        if (son[j]!=fa) {
            dfs(son[j],x);
            s[x]+=s[son[j]];
        }
}
signed main(){
    scanf("%lld",&n);
    for (int i=1;i<=n;i++) scanf("%lld",&a[i]),S+=a[i];
    for (int i=1;i<n;i++){
        int x; scanf("%lld",&x);
        adde(x,i+1);
    }
    dfs(1,0);
    //for (int i=1;i<=n;i++) printf("!%lld ",S/gcd(s[i],S)); puts("");
    for (int i=1;i<=n;i++) if (S/gcd(s[i],S)<=n) hsh[S/gcd(s[i],S)]++;
    //for (int i=1;i<=n;i++) printf("!%lld ",hsh[i]); puts("");
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n/i;j++)
            d[i*j]+=hsh[i];
    for (int i=1;i<=n;i++) if (d[i]==i) pd[i]=1,d[i]=1; else d[i]=0;
    for (int i=2;i<=n;i++) if (S%i==0) d[i]=0;
    //for (int i=1;i<=n;i++) printf("!!%lld ",pd[i]); puts("");
    for (int i=1;i<=n;i++) if (pd[i]){
            for (int j=2;j<=n/i;j++) if (pd[i*j]) d[i*j]+=d[i];
        ans+=d[i];
    }
    //for (int i=1;i<=n;i++) printf("FF%lld ",d[i]); puts("");
    printf("%lld\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1000005, mod = 1000000007;
int kcnt[maxn], A[maxn], N;
LL S[maxn], Sum;
vector<int> adj[maxn];
LL gcd(LL a, LL b)
{
	return !a ? b : gcd(b % a, a);
}

void init()
{
	scanf("%d", &N);
	for(int i = 1;i <= N;++i) scanf("%d", &A[i]), Sum += A[i];
	int p;
	for(int i = 1;i < N;++i)
	{
		scanf("%d", &p);
		adj[p].push_back(i+1);
	}
}

void dfs(int u)
{
	S[u] = A[u];
	LL k;
	for(auto it : adj[u])
	{
		dfs(it);
		S[u] += S[it];
	}
	k = Sum / gcd(Sum, S[u]);
	if(k <= N) ++kcnt[k];
}

void work()
{
	dfs(1);
	for(int i = N;i >= 1;--i) if(kcnt[i])
	{
		for(int j = i + i;j <= N;j += i)
			kcnt[j] += kcnt[i];
	}
	//for(int i = 1;i <= N;++i) printf("%d ", kcnt[i]);puts("");
	for(int i = N;i >= 1;--i)
	{
		if(kcnt[i] == i)
		{
			for(int j = i + i;j <= N;j += i)
				A[i] = (A[i] + A[j]) % mod;
		}
		else A[i] = 0;
	}
	printf("%d\n", A[1]);
}

int main()
{
	init();
	work();
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1000005, mod = 1000000007;
int kcnt[maxn], A[maxn], N;
LL S[maxn], Sum;
vector<int> adj[maxn];
LL gcd(LL a, LL b)
{
	return !a ? b : gcd(b % a, a);
}

void init()
{
	scanf("%d", &N);
	for(int i = 1;i <= N;++i) scanf("%d", &A[i]), Sum += A[i];
	int p;
	for(int i = 1;i < N;++i)
	{
		scanf("%d", &p);
		adj[p].push_back(i+1);
	}
}

void dfs(int u)
{
	S[u] = A[u];
	LL k;
	for(auto it : adj[u])
	{
		dfs(it);
		S[u] += S[it];
	}
	k = Sum / gcd(Sum, S[u]);
	if(k <= N) ++kcnt[k];
}

void work()
{
	dfs(1);
	for(int i = N;i >= 1;--i) if(kcnt[i])
	{
		for(int j = i + i;j <= N;j += i)
			kcnt[j] += kcnt[i];
	}
	//for(int i = 1;i <= N;++i) printf("%d ", kcnt[i]);puts("");
	for(int i = N;i >= 1;--i)
	{
		A[i] = 0;
		if(kcnt[i] == i)
		{
			++A[i];
			for(int j = i + i;j <= N;j += i)
				A[i] = (A[i] + A[j]) % mod;
		}
	}
	printf("%d\n", A[1]);
}

int main()
{
	init();
	work();
	return 0;
}


#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using ll=long long;

const pid_t pid = getpid();
// Problem Specific Parameter:
const int limit = 2000010;
vector<int> graph[limit];
ll a[limit],sa[limit];

ll dfs(int v){
	sa[v] = a[v];
	for(auto &v2:graph[v]){
		dfs(v2);
		sa[v] += sa[v2];
	}
	return sa[v];
}

ll comp[limit];
bool ok[limit];

template<unsigned MOD>  class ModInt {
public:
	ModInt(): x(0) {}
	ModInt(signed y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}
	ModInt(signed long long y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}

	// Arithmetic Oprators
	ModInt &operator+=(ModInt that) {
		if ((x += that.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt &operator-=(ModInt that) {
		if ((x += MOD - that.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt &operator*=(ModInt that) {
		x = 1LL * x * that.x % MOD;
		return *this;
	}
	ModInt &operator%=(ModInt that) {
		x %= that.x;
		return *this;
	}

	ModInt &operator+=(const int that) { return *this += ModInt(that);}
	ModInt &operator-=(const int that) { return *this -= ModInt(that);}
	ModInt &operator*=(const int that) { return *this *= ModInt(that);}
	ModInt &operator%=(const int that) { return *this %= ModInt(that);}

	// Comparators
	bool operator <(ModInt that) { return x < that.x; }
	bool operator >(ModInt that) { return x > that.x; }
	bool operator<=(ModInt that) { return x <= that.x; }
	bool operator>=(ModInt that) { return x >= that.x; }
	bool operator!=(ModInt that) { return x != that.x; }
	bool operator==(ModInt that) { return x == that.x; }

	// Utilities
	unsigned getval() const { return x;}
	ModInt operator+(ModInt that) const { return ModInt(*this) += that;}
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that;}
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that;}
	ModInt operator%(ModInt that) const { return ModInt(*this) %= that;}
	ModInt operator+(const int that) const { return ModInt(*this) += that;}
	ModInt operator-(const int that) const { return ModInt(*this) -= that;}
	ModInt operator*(const int that) const { return ModInt(*this) *= that;}
	ModInt operator%(const int that) const { return ModInt(*this) %= that;}
	ModInt operator=(const int that) { return *this = ModInt(that);}
	friend istream &operator>>(istream& is, ModInt &that) {ll tmp; is >> tmp; that = ModInt(tmp);return is;}
	friend ostream &operator<<(ostream& os, const ModInt &that) { return os << that.x; }

private:
	unsigned x;
};

const int mod = 1000000007;
using mint = ModInt<mod>;
const mint ZERO = mint(0);
const mint ONE = mint(1);

mint memo[limit];

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

	int n;
	cin >> n;
	rep(i,n) cin >> a[i];

	rep(i,1,n){
		int p;
		cin >> p;
		graph[p-1].push_back(i);
	}

	dfs(0);

	rep(i,1,n){
		const ll g = __gcd(sa[i], sa[0] - sa[i]);
		if(g == 0 or sa[0] % g != 0) continue;
		const ll base = sa[0] / g;
		if(base <= n) comp[base]++;
	}

	rrep(i,n+1,1){
		if(comp[i] == 0) continue;
		for(int j = 2 * i; j <= n; j += i){
			if(sa[0] % j != 0) continue;
			comp[j] += comp[i];
		}
	}

	rep(i,1,n+1){
		ok[i] = (comp[i] == (i - 1));
	}

	memo[1] = ONE;

	rep(i,1,n+1){
		if(ok[i] == false) continue;
		for(int j = 2 * i; j <= n; j += i){
			if(ok[j] == false) continue;
			memo[j] += memo[i];
		}
	}

	mint ans = ZERO;
	rep(i,1,n+1) ans += memo[i];
	cout << ans.getval() << endl;

	return 0;
}


#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using ll=long long;
using R=long double;
const R EPS=1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max(x,0.0L));}

const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};

const pid_t pid = getpid();
// Problem Specific Parameter:
const int limit = 2000010;
vector<int> graph[limit];
ll a[limit],sa[limit];

ll dfs(int v){
	sa[v] = a[v];
	for(auto &v2:graph[v]){
		sa[v] += dfs(v2);
	}
	return sa[v];
}

ll comp[limit];
bool ok[limit];

template<unsigned MOD>  class ModInt {
public:
	ModInt(): x(0) {}
	ModInt(signed y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}
	ModInt(signed long long y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}

	// Arithmetic Oprators
	ModInt &operator+=(ModInt that) {
		if ((x += that.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt &operator-=(ModInt that) {
		if ((x += MOD - that.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt &operator*=(ModInt that) {
		x = 1LL * x * that.x % MOD;
		return *this;
	}
	ModInt &operator%=(ModInt that) {
		x %= that.x;
		return *this;
	}

	ModInt &operator+=(const int that) { return *this += ModInt(that);}
	ModInt &operator-=(const int that) { return *this -= ModInt(that);}
	ModInt &operator*=(const int that) { return *this *= ModInt(that);}
	ModInt &operator%=(const int that) { return *this %= ModInt(that);}

	// Comparators
	bool operator <(ModInt that) { return x < that.x; }
	bool operator >(ModInt that) { return x > that.x; }
	bool operator<=(ModInt that) { return x <= that.x; }
	bool operator>=(ModInt that) { return x >= that.x; }
	bool operator!=(ModInt that) { return x != that.x; }
	bool operator==(ModInt that) { return x == that.x; }

	// Utilities
	unsigned getval() const { return x;}
	ModInt operator+(ModInt that) const { return ModInt(*this) += that;}
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that;}
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that;}
	ModInt operator%(ModInt that) const { return ModInt(*this) %= that;}
	ModInt operator+(const int that) const { return ModInt(*this) += that;}
	ModInt operator-(const int that) const { return ModInt(*this) -= that;}
	ModInt operator*(const int that) const { return ModInt(*this) *= that;}
	ModInt operator%(const int that) const { return ModInt(*this) %= that;}
	ModInt operator=(const int that) { return *this = ModInt(that);}
	friend istream &operator>>(istream& is, ModInt &that) {ll tmp; is >> tmp; that = ModInt(tmp);return is;}
	friend ostream &operator<<(ostream& os, const ModInt &that) { return os << that.x; }

private:
	unsigned x;
};

const int mod = 1000000007;
using mint = ModInt<mod>;
const mint ZERO = mint(0);
const mint ONE = mint(1);

mint memo[limit];

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

	int n;
	cin >> n;
	rep(i,n) cin >> a[i];

	rep(i,1,n){
		int p;
		cin >> p;
		graph[p-1].push_back(i);
	}

	dfs(0);

	rep(i,1,n){
		const ll g = __gcd(sa[i],sa[0] - sa[i]);
		if(sa[0] % g != 0) continue;
		const ll base = sa[0] / g;
		comp[base]++;
	}

	rrep(i,n+1,1){
		if(comp[i] == 0) continue;
		for(int j = 2 * i; j <= n; j += i){
			if(sa[0] % j != 0) continue;
			comp[j] += comp[i];
		}
	}

	rep(i,1,n+1){
		ok[i] = (comp[i] == (i - 1));
	}

	memo[1] = ONE;

	rep(i,1,n+1){
		if(ok[i] == false) continue;
		for(int j = 2 * i; j <= n; j += i){
			if(ok[j] == false) continue;
			memo[j] += memo[i];
		}
	}

	mint ans = ZERO;
	rep(i,1,n+1) ans += memo[i];
	cout << ans.getval() << endl;

	return 0;
}


#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

using namespace std;

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using ll=long long;
using R=long double;
const R EPS=1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r){return(r > EPS)-(r < -EPS);}
inline R sq(R x){return sqrt(max(x,0.0L));}

const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};

const pid_t pid = getpid();
// Problem Specific Parameter:
const int limit = 1000010;
vector<int> graph[limit];
ll a[limit],sa[limit];

ll dfs(int v){
	sa[v] = a[v];
	for(auto &v2:graph[v]){
		sa[v] += dfs(v2);
	}
	return sa[v];
}

int comp[limit];
bool ok[limit];

template<unsigned MOD>  class ModInt {
public:
	ModInt(): x(0) {}
	ModInt(signed y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}
	ModInt(signed long long y) : x(y >= 0 ? y % MOD : MOD - (-y) % MOD) {}

	// Arithmetic Oprators
	ModInt &operator+=(ModInt that) {
		if ((x += that.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt &operator-=(ModInt that) {
		if ((x += MOD - that.x) >= MOD) x -= MOD;
		return *this;
	}
	ModInt &operator*=(ModInt that) {
		x = 1LL * x * that.x % MOD;
		return *this;
	}
	ModInt &operator%=(ModInt that) {
		x %= that.x;
		return *this;
	}

	ModInt &operator+=(const int that) { return *this += ModInt(that);}
	ModInt &operator-=(const int that) { return *this -= ModInt(that);}
	ModInt &operator*=(const int that) { return *this *= ModInt(that);}
	ModInt &operator%=(const int that) { return *this %= ModInt(that);}

	// Comparators
	bool operator <(ModInt that) { return x < that.x; }
	bool operator >(ModInt that) { return x > that.x; }
	bool operator<=(ModInt that) { return x <= that.x; }
	bool operator>=(ModInt that) { return x >= that.x; }
	bool operator!=(ModInt that) { return x != that.x; }
	bool operator==(ModInt that) { return x == that.x; }

	// Utilities
	unsigned getval() const { return x;}
	ModInt operator+(ModInt that) const { return ModInt(*this) += that;}
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that;}
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that;}
	ModInt operator%(ModInt that) const { return ModInt(*this) %= that;}
	ModInt operator+(const int that) const { return ModInt(*this) += that;}
	ModInt operator-(const int that) const { return ModInt(*this) -= that;}
	ModInt operator*(const int that) const { return ModInt(*this) *= that;}
	ModInt operator%(const int that) const { return ModInt(*this) %= that;}
	ModInt operator=(const int that) { return *this = ModInt(that);}
	friend istream &operator>>(istream& is, ModInt &that) {ll tmp; is >> tmp; that = ModInt(tmp);return is;}
	friend ostream &operator<<(ostream& os, const ModInt &that) { return os << that.x; }

private:
	unsigned x;
};

const int mod = 1000000007;
using mint = ModInt<mod>;
const mint ZERO = mint(0);
const mint ONE = mint(1);

mint memo[limit];

int main(void){
	int n;
	scanf("%d",&n);
	rep(i,n) scanf("%lld",a+i);

	rep(i,1,n){
		int p;
		scanf("%d",&p);
		graph[p-1].push_back(i);
	}

	dfs(0);

	rep(i,1,n){
		const ll g = __gcd(sa[i],sa[0] - sa[i]);
		if(sa[0] % g != 0) continue;
		const ll base = sa[0] / g;
		comp[base]++;
	}

	rrep(i,n+1,2){
		if(comp[i] == 0) continue;
		for(int j = 2 * i; j <= n; j += i){
			if(sa[0] % j != 0) continue;
			comp[j] += comp[i];
		}
	}

	rep(i,1,n+1){
		ok[i] = (comp[i] == (i - 1));
	}

	memo[1] = ONE;

	rep(i,1,n+1){
		if(ok[i] == false) continue;
		for(int j = 2 * i; j <= n; j += i){
			if(ok[j] == false) continue;
			memo[j] += memo[i];
		}
	}

	mint ans = ZERO;
	rep(i,1,n+1) ans += memo[i];
	printf("%d\n",ans.getval());

	return 0;
}


#include <bits/stdc++.h>
#define FOR(i, a, b)	for (int (i)=(a); (i)<(b); (i)++)
#define PPC(x)			__builtin_popcountll((x))
#define ALL(x)			(x).begin(), (x).end()
#define pb				push_back
using namespace std;

const int maxN = 1000001, mod = 1000000007;

int dp[maxN], dp2[maxN];
long long s[maxN];
vector <int> G[maxN];

long long dfs(int v, int fath)
{
	for (int u : G[v])
		if (u != fath)
			s[v] += dfs(u, v);
	return s[v];
}

inline void add_mod(int& a, int b)	{   a = (a + b) % mod;  }

int main()
{
	int n;
	scanf ("%d", &n);
	FOR(i, 1, n+1)
		scanf ("%lld", s+i);
	FOR(i, 2, n+1)
	{
		int a;
		scanf ("%d", &a);
		G[a].pb(i);
		G[i].pb(a);
	}
	dfs(1, 0);
	FOR(v, 1, n+1)
	{
		s[v] = __gcd(s[v], s[1]);
		long long i = s[1] / s[v];
		if (i > n)
			continue;
		for (int j=i; j<=n; j+=i)
			dp[j]++;
	}
	int res = 0;
	dp2[1] = 1;
	FOR(i, 1, n+1)
	{
		if (dp[i] != i)
			continue;
		add_mod(res, dp2[i]);
		for (int j=i*2; j<=n; j+=i)
			add_mod(dp2[j], dp2[i]);
	}
	printf("%d\n", res);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
typedef long long ll;
template<typename T>inline bool chkmin(T &x,T y){return (y<x)?(x=y,1):0;}
template<typename T>inline bool chkmax(T &x,T y){return (y>x)?(x=y,1):0;}
inline int read(){
	int x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
	return x*f;
}
inline ll readll(){
	ll x;
	char c;
	int f=1;
	while((c=getchar())!='-' && (c>'9' || c<'0'));
	if(c=='-') f=-1,c=getchar();
	x=c^'0';
	while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
	return x*f;
}
const int maxn=1e6+10,mod=1e9+7;
int sum[maxn],Sum[maxn],Begin[maxn],Next[maxn<<1],to[maxn<<1],e;
ll a[maxn],S;
inline void add(int &x,int y){
	x+=y;
	x=(x>=mod?x-mod:x);
}
inline void add_edge(int x,int y){
	to[++e]=y;
	Next[e]=Begin[x];
	Begin[x]=e;
}
void dfs(int x,int ff){
	for(int i=Begin[x];i;i=Next[i]) if(to[i]!=ff){
		dfs(to[i],x);
		a[x]+=a[to[i]];
	}
}
int main(){
#ifndef ONLINE_JUDGE
	freopen("c.in","r",stdin);
	freopen("c.out","w",stdout);
#endif
	int n=read(),lim=1e6;
	REP(i,1,n) a[i]=read(),S+=a[i];
	REP(i,2,n){
		int x=read();
		add_edge(x,i);
	}
	dfs(1,0);
	REP(i,1,n){
		a[i]=S/__gcd(a[i],S);
		if(a[i]<=lim) sum[a[i]]++;
	}
	REP(i,1,lim) REP(j,1,lim/i) Sum[i*j]+=sum[i];
	REP(i,1,lim) sum[i]=(Sum[i]==i && S%i==0);
	REP(i,1,lim) Sum[i]=0;
	assert(sum[1]==1);
	Sum[1]=sum[1];
	REP(i,1,lim) REP(j,2,lim/i) if(sum[i*j]) add(Sum[i*j],Sum[i]);
	ll ans=0;
	REP(i,1,lim) ans+=Sum[i];
	printf("%lld\n",ans);
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<cassert>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<utility>
#include<bitset>
#include<complex>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<stack>

using namespace std;

#define mem(a,b) memset(a,b,sizeof(a))
#define REP(i,a,b) for(int i=a; i<=b; ++i)
#define PER(i,a,b) for(int i=a; i>=b; --i)
#define MP make_pair
#define PB push_back
#define fi first
#define se second
typedef long long LL;
typedef pair<int,int> pii;

const int maxn = 1e6;
const int P = 1e9+7;

LL Gcd(LL a, LL b) { return b ? Gcd(b,a%b) : a; }
inline void Add(LL &a, LL b) { a+=b; if(a>=P) a-=P; }

int n, pi[maxn+5];
int f[maxn+5];
LL ai[maxn+5], cnt[maxn+5];

int main() {
    scanf("%d", &n);
    REP(i,1,n) scanf("%lld", ai+i);
    REP(i,2,n) scanf("%d", pi+i);
    PER(i,n,2) ai[pi[i]] += ai[i];
    REP(i,1,n) {
        LL v = ai[1] / Gcd(ai[1],ai[i]);
        if(v <= n) ++f[v];
    }
    PER(i,n,1) {
        for(int k = i+i; k <= n; k += i)
            f[k] += f[i];
    }
    LL ans = 0;
    cnt[1] = 1;
    for(int i = 1; i <= n; ++i) {
        if(f[i] != i) continue;
        Add(ans, cnt[i]);
        for(int k = i+i; k <= n; k += i)
            Add(cnt[k], cnt[i]);
    }
    printf("%lld\n", ans);
    return 0;
}


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

const int MOD = 1000000007;
long long sum[1000010], k[1000010], f[1000010], F[1000010];
/*
	sum[i] = sum of the subtree i
	k[i] = largest denomiator of subtree i can use as proportion
	f[k] = number of subtrees of which the sum is 0 modulo (S[1]/k)
	F[k] =
*/
int p[1000010];

int main() {
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> sum[i];
	for (int i = 2; i <= n; i++) cin >> p[i];
	for (int i = n; i; i--) sum[p[i]] += sum[i];
	for (int i = n; i; i--) {
		k[i] = sum[1]/__gcd(sum[1],sum[i]);
		if (k[i] <= n) f[k[i]]++;
	}
	for (int i = n; i; i--)
		for (int j = i+i; j <= n; j+=i) f[j] += f[i];
	F[1] = 1;
	long long ans = 0;
	for (int i = 1; i <= n; i++) if (f[i] == i) {
		// the final level cut is possible,
		// which means it can be cut into exactly i parts
		int j = i;
		ans = (ans + F[j]) % MOD;
		while ((j+=i) <= n) F[j] = (F[j] + F[i]) % MOD;
		// this basic cut can be used for higher-level cut
	}
	cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifdef zerol
#define dbg(args...) do { cout << "DEBUG: " << #args << " -> "; err(args); } while (0)
#else
#define dbg(...)
#endif
#ifdef ONLINE_JUDGE
#define assert(condition) if (!(condition)) { int x = 1, y = 0; cout << x / y << endl; }
#endif
void err() { cout << "" << endl; }
template<template<typename...> class T, typename t, typename... Args>
void err(T<t> a, Args... args) { for (auto x: a) cout << x << ' '; err(args...); }
template<typename T, typename... Args>
void err(T a, Args... args) { cout << a << ' '; err(args...); }
// -----------------------------------------------------------------------------

const int N = 1E6 + 100, MOD = 1E9 + 7;
int n, p[N];
LL s[N], f[N], g[N];

int main() {
    cin >> n;
    FOR (i, 1, n + 1) scanf("%lld", s + i);
    FOR (i, 2, n + 1) scanf("%d", p + i);
    FORD (i, n, 1) s[p[i]] += s[i];
    FOR (i, 1, n + 1) {
        LL k = s[1] / __gcd(s[1], s[i]);
        dbg(i, k);
        if (k <= n) f[k]++;
    }
    FORD (i, n, 0) for (int j = i + i; j <= n; j += i)
        f[j] += f[i];
    LL ans = 0; g[1] = 1;
    FOR (i, 1, n + 1) {
        dbg(i, f[i], g[i]);
        if (f[i] != i) continue;
        (ans += g[i]) %= MOD;
        for (int j = i + i; j <= n; j += i)
            (g[j] += g[i]) %= MOD;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end());
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second

const int maxn = 1234567;
const int MOD = 1e9 + 7;

ll a[maxn], dp[maxn];
int p[maxn], cnt[maxn], n;

int main() {
    scanf("%d", &n);
    FOR(i, 1, n) scanf("%lld", &a[i]);
    FOR(i, 2, n) scanf("%d", &p[i]);
    ROF(i, n, 2) a[p[i]] += a[i];
    FOR(i, 1, n) {
        ll now = a[1] / __gcd(a[i], a[1]);
        if (now <= n) cnt[now]++;
    }
    ROF(i, n, 1) for (int j = 2 * i; j <= n; j += i) cnt[j] += cnt[i];
    ll ans = 0;
    dp[1] = 1;
    FOR(i, 1, n) {
        if (cnt[i] != i) continue;
        ans = (ans + dp[i]) % MOD;
        for (int j = 2 * i; j <= n; j += i) if (cnt[j] == j)
                dp[j] = (dp[i] + dp[j]) % MOD;
    }
    printf("%lld\n", ans);
    return 0;
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define popb pop_back()
#define sz size()
#define y1 dfsdsfsdfsd
#define next fdsfsdfjkl
#define int long long
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
double pi = acos(-1.0);
const int inf = 1e9+7;
const ll INF = 1e18+7;
/////////////////////////////////////////

int n;

int a[1000100];
int p[1000100];

vector<int> g[1000100];
int sub[1000100];

void dfs(int v,int pr)
{
    sub[v] = a[v];

    for(int i=0;i<g[v].sz;i++)
    {
        int t = g[v][i];

        if(pr!=t){
            dfs(t,v);
            sub[v] += sub[t];
        }
    }

}

int cnt_sub[1000100];
int cnt[1000100];

int dp[1000100];
main(){speed;
    cin>>n;

    for(int i=1;i<=n;i++) cin>>a[i];

    for(int i=1;i<=n-1;i++)
    {
        cin>>p[i];

        g[p[i]].pb(i+1);
        //g[i+1].pb(p[i]);
    }

    dfs(1,0);

    int sum = sub[1];
    for(int i=1;i<=n;i++)
    {
        int t = sum / __gcd(sum,sub[i]);

        cnt_sub[t]++;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j = i;j<=n;j+=i)
        {
            cnt[j] += cnt_sub[i];
        }
    }

    int ans = 0;

    for(int i=n;i>=1;i--)
    {
        if(cnt[i]<i) continue;

        if(cnt[i]==i)
        {
            //cout<<"ok"<<endl;
            dp[i] = 1;

            if(i>1)
                for(int j = i+i;j<=n;j+=i)
                {
                    dp[i] = (dp[j] + dp[i]) % inf;
                }

        }
        ans += dp[i]%inf;
        ans%=inf;
    }

    cout<<ans<<endl;
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define popb pop_back()
#define sz size()
#define y1 dfsdsfsdfsd
#define next fdsfsdfjkl
#define int long long
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
double pi = acos(-1.0);
const int inf = 1e9+7;
const ll INF = 1e18+7;
/////////////////////////////////////////

int n;

int a[1000100];
int p[1000100];

vector<int> g[1000100];
int sub[1000100];

void dfs(int v,int pr)
{
    sub[v] = a[v];

    for(int i=0;i<g[v].sz;i++)
    {
        int t = g[v][i];

        if(pr!=t){
            dfs(t,v);
            sub[v] += sub[t];
        }
    }

}

int cnt_sub[1000100];
int cnt[1000100];

int dp[1000100];
main(){speed;
    cin>>n;

    for(int i=1;i<=n;i++) cin>>a[i];

    for(int i=1;i<=n-1;i++)
    {
        cin>>p[i];

        g[p[i]].pb(i+1);
        //g[i+1].pb(p[i]);
    }

    dfs(1,0);

    int sum = sub[1];
    for(int i=1;i<=n;i++)
    {
        int t = sum / __gcd(sum,sub[i]);

        cnt_sub[t]++;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j = i;j<=n;j+=i)
        {
            cnt[j] += cnt_sub[i];
        }
    }

    int ans = 0;

    for(int i=n;i>=1;i--)
    {

        if(cnt[i]==i)
        {
            //cout<<"ok"<<endl;
            dp[i] = 1;

            if(i>1)
                for(int j = i+i;j<=n;j+=i)
                {
                    dp[i] = (dp[j] + dp[i]) % inf;
                }

        }
        ans += dp[i]%inf;
        ans%=inf;
    }

    cout<<ans<<endl;
}


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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define popb pop_back()
#define sz size()
#define y1 dfsdsfsdfsd
#define next fdsfsdfjkl
#define int long long
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
typedef unsigned long long ull;
double pi = acos(-1.0);
const int inf = 1e9+7;
const ll INF = 1e18+7;
/////////////////////////////////////////

int n;

int a[1000100];
int p[1000100];

vector<int> g[1000100];
int sub[1000100];

void dfs(int v,int pr)
{
    sub[v] = a[v];

    for(int i=0;i<g[v].sz;i++)
    {
        int t = g[v][i];

        if(pr!=t){
            dfs(t,v);
            sub[v] += sub[t];
        }
    }

}

int cnt_sub[1000100];
int cnt[1000100];

int dp[1000100];
main(){speed;
    cin>>n;

    for(int i=1;i<=n;i++) cin>>a[i];

    for(int i=1;i<=n-1;i++)
    {
        cin>>p[i];

        g[p[i]].pb(i+1);
        g[i+1].pb(p[i]);
    }

    dfs(1,0);

    int sum = sub[1];
    for(int i=1;i<=n;i++)
    {
        int t = sum / __gcd(sum,sub[i]);

        cnt_sub[t]++;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j = i;j<=n;j+=i)
        {
            cnt[j] += cnt_sub[i];
        }
    }

    int ans = 0;

    for(int i=n;i>=1;i--)
    {

        if(cnt[i]==i)
        {
            //cout<<"ok"<<endl;
            dp[i] = 1;

            if(i>1)
                for(int j = i+i;j<=n;j+=i)
                {
                    dp[i] = (dp[j] + dp[i]) % inf;
                }

        }
        ans += dp[i]%inf;
        ans%=inf;
    }

    cout<<ans<<endl;
}