#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<long long int>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vb vector<bool>
#define vvl vector<vector<ll> >
#define vvi vector<vector<int> >
#define pl pair<ll,ll>
#define pb push_back
#define PI 3.14159265
#define mod 998244353
#define pb push_back
#define mp make_pair
#define fri(s,n) for(int i=s;i<n;i++)
#define frj(s,n) for(int j=s;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define vsi vector<set<int> >
#define pii pair<int,int>
#define inf 1e18
#define vpii vector<pair<int,int> >

vvi graph;
vi incoming;
stack<int> ans;
void dfs(int node,vb &visited,stack<int> &s){
    s.push(node);
    visited[node]=true;
    for(auto nbr:graph[node]){
        if(!visited[nbr] && incoming[nbr]==1)
            dfs(nbr,visited,s);
    }
    if(s.size()>ans.size())
        ans=s;
    s.pop();
    return;
    }
int main(){
    int n;
    cin>>n;
    vi src;
    fri(1,n+1){
        int x;
        cin>>x;
        if(x==1)
            src.pb(i);}
    graph.resize(n+1);
    incoming.resize(n+1);
    fri(1,n+1){
        int x;
        cin>>x;
        if(x==0)
            continue;
        graph[i].pb(x);
        incoming[x]++;
        }
    for(node:src){
        vb visited(n+1);
        stack<int> s;
        dfs(node,visited,s);
    }
    cout<<ans.size()<<"\n";
    while(!ans.empty()){
        cout<<ans.top()<<" ";
        ans.pop();}
    }


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define vl vector<long long int>
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vb vector<bool>
#define vvl vector<vector<ll> >
#define vvi vector<vector<int> >
#define pl pair<ll,ll>
#define pb push_back
#define PI 3.14159265
#define mod 998244353
#define pb push_back
#define mp make_pair
#define fri(s,n) for(int i=s;i<n;i++)
#define frj(s,n) for(int j=s;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define vsi vector<set<int> >
#define pii pair<int,int>
#define inf 1e18
#define vpii vector<pair<int,int> >

vvi graph;
vi incoming;
stack<int> ans;
void dfs(int node,vb &visited,stack<int> &s){
    s.push(node);
    visited[node]=true;
    for(auto nbr:graph[node]){
        if(!visited[nbr] && incoming[nbr]==1)
            dfs(nbr,visited,s);
    }
    if(s.size()>ans.size())
        ans=s;
    s.pop();
    return;
    }
int main(){
    int n;
    cin>>n;
    vi src;
    fri(1,n+1){
        int x;
        cin>>x;
        if(x==1)
            src.pb(i);}
    graph.resize(n+1);
    incoming.resize(n+1);
    fri(1,n+1){
        int x;
        cin>>x;
        if(x==0)
            continue;
        graph[i].pb(x);
        incoming[x]++;
        }
    for(auto node:src){
        vb visited(n+1);
        stack<int> s;
        dfs(node,visited,s);
    }
    cout<<ans.size()<<"\n";
    while(!ans.empty()){
        cout<<ans.top()<<" ";
        ans.pop();}
    }


#include<bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define ll long long
#define maxheap priority_queue<int>
#define minheap priority_queue<int,vector<int>,greater<int>>
const   double eps = 1e-9;
const long double pi = acos(-1.0);
using namespace std;

int n;
const int N = 1e5 + 1;
int a[N], deg[N], pr[N];
bool used[N], del[N];
void solve(){
    cin >> n;

    for (int i = 1; i <= n; i++){
        cin >> a[i];
        used[i] = false;
        del[i] = false;
        deg[i] = 0;
        pr[i] = 0;
    }
    for (int i = 1; i <= n; i++){
        int x;
        cin >> x;
        if (x == 0) continue;
        deg[x]++;
        pr[i] = x;
        if (deg[x] > 1) del[x] = true;
    }

    for (int i = 1; i <= n; i++){
        if (del[pr[i]])
            pr[i] = 0;
    }
    int ans = 0;
    int ind = 0;
    for (int i = 1; i <= n; i++){
        if (!a[i]) continue;
        int x = i;
        int kol = 1;
        while (pr[x] != 0){
            x = pr[x];
            kol++;
        }
        if (ans < kol){
            ans = kol;
            ind = i;
        }
    }
    cout << ans << endl;
    vector <int> ansv;
    ansv.pb(ind);
    while (pr[ind] != 0){
        ind = pr[ind];
        ansv.pb(ind);
    }
    reverse(ansv.begin(), ansv.end());
    for (int x : ansv) cout << x << " ";

}
int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(10);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int step;
    step = 1;
    //cin >> step;
    for (int i = 1; i <= step; i++){
         solve();
    }
}


#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include <unordered_map>
#include<stack>
#include<iomanip>
#include <cstdio>
using namespace std;
#define ll long long

signed main() {
    int n;
    cin >> n;
    vector<int> a(n), tp(n), deg(n);
    int ans = 1;
    int ind;

    for (int i = 0; i < n; ++i)
        cin >> tp[i];
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
        if (a[i] >= 0) {
            deg[a[i]]++;
        }
    }
    for (int i = 0; i < n; ++i) {
        if (tp[i] == 1) {
            int cur = 1;
            int v = a[i];
            while (v >= 0 && deg[v] <= 1 && tp[v] == 0) {
                cur++;
                v = a[v];
            }
            if (cur >= ans) {
                ans = cur;
                ind = i;
            }
        }
    }
    cout << ans << '\n';
    vector<int> res = {ind};
    int v = a[ind];
    while (v >= 0 && deg[v] <= 1 && tp[v] == 0) {
        res.push_back(v);
        v = a[v];
    }
    reverse(res.begin(), res.end());
    for (auto u:res) {
        cout << u + 1 << ' ';
    }
    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 998244353
#define vid vector<double>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define vip vector<pair<int,int> >
#define v1s vector<string>
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >
#define pa pair<int,int>
#define endl '\n'

v2d g(100009);

void bfs(v2d &v,v1d &vis,v1d &y,int src){
    vis[src]= 1;
    queue<int> q;
    q.push(src);
    while(!q.empty()){
        int a= q.front();q.pop();
        if(g[a].size()>1||v[a].size()>1)break;
        y.pb(a);
        if(v[a].size()){
            q.push(v[a][0]);
            vis[v[a][0]];
        }
    }
}

int32_t main(){
    IOS;
    int n;cin>>n;
    v1d t(n+1),a(n+1),p;
    v2d v(n+1);
    for(int i=1;i<=n;i++){
        cin>>t[i];
        if(t[i])p.pb(i);
    }
    bool flag= true;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]){flag= false;
            g[a[i]].pb(i);
            v[i].pb(a[i]);
        }
    }
    if(flag){
        cout<<"1\n"<<p[0];return 0;
    }
    v1d r, vis(n+1,0);
    for(auto i:p){
        if(vis[i])continue;
        vis[i]= 1;
        for(auto j:v[i]){
            v1d y;y.pb(i);
            bfs(v,vis,y,j);
            if(y.size()>r.size())r= y;
        }
    }
    cout<<r.size()<<endl;
    for(int i=r.size()-1;i>=0;i--)cout<<r[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>
#define int long long
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define rep(i,A,B) for(int i=A;i<=B;i++)
#define mod 998244353
#define vid vector<double>
#define v1d vector<int>
#define v2d vector<vector<int>>
#define vip vector<pair<int,int> >
#define v1s vector<string>
#define v2s vector<vector<string> >
#define vsp vector<pair<string,string> >
#define pa pair<int,int>
#define endl '\n'

v2d g(100009);

void bfs(v2d &v,v1d &vis,v1d &y,int src){
    vis[src]= 1;
    queue<int> q;
    q.push(src);
    while(!q.empty()){
        int a= q.front();q.pop();
        if(g[a].size()>1||v[a].size()>1)break;
        y.pb(a);
        if(v[a].size()){
            q.push(v[a][0]);
            vis[v[a][0]];
        }
    }
}

int32_t main(){
    IOS;
    int n;cin>>n;
    v1d t(n+1),a(n+1),p;
    v2d v(n+1);
    for(int i=1;i<=n;i++){
        cin>>t[i];
        if(t[i])p.pb(i);
    }
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]){
            g[a[i]].pb(i);
            v[i].pb(a[i]);
        }
    }
    v1d r, vis(n+1,0);
    for(auto i:p){
        if(vis[i])continue;
        vis[i]= 1;
        for(auto j:v[i]){
            v1d y;y.pb(i);
            bfs(v,vis,y,j);
            if(y.size()>r.size())r= y;
        }
    }
    cout<<r.size()<<endl;
    for(int i=r.size()-1;i>=0;i--)cout<<r[i]<<" ";

    return 0;
}


#include<bits\stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	int i;
	vector <int> v[n+1],v1,v2;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=1;i<=n;i++)
	{
		cin>>b[i];
		if(b[i]!=0)
		{
			v[b[i]].push_back(i);
		}
	}
	int max=1;
	int count=0;
	for(i=1;i<=n;i++)
	{
		if(v[i].size()==1)
		{   if(v[i][0]==i+1)
			count++;
			else
			count=0;
		}
		else if(v[i].size()==0&&a[i]==0)
		{
			count=0;
		}
		else if(v[i].size()>1)
		{
			count=0;
		}
		else if(a[i]==1)
		{
			count++;
			if(count>max)
			{
				max=count;
				while(count--)
				{
					v1.push_back(i-count);
				}
			}
			count=0;
		}
	}
	if(max==1)
	{
		for(i=1;i<=n;i++)
		{
			if(a[i]==1)
			{
				cout<<max<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
	else
	{
	cout<<max<<endl;
	for(i=0;i<v1.size();i++)
	{
		cout<<v1[i]<<" ";
	}
    }

}


#include<bits\stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	int i;
	vector <int> v[n+1],v1,v2;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=1;i<=n;i++)
	{
		cin>>b[i];
		if(b[i]!=0)
		{
			v[b[i]].push_back(i);
		}
	}
	int max=1;
	int count=0;
	for(i=1;i<=n;i++)
	{
		if(v[i].size()==1)
		{
			count++;
		}
		else if(v[i].size()==0&&a[i]==0)
		{
			count=0;
		}
		else if(v[i].size()>1)
		{
			count=0;
		}
		else if(v[i].size()==0&&a[i]==1)
		{
			count++;
			if(count>max)
			{
				max=count;
				while(count--)
				{
					v1.push_back(i-count);
				}
			}
			count=0;
		}
	}
	if(max==1)
	{
		for(i=1;i<=n;i++)
		{
			if(a[i]==1)
			{
				cout<<max<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
	else
	{
	cout<<max<<endl;
	for(i=0;i<v1.size();i++)
	{
		cout<<v1[i]<<" ";
	}
    }

}


#include<bits\stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	int i;
	vector <int> v[n+1],v1,v2;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=1;i<=n;i++)
	{
		cin>>b[i];
		if(b[i]!=0)
		{
			v[b[i]].push_back(i);
		}
	}
	int max=1;
	int count=0;
	for(i=1;i<=n;i++)
	{
		if(v[i].size()==1)
		{   if(v[i][0]==i+1)
			count++;
		}
		else if(v[i].size()==0&&a[i]==0)
		{
			count=0;
		}
		else if(v[i].size()>1)
		{
			count=0;
		}
		else if(v[i].size()==0&&a[i]==1)
		{
			count++;
			if(count>max)
			{
				max=count;
				while(count--)
				{
					v1.push_back(i-count);
				}
			}
			count=0;
		}
	}
	if(max==1)
	{
		for(i=1;i<=n;i++)
		{
			if(a[i]==1)
			{
				cout<<max<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
	else
	{
	cout<<max<<endl;
	for(i=0;i<v1.size();i++)
	{
		cout<<v1[i]<<" ";
	}
    }

}


#include<bits\stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	int i,j;
	int par[n+1];
	vector <int> v1,v2,v[n+1],v3;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]==1)
		v1.push_back(i);
	}
	v[0].clear();
	for(i=1;i<=n;i++)
	{
		cin>>b[i];
		par[i]=b[i];
		v[b[i]].push_back(i);
	}
	int max=0;
	int count=0;
	for(i=0;i<v1.size();i++)
	{
		int m=v1[i];
		count++;
		v2.push_back(m);
		while(par[m]&&v[par[m]].size()==1)
		{
			count++;
			v2.push_back(par[m]);
			m=par[m];
		}
		if(count>max)
		{
			max=count;
			v3.clear();
			for(j=0;j<v2.size();j++)
			{
				v3.push_back(v2[j]);
			}
		}
		v2.clear();
		count=0;
	}
	cout<<max<<endl;
	for(i=v3.size()-1;i>=0;i--)
	{
		cout<<v3[i]<<" ";
	}

}


#include<bits\stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	int i;
	vector <int> v[n+1],v1,v2;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=1;i<=n;i++)
	{
		cin>>b[i];
		if(b[i]!=0)
		{
			v[b[i]].push_back(i);
		}
	}
	int max=1;
	int count=0;
	for(i=1;i<=n;i++)
	{
		if(v[i].size()==1)
		{   if(v[i][0]=i+1)
			count++;
		}
		else if(v[i].size()==0&&a[i]==0)
		{
			count=0;
		}
		else if(v[i].size()>1)
		{
			count=0;
		}
		else if(v[i].size()==0&&a[i]==1)
		{
			count++;
			if(count>max)
			{
				max=count;
				while(count--)
				{
					v1.push_back(i-count);
				}
			}
			count=0;
		}
	}
	if(max==1)
	{
		for(i=1;i<=n;i++)
		{
			if(a[i]==1)
			{
				cout<<max<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
	else
	{
	cout<<max<<endl;
	for(i=0;i<v1.size();i++)
	{
		cout<<v1[i]<<" ";
	}
    }

}


#include<bits\stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	int i,j;
	int par[n+1];
	vector <int> v1,v2,v[n+1],v3;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		if(a[i]==1)
		v1.push_back(i);
	}
	v[0].clear();
	for(i=1;i<=n;i++)
	{
		cin>>b[i];
		par[i]=b[i];
		v[b[i]].push_back(i);
	}
	int max=0;
	int count=0;
	for(i=0;i<v1.size();i++)
	{
		int m=v1[i];
		count++;
		v2.push_back(m);
		while(par[m]&&v[par[m]].size()==1)
		{
			count++;
			v2.push_back(par[m]);
			m=par[m];
		}
		if(count>max)
		{
			max=count;
			v3.clear();
			for(j=0;j<v2.size();j++)
			{
				v3.push_back(v2[j]);
			}
		}
		v2.clear();
		count=0;
	}
	cout<<max<<endl;
	for(i=0;i<v3.size();i++)
	{
		cout<<v3[i]<<" ";
	}

}


#include<bits\stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n;
	int a[n+1],b[n+1];
	int i;
	vector <int> v[n+1],v1,v2;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	for(i=1;i<=n;i++)
	{
		cin>>b[i];
		if(b[i]!=0)
		{
			v[b[i]].push_back(i);
		}
	}
	int max=1;
	int count=0;
	for(i=1;i<=n;i++)
	{
		if(v[i].size()==1)
		{   if(v[i][0]==i+1)
			count++;
			else
			count=0;
		}
		else if(v[i].size()==0&&a[i]==0)
		{
			count=0;
		}
		else if(v[i].size()>1)
		{
			count=0;
		}
		else if(v[i].size()==0&&a[i]==1)
		{
			count++;
			if(count>max)
			{
				max=count;
				while(count--)
				{
					v1.push_back(i-count);
				}
			}
			count=0;
		}
	}
	if(max==1)
	{
		for(i=1;i<=n;i++)
		{
			if(a[i]==1)
			{
				cout<<max<<endl;
				cout<<i<<endl;
				break;
			}
		}
	}
	else
	{
	cout<<max<<endl;
	for(i=0;i<v1.size();i++)
	{
		cout<<v1[i]<<" ";
	}
    }

}


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

#define ll long long
#define pii pair<int, int>

const int MAXN=1e5+9;

int t[MAXN], par[MAXN], in[MAXN];

void solve(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++) cin >> t[i];
	for(int i = 0; i < n; i++){
		int x;
		cin >> x;
		--x;
		par[i] = x;
		in[x]++;
	}
	vector<int> ans;

	for(int i = 0; i < n; i++){
		if(t[i]){
			int st = i;
			vector<int> tmp;
			while(par[st] != -1 && in[par[st]] <= 1)
				tmp.push_back(st), st = par[st];
			tmp.push_back(st);
			if(ans.size() < tmp.size())
				ans = tmp;
		}
	}

	cout << ans.size() << endl;
	reverse(ans.begin(), ans.end());
	for(int i = 0; i < ans.size(); i++) cout << 1+ans[i] << " ";
	cout << endl;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t=1;
	// cin >> t;
	while(t--){
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define loop(i,x,z) for (int i=x;i<z;i++)
int n, temp,dp[100002],ht;
vector<int> A[100002];
bool flag=0, htl[100002];
int dfs(int num)
{
	if (flag)cout << num << " ";
	if (htl[num]) return 1;
	if(!flag)if (dp[num] != -1) return dp[num];
	if (A[num].size() > 1 || A[num].size()==0) return 0;
	int k = dfs(A[num][0]);
	if (k) dp[num] = k + 1;
	else dp[num] = 0;
	return dp[num];
}
int main()
{
	cin >> n;
	memset(dp, -1, sizeof(dp));
	loop(i, 1, n+1)
	{
		cin >> temp;
		 htl[i]= temp;
	}
	loop(i, 1, n + 1)
	{
		cin >> temp;
		A[temp].emplace_back(i);
	}
	temp = 0;
	loop(i, 1, n + 1)
	{
		if (!htl[i])
		{
			int x = dfs(i);
			if (x > dp[temp]) temp = i;
		}
		else ht = i;
	}
	flag = 1;
	if (dp[temp])
	{
		cout << dp[temp] << "\n";
		dfs(temp);
	}
	else
	{
		cout << 1 << "\n"<<ht;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define loop(i,x,z) for (int i=x;i<z;i++)
int n, temp,dp[100002],ht;
vector<int> A[100002];
bool flag=0, htl[100002],vis[100002];
int dfs(int num)
{
    if(vis[num] and !flag)
    {
        return dp[num];
    }
    vis[num]=1;
	if (flag)cout << num << " ";
	if (htl[num]) return 1;
	if(!flag)if (dp[num] != -1) return dp[num];
	if (A[num].size() > 1 || A[num].size()==0) return 0;
	int k = dfs(A[num][0]);
	if (k) dp[num] = k + 1;
	else dp[num] = 0;
	return dp[num];
}
int main()
{
	cin >> n;
	memset(dp, -1, sizeof(dp));
	loop(i, 1, n+1)
	{
		cin >> temp;
		 htl[i]= temp;
	}
	loop(i, 1, n + 1)
	{
		cin >> temp;
		A[temp].emplace_back(i);
	}
	temp = 0;
	loop(i, 1, n + 1)
	{
		if (!htl[i])
		{
			int x = dfs(i);
			if (x > dp[temp]) temp = i;
		}
		else ht = i;
	}
	flag = 1;
	if (dp[temp]>0)
	{
		cout << dp[temp] << "\n";
		dfs(temp);
	}
	else
	{
		cout << 1 << "\n"<<ht;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define loop(i,x,z) for (int i=x;i<z;i++)
int n, temp,dp[100002],ht;
vector<int> A[100002];
bool flag=0, htl[100002],vis[100002];
int dfs(int num)
{
    if(vis[num] and !flag)
    {
        return dp[num];
    }
    vis[num]=1;
	if (flag)cout << num << " ";
	if (htl[num]) return 1;
	if(!flag)if (dp[num] != -1) return dp[num];
	if (A[num].size() > 1 || A[num].size()==0) return 0;
	int k = dfs(A[num][0]);
	if (k) dp[num] = k + 1;
	else dp[num] = 0;
	return dp[num];
}
int main()
{
	cin >> n;
	memset(dp, -1, sizeof(dp));
	loop(i, 1, n+1)
	{
		cin >> temp;
		 htl[i]= temp;
	}
	loop(i, 1, n + 1)
	{
		cin >> temp;
		A[temp].emplace_back(i);
	}
	temp = 0;
	loop(i, 1, n + 1)
	{
		if (!htl[i])
		{
			int x = dfs(i);
			if (x > dp[temp]) temp = i;
		}
		else ht = i;
	}
	flag = 1;
	if (dp[temp])
	{
		cout << dp[temp] << "\n";
		dfs(temp);
	}
	else
	{
		cout << 1 << "\n"<<ht;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define loop(i,x,z) for (int i=x;i<z;i++)
int n, temp,dp[100002],ht;
vector<int> A[100002];
bool flag=0, htl[100002];
int dfs(int num,int par)
{
	if (flag)cout << num << " ";
	if (htl[num]) return 1;
	if(!flag)if (dp[num] != -1) return dp[num];
	if (A[num].size() > 1 || A[num].size()==0) return 0;
	if (A[num][0] == par) return 0;
	int k = dfs(A[num][0],num);
	if (k) dp[num] = k + 1;
	else dp[num] = 0;
	return dp[num];
}
int main()
{
	cin >> n;
	memset(dp, -1, sizeof(dp));
	loop(i, 1, n+1)
	{
		cin >> temp;
		 htl[i]= temp;
	}
	loop(i, 1, n + 1)
	{
		cin >> temp;
		A[temp].emplace_back(i);
	}
	temp = 0;
	loop(i, 1, n + 1)
	{
		if (!htl[i])
		{
			int x = dfs(i,0);
			if (x > dp[temp]) temp = i;
		}
		else ht = i;
	}
	flag = 1;
	if (dp[temp])
	{
		cout << dp[temp] << "\n";
		dfs(temp,0);
	}
	else
	{
		cout << 1 << "\n"<<ht;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define loop(i,x,z) for (int i=x;i<z;i++)
int n, temp,dp[100002],ht;
vector<int> A[100002];
bool flag=0, htl[100002],vis[100002];
int dfs(int num)
{
    if(vis[num]) return 0;
    vis[num]=1;
	if (flag)cout << num << " ";
	if (htl[num]) return 1;
	if(!flag)if (dp[num] != -1) return dp[num];
	if (A[num].size() > 1 || A[num].size()==0) return 0;
	int k = dfs(A[num][0]);
	if (k) dp[num] = k + 1;
	else dp[num] = 0;
	return dp[num];
}
int main()
{
	cin >> n;
	memset(dp, -1, sizeof(dp));
	loop(i, 1, n+1)
	{
		cin >> temp;
		 htl[i]= temp;
	}
	loop(i, 1, n + 1)
	{
		cin >> temp;
		A[temp].emplace_back(i);
	}
	temp = 0;
	loop(i, 1, n + 1)
	{
		if (!htl[i])
		{
			int x = dfs(i);
			if (x > dp[temp]) temp = i;
		}
		else ht = i;
	}
	flag = 1;
	if (dp[temp])
	{
		cout << dp[temp] << "\n";
		dfs(temp);
	}
	else
	{
		cout << 1 << "\n"<<ht;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define loop(i,x,z) for (int i=x;i<z;i++)
int n, temp,dp[100002],ht;
vector<int> A[100002];
bool flag=0, htl[100002],vis[100002];
int dfs(int num)
{
    if(vis[num] and !flag) return 0;
    vis[num]=1;
	if (flag)cout << num << " ";
	if (htl[num]) return 1;
	if(!flag)if (dp[num] != -1) return dp[num];
	if (A[num].size() > 1 || A[num].size()==0) return 0;
	int k = dfs(A[num][0]);
	if (k) dp[num] = k + 1;
	else dp[num] = 0;
	return dp[num];
}
int main()
{
	cin >> n;
	memset(dp, -1, sizeof(dp));
	loop(i, 1, n+1)
	{
		cin >> temp;
		 htl[i]= temp;
	}
	loop(i, 1, n + 1)
	{
		cin >> temp;
		A[temp].emplace_back(i);
	}
	temp = 0;
	loop(i, 1, n + 1)
	{
		if (!htl[i])
		{
			int x = dfs(i);
			if (x > dp[temp]) temp = i;
		}
		else ht = i;
	}
	flag = 1;
	if (dp[temp])
	{
		cout << dp[temp] << "\n";
		dfs(temp);
	}
	else
	{
		cout << 1 << "\n"<<ht;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define frr(frm,fornn)for(int i=frm; i<fornn; i++)
#define rfrr(frm,fornn)for(int i=frm; i>=fornn; i--)
#define fr(iii,frm,fornn)for(int iii=frm; iii<fornn; iii++)
#define rfr(iii,frm,fornn)for(int iii=frm; iii>=fornn; iii--)
#define ll long long int
#define lll unsigned long long int
#define pb push_back
#define seet(seta) memset(seta,-1,sizeof(seta))
#define clr(seta) memset(seta,0,sizeof(seta))
#define e "\n"
#define infi 10000000000000
typedef vector <int> ve;
typedef vector <bool> vb;
ve vec[100009],ans[100009];
vb vis;
int type[100009];
int t[100009];
void dfs(int u,int x)
{
	ans[x].pb(u);
	if(vec[u].size()==0)return;
	int temp=vec[u][0];
	if(vec[u].size()==1 and t[temp]==0 and type[temp]!=-2)dfs(temp,x);
}
void solve()
{
	int n;
	cin>>n;
	int v[n+10];
	frr(1,n+1)
	{
		int x;
		cin>>x;
		v[i]=x;
		if(x)t[i]=1;
		else t[i]=0;
	}
	vis.assign(n+10,false);
	frr(1,n+1)
	{
		int x;
		cin>>x;
		if(x)
		{
			vec[i].pb(x);
			if(vis[x])type[x]=-2;
			vis[x]=1;
		}
	}
	vis.assign(n+10,false);
	frr(1,n+1)
	{
		if(v[i] and type[i]!=-2)
			dfs(i,i);
	}
	int index,mx=-333;
	frr(1,n+1)
	{
		if(v[i]==1)
		{
			int y=ans[i].size();
			if(y>mx)
			{
				mx=ans[i].size();
				index=i;
			}
		}
	}
	cout<<ans[index].size()<<e;
	rfrr(ans[index].size()-1,0)cout<<ans[index][i]<<" ";
}

main()
{
	//freopen("input.txt","r",stdin);
	int tc,cse=0;
	tc=1;
	while(tc--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
#define frr(frm,fornn)for(int i=frm; i<fornn; i++)
#define rfrr(frm,fornn)for(int i=frm; i>=fornn; i--)
#define fr(iii,frm,fornn)for(int iii=frm; iii<fornn; iii++)
#define rfr(iii,frm,fornn)for(int iii=frm; iii>=fornn; iii--)
#define ll long long int
#define lll unsigned long long int
#define pb push_back
#define seet(seta) memset(seta,-1,sizeof(seta))
#define clr(seta) memset(seta,0,sizeof(seta))
#define e "\n"
#define infi 10000000000000
typedef vector <int> ve;
typedef vector <bool> vb;
ve vec[100009],ans[100009];
vb vis;
int type[100009];
int t[100009];

void dfs(int u,int x)
{
	//cout<<x<<" "<<u<<e;
	ans[x].pb(u);
	if(vec[u].size()==0)return;

	int temp=vec[u][0];
//	cout<<vec[u].size()<<e;

	if(vec[u].size()==1 and t[temp]==0 and type[temp]!=-2)dfs(temp,x);
	//cout<<"__"<<e;
}
void solve()
{
	int n;
	cin>>n;
	int v[n+10];
	frr(1,n+1)
	{
		int x;
		cin>>x;
		v[i]=x;
		if(x)t[i]=1;
		else t[i]=0;
	}
	vis.assign(n+10,false);
	frr(1,n+1)
	{
		int x;
		cin>>x;
		if(x)
		{
			vec[i].pb(x);
			if(vis[x])type[x]=-2;
			vis[x]=1;
		}
	}
	vis.assign(n+10,false);
	frr(1,n+1)
	{
		if(v[i] and type[i]!=-2)
			dfs(i,i);
	}

	int index,mx=-333;
	frr(1,n+1)
	{
		if(v[i]==1)
		{
			//cout<<index<<".. "<<i<<" "<<ans[i].size()<<" "<<mx<<e;
			int y=ans[i].size();
			if(y>mx)
			{
				mx=ans[i].size();
				//cout<<"bal"<<e;
				index=i;
			}
		}
	}
	//cout<<"______"<<index<<e;
	cout<<ans[index].size()<<e;
	rfrr(ans[index].size()-1,0)cout<<ans[index][i]<<" ";
//	cout<<ans[index].size()<<e;
//	frr(0,ans[index].size())cout<<ans[index][i]<<" ";
}

main()
{
	//freopen("input.txt","r",stdin);
	int tc,cse=0;
	tc=1;
	while(tc--)
	{
		solve();
	}
}


#include<bits/stdc++.h>
using namespace std;
int arr1[1000000], arr2[1000000], freq[1000000];
set<int> si;
vector<vector<int> > adjlist;
vector<int> visited, vi, fini;
int coun=0;
void dfs(int s){
    visited[s]=1;
    coun++;
    vi.push_back(s);
    for(int i=0; i<adjlist[s].size(); i++){
        int v=adjlist[s][i];
        if(visited[v]==0 && si.find(v)==si.end() && arr1[v]==0)
            dfs(v);
    }
}
int main(){
int n;
cin>>n;
visited.assign(n+1, 0);
adjlist.assign(n+1, vector<int>());
for(int i=1; i<=n; i++)
    cin>>arr1[i];
for(int i=0; i<n; i++){
    cin>>arr2[i];
    if(arr2[i]==0)
        continue;
    adjlist[i+1].push_back(arr2[i]);
    freq[arr2[i]]++;
    if(freq[arr2[i]]>=2)
        si.insert(arr2[i]);
}
int maxi=0;
for(int i=1; i<=n; i++){
    if(arr1[i]==1){
        coun=0;
        vi.clear();
        dfs(i);
        if(maxi<coun){
            maxi=coun;
            fini=vi;
        }
    }
}
cout<<fini.size()<<'\n';
for(int i=0; i<fini.size(); i++)
    cout<<fini[i]<<" ";
return 0;
}


#include<bits/stdc++.h>
using namespace std;
int arr1[1000000], arr2[1000000], freq[1000000];
set<int> si;
vector<vector<int> > adjlist;
vector<int> visited, vi, fini;
int coun=0;
void dfs(int s){
    visited[s]=1;
    coun++;
    vi.push_back(s);
    for(int i=0; i<adjlist[s].size(); i++){
        int v=adjlist[s][i];
        if(visited[v]==0 && si.find(v)==si.end() && arr1[v]==0)
            dfs(v);
    }
}
int main(){
int n;
cin>>n;
visited.assign(n+1, 0);
adjlist.assign(n+1, vector<int>());
for(int i=1; i<=n; i++)
    cin>>arr1[i];
for(int i=0; i<n; i++){
    cin>>arr2[i];
    if(arr2[i]==0)
        continue;
    adjlist[i+1].push_back(arr2[i]);
    freq[arr2[i]]++;
    if(freq[arr2[i]]>=2)
        si.insert(arr2[i]);
}
int maxi=0;
for(int i=1; i<=n; i++){
    if(arr1[i]==1){
        coun=0;
        vi.clear();
        dfs(i);
        if(maxi<coun){
            maxi=coun;
            fini=vi;
        }
    }
}
reverse(fini.begin(), fini.end());
cout<<fini.size()<<'\n';
for(int i=0; i<fini.size(); i++)
    cout<<fini[i]<<" ";
return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 100000 + 5;

int par[maxn];
int type[maxn];
int out[maxn];

int main(void){
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cin >> type[i];
	}
	for(int i = 1 ; i <= n ; i++){
		cin >> par[i];
		out[par[i]]++;
	}
	vector<int> ans;
	for(int i = 1 ; i <= n ; i++){
		if(type[i] == 1){
			vector<int> path;
			int curr = i;
			while(par[curr] != 0 && out[par[curr]] == 1){
				path.push_back(curr);
				curr = par[curr];
			}
			path.push_back(curr);
			if(path.size() > ans.size()){
				ans = path;
			}
		}
	}
	reverse(ans.begin(), ans.end());
	cout << ans.size() << endl;
	for(int x : ans){
		cout << x << " ";
	}
	cout << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int maxn = 10000 + 1;

int par[maxn];
int type[maxn];
int out[maxn];

int main(void){
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i++){
		cin >> type[i];
	}
	for(int i = 1 ; i <= n ; i++){
		cin >> par[i];
		out[par[i]]++;
	}
	vector<int> ans;
	for(int i = 1 ; i <= n ; i++){
		if(type[i] == 1){
			vector<int> path;
			int curr = i;
			while(par[curr] != 0 && out[par[curr]] == 1){
				path.push_back(curr);
				curr = par[curr];
			}
			path.push_back(curr);
			if(path.size() > ans.size()){
				ans = path;
			}
		}
	}
	reverse(ans.begin(), ans.end());
	cout << ans.size() << endl;
	for(int x : ans){
		cout << x << " ";
	}
	cout << endl;
	return 0;
}


/*
___ is a raging sadist.
*/

//Destroy all WA at test 2

#include <bits/stdc++.h>
#define fastio()                  \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
#define endl "\n"
#define PI 3.14159265
const ll MOD = 1000 * 1000 * 1000 + 7;
const ll MOD1 = 998244353;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
using namespace std;

ll power(ll x, ll y)
{
   ll res = 1;

    while (y > 0)
    {

        if (y & 1)
            res = (long long)(res*x);

        y = y>>1;

        if (x<=100000000)
        x = (long long)(x*x);

        //cout<<x<<'\n';
    }
    return res;
}

ll cnt[100005];
ll dp[100005];
vector <ll> ans;

ll track(ll idx, ll obj[], ll from[])
{

    if (idx==0)
        return 0;

    if (dp[idx]!=0)
        return dp[idx];

    if (cnt[idx]>1)
        return dp[idx]=0;

    dp[idx]=dp[idx]+1+track(from[idx],obj,from);

    return dp[idx];

}

void trace(ll idx, ll from[])
{
    if (cnt[idx]>1||idx==0)
        return;

    ans.pb(idx);
    trace(from[idx],from);
}

int main()
{

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

fastio();

    ll n;
    cin>>n;

    ll obj[n+1]; ll from[n+1];

    For(i,1,n+1)
    cin>>obj[i];

    For(i,1,n+1)
    {
        cin>>from[i];
        cnt[from[i]]++;
    }

    ll fin=0; ll pos=1;

    for(int i=1;i<=n;i++)
    {
        if (obj[i]==1)
        {

            ll now=track(i,obj,from);

            if (now>fin)
            {
                 fin=now;
                 pos=i;
            }
        }
    }

    //cout<<pos<<endl;

    cout<<fin<<endl;

    trace(pos,from);

    for(int i=siz(ans)-1;i>=0;i--)
        cout<<ans[i]<<' ';

return 0;
}


#define _USE_MATH_DEFINES

#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <set>
#include <utility>
#include <vector>
#include <list>
#include <algorithm>
#include <regex>
#include <cmath>
#include <map>
#include <iterator>
#include <climits>
#include <numeric>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <queue>
#include <deque>
#include <stack>
#include <array>
#include <valarray>
#include <complex>
#include <random>
#include <functional>
#include <cassert>
#include <chrono>
#include <memory>

using namespace std;

#define int long long
//region TEMPLATE
////@formatter:off
//#define double long double
typedef long long ll;
typedef unsigned long long ull;
template<typename T>void __read(T &a) { cin >> a; }
template<typename T, typename ... Args> void __read(T &a, Args &... args) { cin >> a;__read(args...);}
#define make(t, ...) t __VA_ARGS__; __read(__VA_ARGS__)
#define makev(a, n) VI a(n); FOR(n) cin >> a[i]
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define FORIT(p) for(auto it = p.begin(); it != p.end(); it++)
#define FOR(b) for(signed i = 0; i <(b); i++)
#define FORE(i, l, r) for(int i = (l); i < (r); i++)
#define NFOR(i, n) for(int i = (((int) n)-1); i >= 0; i--)
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
typedef pair<pair<int, int>, int> PPII;
constexpr ll M7 = 1000000007ll;
constexpr ll M9 = 1000000009ll;
constexpr ll MFFT = 998244353ll;
template<class T> void outv(T &a) { for (auto &x : a) cout << x << ' '; }
static mt19937 rnd(static_cast<unsigned>(chrono::steady_clock::now().time_since_epoch().count())); // NOLINT(cert-err58-cpp)
auto __fast_io__ = (ios_base::sync_with_stdio(false), cin.tie(nullptr)); // NOLINT(cert-err58-cpp,cppcoreguidelines-interfaces-global-init)
////@formatter:on
//endregion

int32_t main() {
    make(int, n);
    makev(t, n);
    VI pr(n), d(n);
    for(int i = 0; i < n; ++i){
        cin >> pr[i];
        pr[i]--;
        if (pr[i] >= 0){
            d[pr[i]]++;
        }
    }
    VI ans;
    for(int i = 0; i < n; ++i){
        if (t[i] == 1){
            VI cur;
            cur.push_back(i);
            while(true){
                int v = pr[cur.back()];
                if (v != -1 && d[v] <= 1 && t[v] == 0){
                    cur.push_back(v);
                } else {
                    break;
                }
            }
            if (cur.size() > ans.size()){
                ans = cur;
            }
        }
    }
    reverse(ALL(ans));
    cout << ans.size() << '\n';
    for(auto& v : ans){
        cout << v + 1 << ' ';
    }

    return 0;
}


//Be Name Khorgraphaph, Besmellah Rahmane Rahim, In The Name Of God;
//#include<bits/stweight++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>
#include <iostream>
#include <fstream>

using namespace std;

const int N = 1e5 + 5;
vector<int> hotel;
int deg[N];
int par[N];
int n;
int ans = 0;
vector<int> jvb;

void solver(int k)
{
	vector<int> path;
	int cnt = 1;
	path.push_back(k);
	while (par[k] != k)
	{
		k = par[k];
		if (deg[k] > 1)
		{
			break;
		}
		path.push_back(k);
		cnt++;
	}
	ans = max(ans, cnt);
	jvb.clear();
	if (ans == cnt)
	{
		for (auto nei: path)
		{
			jvb.push_back(nei);
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int tmp;
		cin >> tmp;
		if (tmp == 1)
		{
			hotel.push_back(i);
		}
	}
	for (int i = 0; i < n; i++)
	{
		int p;
		cin >> p;
		if (p == 0)
		{
			par[i] = i;
			continue;
		}
		else
		{
			par[i] = p - 1;
			deg[p - 1]++;
		}
	}
	for (auto u: hotel)
	{
		solver(u);
	}
	cout << ans << endl;
	reverse(jvb.begin(), jvb.end());
	for (auto nei: jvb)
	{
		cout << nei + 1 << " ";
	}
}


//Be Name Khorgraphaph, Besmellah Rahmane Rahim, In The Name Of God;
//#include<bits/stweight++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>
#include <iostream>
#include <fstream>

using namespace std;

const int N = 1e5 + 5;
vector<int> hotel;
int deg[N];
int par[N];
int n;
int ans = 0;
vector<int> jvb;

void solver(int k)
{
	vector<int> path;
	int cnt = 1;
	path.push_back(k);
	while (par[k] != k)
	{
		k = par[k];
		if (deg[k] > 1)
		{
			break;
		}
		path.push_back(k);
		cnt++;
	}
	ans = max(ans, cnt);
	if (ans == cnt)
	{
		jvb.clear();
		for (auto nei: path)
		{
			jvb.push_back(nei);
		}
	}
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		int tmp;
		cin >> tmp;
		if (tmp == 1)
		{
			hotel.push_back(i);
		}
	}
	for (int i = 0; i < n; i++)
	{
		int p;
		cin >> p;
		if (p == 0)
		{
			par[i] = i;
			continue;
		}
		else
		{
			par[i] = p - 1;
			deg[p - 1]++;
		}
	}
	for (auto u: hotel)
	{
		solver(u);
	}
	cout << ans << endl;
	reverse(jvb.begin(), jvb.end());
	for (auto nei: jvb)
	{
		cout << nei + 1 << " ";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
typedef long long ll;
typedef pair<ll,ll>pll;
ll bin_pow(ll a,ll b){
    if(b==0)return 1;
    if(b%2==0){
        ll t=bin_pow(a,b/2);
        return t*t%MOD;
    }
    else return a*bin_pow(a,b-1)%MOD;
}
bool used[100001];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll t=1,n,m,k=0,x=0,y=0,z=0,sum=0,l=0,r=0,ans=0,mn=LLONG_MAX,mx=LLONG_MIN,d;
    //cin>>t;
    while(t--){
            cin>>n;
            vector<ll>a(n),b(n),c(n),d;
            for(int i=0;i<n;i++)cin>>b[i];
            for(int i=0;i<n;i++){
                cin>>a[i],a[i]--;
            if(a[i]!=-1)c[a[i]]++;
    }
            for(int i=0;i<n;i++){
                    vector<ll>e;
                if(b[i]==1){
                        x=i;
                        y=1;
                    e.push_back(x);
                    while(a[x]!=-1){
                        x=a[x];
                        if(c[x]>=2)break;
                        e.push_back(x);
                        y++;

                    }
                    if(y>mx){
                            mx=y;
                        d.clear();
                        for(auto u : e)d.push_back(u);
                    }
                }
            }
            ans=mx;
    cout<<ans<<"\n";
    reverse(d.begin(),d.end());
    for(auto u : d)cout<<u+1<<" ";
    cout<<"\n";
    }
    return 0;
}


 #include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin>>n;
	vector<set<int> > U(n+1);
	vector<set<int> > U1(n+1);
	vector<bool> ob(n+1);
	for(int i=1;i<=n;++i)
	{
		int a;
		cin>>a;
		ob[i]=a;
	}

    for(int i=1;i<=n;++i)
    {
    	int a;
    	cin>>a;
    	if(a)
    	{
    	U[i].insert(a);
    	U1[a].insert(i);
        }
    }
    /*
	for(int i=1;i<=n;++i)
	{
		cout<<i<<": ";
		for(set<int>::iterator it = U[i].begin();it!=U[i].end();++it)
		{
			cout<<*it<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
		for(int i=1;i<=n;++i)
	{
		cout<<i<<": ";
		for(set<int>::iterator it = U1[i].begin();it!=U1[i].end();++it)
		{
			cout<<*it<<" ";
		}
		cout<<endl;
	}
	*/
	vector<int> max_vc;
	for(int i = 1;i<=n;++i)
	{
		if(ob[i])
	     {
	     	int ind = i;
	     	cout<<ind<<" "<<endl;
	     	int count = 0;
	     	vector<int> path;
	     	while(U[ind].size()<=1&&ind!=0&&U1[ind].size()<=1)
	     	{
	     	    ++count;
	     	    path.push_back(ind);
			    set<int>::iterator it = U[ind].begin();
			 	ind = *it;
			 	//cout<<"ind "<<endl;
			 	//cout<<ind<<endl;
			 	if(ob[ind])break;
			}
		//	cout<<count<<endl;
		//	cout<<i<<": ";
	     //	for(int i1 = path.size()-1;i1>-1;--i1)cout<<path[i1]<<" ";
	     //	cout<<endl;
	     if(count>max_vc.size())
		 {
		 	max_vc.clear();
		 for(int i1=0;i1<count;++i1)max_vc.push_back(path[i1]);
     	}
	   	}
	}
	cout<<max_vc.size()<<endl;
	for(int i=max_vc.size()-1;i>-1;--i)cout<<max_vc[i]<<" ";
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin>>n;
	vector<set<int> > U(n+1);
	vector<set<int> > U1(n+1);
	vector<bool> ob(n+1);
	for(int i=1;i<=n;++i)
	{
		int a;
		cin>>a;
		ob[i]=a;
	}

    for(int i=1;i<=n;++i)
    {
    	int a;
    	cin>>a;
    	U[i].insert(a);
    	U1[a].insert(i);
    }
	for(int i=1;i<=n;++i)
	{

		cout<<i<<": ";
		for(set<int>::iterator it = U[i].begin();it!=U[i].end();++it)
		{
			cout<<*it<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
		for(int i=1;i<=n;++i)
	{
		cout<<i<<": ";
		for(set<int>::iterator it = U1[i].begin();it!=U1[i].end();++it)
		{
			cout<<*it<<" ";
		}
		cout<<endl;
	}
	int mmax = 0;
	for(int i=1;i<=n;++i)
	{
		int count = 0;
		if(ob[i])
		{
			int ind = i;

			set<int>::iterator it = U1[ind].begin();
			 while(U[ind].size()&&U[ind].size()==1)
			 {
			 	cout<<ind<<" "<<U[ind].size()<<endl;
			    ind = *it;
			    ++count;
			    it = U1[ind].begin();
			 }
		}
		mmax = max(mmax,count);
	}
	cout<<mmax<<endl;
	return 0;
}


 #include<bits/stdc++.h>

using namespace std;

int main()
{
	int n;
	cin>>n;
	vector<set<int> > U(n+1);
	vector<set<int> > U1(n+1);
	vector<bool> ob(n+1);
	for(int i=1;i<=n;++i)
	{
		int a;
		cin>>a;
		ob[i]=a;
	}

    for(int i=1;i<=n;++i)
    {
    	int a;
    	cin>>a;
    	if(a)
    	{
    	U[i].insert(a);
    	U1[a].insert(i);
        }
    }
    /*
	for(int i=1;i<=n;++i)
	{
		cout<<i<<": ";
		for(set<int>::iterator it = U[i].begin();it!=U[i].end();++it)
		{
			cout<<*it<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
		for(int i=1;i<=n;++i)
	{
		cout<<i<<": ";
		for(set<int>::iterator it = U1[i].begin();it!=U1[i].end();++it)
		{
			cout<<*it<<" ";
		}
		cout<<endl;
	}
	*/
	vector<int> max_vc;
	for(int i = 1;i<=n;++i)
	{
		if(ob[i])
	     {
	     	int ind = i;
	     //	cout<<ind<<" "<<endl;
	     	int count = 0;
	     	vector<int> path;
	     	while(U[ind].size()<=1&&ind!=0&&U1[ind].size()<=1)
	     	{
	     	    ++count;
	     	    path.push_back(ind);
			    set<int>::iterator it = U[ind].begin();
			 	ind = *it;
			 	//cout<<"ind "<<endl;
			 	//cout<<ind<<endl;
			 	if(ob[ind])break;
			}
		//	cout<<count<<endl;
		//	cout<<i<<": ";
	     //	for(int i1 = path.size()-1;i1>-1;--i1)cout<<path[i1]<<" ";
	     //	cout<<endl;
	     if(count>max_vc.size())
		 {
		 	max_vc.clear();
		 for(int i1=0;i1<count;++i1)max_vc.push_back(path[i1]);
     	}
	   	}
	}
	cout<<max_vc.size()<<endl;
	for(int i=max_vc.size()-1;i>-1;--i)cout<<max_vc[i]<<" ";
	return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=2e3+5;
const int mod=1e9+7;
const int base=37;
const int _mod=998244353;

int n,m,ans,res,val[N];
char str[2][N],W[N];

template <typename _Tp> inline void IN(_Tp&x) {
	char ch;bool flag=0;x=0;
	while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=true;
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	if(flag) x=-x;
}

int power[N];
inline int mul(int x,int y) {return 1ll*x*y%_mod;}
inline int del(int x,int y) {x-=y;if(x<0) x+=_mod;return x;}
inline int add(int x,int y) {x+=y;if(x>=_mod) x-=_mod;return x;}
inline void inc(int&x,int y) {x+=y;if(x>=_mod) x-=_mod;}
struct Hash {
	int pre[N],suf[N];
	inline void init(char*S) {
		for(int i=1;i<=n;++i) pre[i]=add(mul(pre[i-1],base),S[i]-'a');
		for(int i=n;i>=1;--i) suf[i]=add(mul(suf[i+1],base),S[i]-'a');
	}
	inline int get_pre(int l,int r) {return del(pre[r],mul(pre[l-1],power[r-l+1]));}
	inline int get_suf(int l,int r) {return del(suf[l],mul(suf[r+1],power[r-l+1]));}
}L[2];
inline int merge(int x,int y,int len) {return add(x,mul(y,power[len]));}

int f[2][N][N],g[2][N][N];
inline void solve() {
	memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	for(int i=1;i<=m;++i) val[i]=add(mul(val[i-1],base),W[i]-'a');
	for(int i=0;i<=1;++i)
		for(int r=1;r<=n;++r) for(int l=r;l>=1;--l) {
			int len=2*(r-l+1);
			if(len>m) continue;
			ll _1=L[i].get_pre(l,r),_2=L[i^1].get_suf(l,r);
			if(merge(_1,_2,r-l+1)==val[len]) len==m?++res:g[i][r][len]=1;
		}
	for(int i=0;i<=1;++i)
		for(int j=1;j<=n;++j)
			if(str[i][j]==W[1]) g[i][j][1]=1;
	for(int len=1;len<m;++len)
		for(int j=1;j<=n;++j) for(int i=0;i<=1;++i) {
			if(g[i][j][len])
				if(j<n&&str[i][j+1]==W[len+1]) inc(f[i][j+1][len+1],g[i][j][len]);
			if(f[i][j][len]) {
				if(j<n&&str[i][j+1]==W[len+1]) inc(f[i][j+1][len+1],f[i][j][len]);
				if(str[i^1][j]==W[len+1]) inc(g[i^1][j][len+1],f[i][j][len]);
			}
		}
	for(int i=m;i>=1;--i) val[i]=add(mul(val[i+1],base),W[i]-'a');
	for(int i=0;i<=1;++i)
		for(int l=1;l<=n;++l) {
			if(str[i][l]==W[m])
				inc(ans,(bool)(m==1)),inc(ans,f[i][l-1][m-1]),inc(ans,g[i][l-1][m-1]);
			for(int r=l;r<=n;++r) {
				int len=2*(r-l+1);
				if(len>m) continue;
				ll _1=L[i^1].get_pre(l,r),_2=L[i].get_suf(l,r);
				if(merge(_2,_1,r-l+1)==val[m-len+1])
					if(len==m&&m!=2) ++res;
					else inc(ans,f[i][l-1][m-len]),inc(ans,g[i][l-1][m-len]);
			}
		}
}

int main() {
	scanf("%s\n%s\n%s",str[0]+1,str[1]+1,W+1);
	n=strlen(str[0]+1),m=strlen(W+1);
	L[0].init(str[0]),L[1].init(str[1]);
	int lim=max(n,m);
	power[0]=1;
	for(int i=1;i<=lim;++i) power[i]=mul(power[i-1],base);
	solve(),reverse(W+1,W+1+m),solve();
	inc(ans,res>>1);
	if(m==1) ans>>=1;
	printf("%d\n",ans);
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=2005;
const int mod=1e9+7;
const int base=131;
int n,m,pw[N],val[2][N],pre[N],suf[N],f[2][N][N],ans;
char s[2][N],t[N];
void Inc(int &x,int y){x+=y;x>=mod?x-=mod:x;}
int cal(int *hsh,int l,int r){
	return (hsh[r]+1ll*hsh[l-1]*(mod-pw[r-l+1]))%mod;
}
bool check1(int i,int j,int k){
	if(k&1)return false;k>>=1;if(j<k)return false;
	return cal(val[i],j-k+1,j)==cal(suf,m-k+1,m)&&cal(val[i^1],j-k+1,j)==cal(pre,k+1,k+k);
}
bool check2(int i,int j,int k){
	if(k&1)return false;k>>=1;if(j+k>n+1)return false;
	return cal(val[i^1],j,j+k-1)==cal(pre,m-k-k+1,m-k)&&cal(val[i],j,j+k-1)==cal(suf,1,k);
}
void solve(){
	memset(f,0,sizeof(f));
	for(int j=1;j<=n;++j)
		for(int i=0;i<2;++i)
			for(int k=1;k<=m;++k)
				if(s[i][j]==t[k]){
					if(k==1)Inc(f[i][j][k],1);
					if(check1(i^1,j,k))Inc(f[i][j][k],1);
					Inc(f[i][j][k],f[i][j-1][k-1]);
					if(k>1&&s[i^1][j]==t[k-1])Inc(f[i][j][k],f[i^1][j-1][k-2]);
					if(j<n&&k<m){
						int kk=m-k;
						if(kk==1&&s[i][j+1]==t[m])Inc(ans,f[i][j][k]);
						if(check2(i^1,j+1,kk))Inc(ans,f[i][j][k]);
					}
				}
}
int main(){
	for(int i=pw[0]=1;i<N;++i)pw[i]=1ll*pw[i-1]*base%mod;
	scanf("%s%s%s",s[0]+1,s[1]+1,t+1);
	n=strlen(s[0]+1);m=strlen(t+1);
	if(m==1){
		for(int i=0;i<2;++i)
			for(int j=1;j<=n;++j)
				if(s[i][j]==t[1])
					++ans;
	}else{
		for(int i=0;i<2;++i)
			for(int j=1;j<=n;++j)
				val[i][j]=(1ll*val[i][j-1]*base+s[i][j])%mod;
		for(int i=1;i<=m;++i){
			pre[i]=(1ll*pre[i-1]*base+t[i])%mod;
			suf[i]=(1ll*suf[i-1]*base+t[m-i+1])%mod;
		}
		solve();
		reverse(t+1,t+m+1);
		for(int i=1;i<=m;++i){
			pre[i]=(1ll*pre[i-1]*base+t[i])%mod;
			suf[i]=(1ll*suf[i-1]*base+t[m-i+1])%mod;
		}
		solve();
		for(int i=0;i<2;++i)
			for(int j=1;j<=n;++j){
				if(check1(i,j,m))Inc(ans,1);
				if(m>2&&check2(i,j,m))Inc(ans,1);
			}
	}
	printf("%d\n",ans);
	return 0;
}


/*
 _       ___  ___       ___  ___   _____   _____    _____       ___   _       _____   _   _____    _____
| |     /   |/   |     /   |/   | /  _  \ |  _  \  |_   _|     /   | | |     |  _  \ | | |  _  \  |  _  \
| |    / /|   /| |    / /|   /| | | | | | | |_| |    | |      / /| | | |     | |_| | | | | |_| |  | | | |
| |   / / |__/ | |   / / |__/ | | | | | | |  _  /    | |     / / | | | |     |  _  { | | |  _  /  | | | |
| |  / /       | |  / /       | | | |_| | | | \ \    | |    / /  | | | |___  | |_| | | | | | \ \  | |_| |
|_| /_/        |_| /_/        |_| \_____/ |_|  \_\   |_|   /_/   |_| |_____| |_____/ |_| |_|  \_\ |_____/

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

#define int long long
#define I inline
#define ri register int
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for(ri i=(a);i<=(b);++i)
#define dow(i,a,b) for(ri i=(a);i>=(b);--i)
#define go(i,u) for(ri i=lnk[u];i;i=nxt[i])
#define PII pair<int,int>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define mem(f,x) memset(f,x,sizeof(f))

I int gi(){
	int x=0,f=0;char ch=getchar();
	while(!isdigit(ch)){f|=ch=='-',ch=getchar();}
	while(isdigit(ch)){x=x*10+ch-48,ch=getchar();}
	return f?-x:x;
}

I void pi(int x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)pi(x/10);
	putchar(x%10+48);
}
I void pib(int x){pi(x);putchar(' ');}
I void pin(int x){pi(x);putchar('\n');}

const int N = 2e3+5,P = 233,mo = 1e9+7;

int n,m;
int base[N],h1[2][N],h2[2][N],h[N];
char a[2][N],b[N];

int f[2][N][N],g[2][N][N];

I int get1(int l,int r,int i){return h1[i][r] - h1[i][l-1]*base[r-l+1];}
I int get2(int l,int r,int i){return h2[i][l] - h2[i][r+1]*base[r-l+1];}
I int merge(int x,int y,int len){return x*base[len] + y;}
I void add(int &x,int y){x = (x+y+mo)%mo;}
I void del(int &x,int y){x = (x-y+mo)%mo;}

int res,ans;

void dp()
{
	mem(f,0);mem(g,0);

	rep(i,1,m)h[i] = h[i-1]*P+b[i]-'a'+1;
	rep(i,0,1)rep(j,1,n)
	for(ri k = j,len = (2*(j-k+1));k&&len<=m;--k,len+=2)
		if(merge(get2(k,j,i^1),get1(k,j,i),j-k+1) == h[len])
		{
			if(len == m)res ++;
			else g[i][j][len] = 1;
		}

	rep(i,0,1)rep(j,1,n)
	if(a[i][j] == b[1]) g[i][j][1] = 1;
	rep(k,1,m-1)rep(i,0,1)rep(j,1,n)
	{
		if(j<n && a[i][j+1] == b[k+1])
		add(f[i][j+1][k+1],g[i][j][k]+f[i][j][k]);
		if(a[i^1][j] == b[k+1])
		add(g[i^1][j][k+1],f[i][j][k]);
	}

	dow(i,m,1)h[i] = h[i+1]*P + b[i]-'a'+1;
	rep(i,0,1)rep(j,1,n)
	{
		if(a[i][j] == b[m])
			if(m == 1)add(ans,1);
			else add(ans,f[i][j-1][m-1]+g[i][j][m-1]);
		for(ri k = j,len = 2*(k-j+1);k<=n&&len<=m;k++,len+=2)
			if(merge(get1(j,k,i^1),get2(j,k,i),k-j+1) == h[m-len+1])
				if(len == m && m!=2)res ++;
				else add(ans,f[i][j-1][m-len]+g[i][j-1][m-len]);
	}
}

signed main()
{

	scanf("%s",a[0]+1);scanf("%s",a[1]+1);
	n = strlen(a[0]+1);
	scanf("%s",b+1);
	m = strlen(b+1);

	base[0]=1;rep(i,1,max(n,m))base[i]=base[i-1]*P;
	rep(i,0,1)rep(j,1,n)h1[i][j] = h1[i][j-1]*P+a[i][j]-'a'+1;
	rep(i,0,1)dow(j,n,1)h2[i][j] = h2[i][j+1]*P+a[i][j]-'a'+1;

	dp();
	reverse(b+1,b+m+1);
	dp();
	add(ans,res/2);
	if(m==1)ans>>=1;
	pin(ans);
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=2e3+5;
const int mod=1e9+7;
const int base=37;
const int _mod=19260817;

int n,m,ans,res,val[N];
char str[2][N],W[N];

template <typename _Tp> inline void IN(_Tp&x) {
	char ch;bool flag=0;x=0;
	while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=true;
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	if(flag) x=-x;
}

int power[N];
inline int mul(int x,int y) {return 1ll*x*y%_mod;}
inline int del(int x,int y) {x-=y;if(x<0) x+=_mod;return x;}
inline int add(int x,int y) {x+=y;if(x>=_mod) x-=_mod;return x;}
inline void inc(int&x,int y) {x+=y;if(x>=_mod) x-=_mod;}
struct Hash {
	int pre[N],suf[N];
	inline void init(char*S) {
		for(int i=1;i<=n;++i) pre[i]=add(mul(pre[i-1],base),S[i]-'a');
		for(int i=n;i>=1;--i) suf[i]=add(mul(suf[i+1],base),S[i]-'a');
	}
	inline int get_pre(int l,int r) {return del(pre[r],mul(pre[l-1],power[r-l+1]));}
	inline int get_suf(int l,int r) {return del(suf[l],mul(suf[r+1],power[r-l+1]));}
}L[2];
inline int merge(int x,int y,int len) {return add(x,mul(y,power[len]));}

int f[2][N][N],g[2][N][N];
inline void solve() {
	memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	for(int i=1;i<=m;++i) val[i]=add(mul(val[i-1],base),W[i]-'a');
	for(int i=0;i<=1;++i)
		for(int r=1;r<=n;++r) for(int l=r;l>=1;--l) {
			int len=2*(r-l+1);
			if(len>m) continue;
			ll _1=L[i].get_pre(l,r),_2=L[i^1].get_suf(l,r);
			if(merge(_1,_2,r-l+1)==val[len]) len==m?++res:g[i][r][len]=1;
		}
	for(int i=0;i<=1;++i)
		for(int j=1;j<=n;++j)
			if(str[i][j]==W[1]) g[i][j][1]=1;
	for(int len=1;len<m;++len)
		for(int j=1;j<=n;++j) for(int i=0;i<=1;++i) {
			if(g[i][j][len])
				if(j<n&&str[i][j+1]==W[len+1]) inc(f[i][j+1][len+1],g[i][j][len]);
			if(f[i][j][len]) {
				if(j<n&&str[i][j+1]==W[len+1]) inc(f[i][j+1][len+1],f[i][j][len]);
				if(str[i^1][j]==W[len+1]) inc(g[i^1][j][len+1],f[i][j][len]);
			}
		}
	for(int i=m;i>=1;--i) val[i]=add(mul(val[i+1],base),W[i]-'a');
	for(int i=0;i<=1;++i)
		for(int l=1;l<=n;++l) {
			if(str[i][l]==W[m])
				inc(ans,(bool)(m==1)),inc(ans,f[i][l-1][m-1]),inc(ans,g[i][l-1][m-1]);
			for(int r=l;r<=n;++r) {
				int len=2*(r-l+1);
				if(len>m) continue;
				ll _1=L[i^1].get_pre(l,r),_2=L[i].get_suf(l,r);
				if(merge(_2,_1,r-l+1)==val[m-len+1])
					if(len==m&&m!=2) ++res;
					else inc(ans,f[i][l-1][m-len]),inc(ans,g[i][l-1][m-len]);
			}
		}
}

int main() {
	scanf("%s\n%s\n%s",str[0]+1,str[1]+1,W+1);
	n=strlen(str[0]+1),m=strlen(W+1);
	L[0].init(str[0]),L[1].init(str[1]);
	int lim=max(n,m);
	power[0]=1;
	for(int i=1;i<=lim;++i) power[i]=mul(power[i-1],base);
	solve(),reverse(W+1,W+1+m),solve();
	inc(ans,res>>1);
	if(m==1) ans>>=1;
	printf("%d\n",ans);
	return 0;
}


#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

const int N=2e3+5;
const int mod=1e9+7;
const int base=37;
const int _mod=19260817;

int n,m,ans,res,val[N];
char str[2][N],W[N];

template <typename _Tp> inline void IN(_Tp&x) {
	char ch;bool flag=0;x=0;
	while(ch=getchar(),!isdigit(ch)) if(ch=='-') flag=true;
	while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
	if(flag) x=-x;
}

int power[N];
inline int mul(int x,int y) {return 1ll*x*y%_mod;}
inline int del(int x,int y) {x-=y;if(x<0) x+=_mod;return x;}
inline int add(int x,int y) {x+=y;if(x>=_mod) x-=_mod;return x;}
inline void inc(int&x,int y) {x+=y;if(x>=mod) x-=mod;}
struct Hash {
	int pre[N],suf[N];
	inline void init(char*S) {
		for(int i=1;i<=n;++i) pre[i]=add(mul(pre[i-1],base),S[i]-'a');
		for(int i=n;i>=1;--i) suf[i]=add(mul(suf[i+1],base),S[i]-'a');
	}
	inline int get_pre(int l,int r) {return del(pre[r],mul(pre[l-1],power[r-l+1]));}
	inline int get_suf(int l,int r) {return del(suf[l],mul(suf[r+1],power[r-l+1]));}
}L[2];
inline int merge(int x,int y,int len) {return add(x,mul(y,power[len]));}

int f[2][N][N],g[2][N][N];
inline void solve() {
	memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	for(int i=1;i<=m;++i) val[i]=add(mul(val[i-1],base),W[i]-'a');
	for(int i=0;i<=1;++i)
		for(int r=1;r<=n;++r) for(int l=r;l>=1;--l) {
			int len=2*(r-l+1);
			if(len>m) continue;
			ll _1=L[i].get_pre(l,r),_2=L[i^1].get_suf(l,r);
			if(merge(_1,_2,r-l+1)==val[len]) len==m?++res:g[i][r][len]=1;
		}
	for(int i=0;i<=1;++i)
		for(int j=1;j<=n;++j)
			if(str[i][j]==W[1]) g[i][j][1]=1;
	for(int len=1;len<m;++len)
		for(int j=1;j<=n;++j) for(int i=0;i<=1;++i) {
			if(g[i][j][len])
				if(j<n&&str[i][j+1]==W[len+1]) inc(f[i][j+1][len+1],g[i][j][len]);
			if(f[i][j][len]) {
				if(j<n&&str[i][j+1]==W[len+1]) inc(f[i][j+1][len+1],f[i][j][len]);
				if(str[i^1][j]==W[len+1]) inc(g[i^1][j][len+1],f[i][j][len]);
			}
		}
	for(int i=m;i>=1;--i) val[i]=add(mul(val[i+1],base),W[i]-'a');
	for(int i=0;i<=1;++i)
		for(int l=1;l<=n;++l) {
			if(str[i][l]==W[m])
				inc(ans,(bool)(m==1)),inc(ans,f[i][l-1][m-1]),inc(ans,g[i][l-1][m-1]);
			for(int r=l;r<=n;++r) {
				int len=2*(r-l+1);
				if(len>m) continue;
				ll _1=L[i^1].get_pre(l,r),_2=L[i].get_suf(l,r);
				if(merge(_2,_1,r-l+1)==val[m-len+1])
					if(len==m&&m!=2) ++res;
					else inc(ans,f[i][l-1][m-len]),inc(ans,g[i][l-1][m-len]);
			}
		}
}

int main() {
	scanf("%s\n%s\n%s",str[0]+1,str[1]+1,W+1);
	n=strlen(str[0]+1),m=strlen(W+1);
	L[0].init(str[0]),L[1].init(str[1]);
	int lim=max(n,m);
	power[0]=1;
	for(int i=1;i<=lim;++i) power[i]=mul(power[i-1],base);
	solve(),reverse(W+1,W+1+m),solve();
	inc(ans,res>>1);
	if(m==1) ans>>=1;
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int N = 2005;
const LL base = 233;
const LL mod = 998244353;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = 1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

namespace ModCalculator {
	const int MOD = 1e9 + 7;
	inline void Inc(int &x, int y) {
		x += y; if (x >= MOD) x -= MOD;
	}
	inline void Dec(int &x, int y) {
		x -= y; if (x < 0) x += MOD;
	}
	inline int Add(int x, int y) {
		Inc(x, y); return x;
	}
	inline int Sub(int x, int y) {
		Dec(x, y); return x;
	}
	inline int Mul(int x, int y) {
		return 1LL * x * y % MOD;
	}
	inline int Ksm(int x, int y) {
		int ret = 1;
		for (; y; y >>= 1) {
			if (y & 1) ret = Mul(ret, x);
			x = Mul(x, x);
		}
		return ret;
	}
	inline int Inv(int x) {
		return Ksm(x, MOD - 2);
	}
}
using namespace ModCalculator;

char s[2][N], t[N];
int n, m, ans, dp[2][N][N];
LL pw[N];

struct HashTable {
	LL hs[N];
	inline void Build(int nn, char *ss) {
		for (int i = 1; i <= nn; ++i) {
			hs[i] = (hs[i - 1] * base + ss[i]) % mod;
		}
	}
	inline LL Hash(int l, int r) {
		return (hs[r] - hs[l - 1] * pw[r - l + 1] % mod + mod) % mod;
	}
} HT, H0[2], H1[2];

void Calc() {
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < 2; ++j) {
			int l = j ^ 1;
			if (s[j][i] == t[1]) {
				Inc(dp[j][i][1], 1);
			}
			for (int k = 1, rk = min(i, m / 2); k <= rk; ++k) {
				if (H1[l].Hash(n - i + 1, n - i + k) == HT.Hash(1, k) &&
					H0[j].Hash(i - k + 1, i) == HT.Hash(k + 1, k * 2)) {
					Inc(dp[j][i][k * 2], 1);
				}
			}
			for (int k = 2; k <= m; ++k) {
				if (s[j][i] == t[k]) {
					Inc(dp[j][i][k], dp[j][i - 1][k - 1]);
				}
				if (k > 2 && s[j][i] == t[k] && s[l][i] == t[k - 1]) {
					Inc(dp[j][i][k], dp[l][i - 1][k - 2]);
				}
			}
			Inc(ans, dp[j][i][m]);
			for (int k = 2, rk = min(n - i, m / 2); k <= rk; ++k) {
				if (H0[j].Hash(i + 1, i + k) == HT.Hash(m - k * 2 + 1, m - k) &&
					H1[l].Hash(n - i - k + 1, n - i) == HT.Hash(m - k + 1, m)) {
					Inc(ans, dp[j][i][m - k * 2]);
				}
			}
		}
	}
}

void init() {
	scanf("%s", s[0] + 1);
	scanf("%s", s[1] + 1);
	scanf("%s", t + 1);
	n = strlen(s[0] + 1);
	m = strlen(t + 1);
	pw[0] = 1;
	for (int i = 1; i <= n; ++i) {
		pw[i] = pw[i - 1] * base % mod;
	}
}

void solve() {
	if (m == 1) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j < 2; ++j) {
				ans += (s[j][i] == t[1]);
			}
		}
		printf("%d\n", ans);
		return;
	}
	if (m == 2) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j < 2; ++j) {
				if (s[j][i] != t[1]) continue;
				if (i > 1) ans += (s[j][i - 1] == t[2]);
				if (i < n) ans += (s[j][i + 1] == t[2]);
				if (j > 0) ans += (s[j - 1][i] == t[2]);
				if (j < 1) ans += (s[j + 1][i] == t[2]);
			}
		}
		printf("%d\n", ans);
		return;
	}
	HT.Build(m, t);
	H0[0].Build(n, s[0]);
	H0[1].Build(n, s[1]);
	reverse(s[0] + 1, s[0] + n + 1);
	reverse(s[1] + 1, s[1] + n + 1);
	H1[0].Build(n, s[0]);
	H1[1].Build(n, s[1]);
	reverse(s[0] + 1, s[0] + n + 1);
	reverse(s[1] + 1, s[1] + n + 1);
	Calc();
	swap(H0[0], H1[0]);
	swap(H0[1], H1[1]);
	reverse(s[0] + 1, s[0] + n + 1);
	reverse(s[1] + 1, s[1] + n + 1);
	mset(dp, 0);
	Calc();
	printf("%d\n", ans);
}

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


#include <bits/stdc++.h>
#define mset(a, b) memset(a, b, sizeof(a))
#define mcpy(a, b) memcpy(a, b, sizeof(a))
using namespace std;
typedef long long LL;
const int N = 2005;
const LL base = 233;
const LL mod = 998244353;

template <typename T> inline void read(T &AKNOI) {
	T x = 0, flag = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') flag = 1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	AKNOI = flag * x;
}

namespace ModCalculator {
	const int MOD = 1e9 + 7;
	inline void Inc(int &x, int y) {
		x += y; if (x >= MOD) x -= MOD;
	}
	inline void Dec(int &x, int y) {
		x -= y; if (x < 0) x += MOD;
	}
	inline int Add(int x, int y) {
		Inc(x, y); return x;
	}
	inline int Sub(int x, int y) {
		Dec(x, y); return x;
	}
	inline int Mul(int x, int y) {
		return 1LL * x * y % MOD;
	}
	inline int Ksm(int x, int y) {
		int ret = 1;
		for (; y; y >>= 1) {
			if (y & 1) ret = Mul(ret, x);
			x = Mul(x, x);
		}
		return ret;
	}
	inline int Inv(int x) {
		return Ksm(x, MOD - 2);
	}
}
using namespace ModCalculator;

char s[2][N], t[N];
int n, m, ans, dp[2][N][N];
LL pw[N];

struct HashTable {
	LL hs[N];
	inline void Build(int nn, char *ss) {
		for (int i = 1; i <= nn; ++i) {
			hs[i] = (hs[i - 1] * base + ss[i]) % mod;
		}
	}
	inline LL Hash(int l, int r) {
		return (hs[r] - hs[l - 1] * pw[r - l + 1] % mod + mod) % mod;
	}
} HT, H0[2], H1[2];

void Calc() {
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j < 2; ++j) {
			int l = j ^ 1;
			if (s[j][i] == t[1]) {
				Inc(dp[j][i][1], 1);
			}
			for (int k = 1, rk = min(i, m / 2); k <= rk; ++k) {
				if (H1[l].Hash(n - i + 1, n - i + k) == HT.Hash(1, k) &&
					H0[j].Hash(i - k + 1, i) == HT.Hash(k + 1, k * 2)) {
					Inc(dp[j][i][k * 2], 1);
				}
			}
			for (int k = 2; k <= m; ++k) {
				if (s[j][i] == t[k]) {
					Inc(dp[j][i][k], dp[j][i - 1][k - 1]);
				}
				if (k > 2 && s[j][i] == t[k] && s[l][i] == t[k - 1]) {
					Inc(dp[j][i][k], dp[l][i - 1][k - 2]);
				}
			}
			Inc(ans, dp[j][i][m]);
			for (int k = 2, rk = min(n - i, m / 2); k <= rk; ++k) {
				if (H0[j].Hash(i + 1, i + k) == HT.Hash(m - k * 2 + 1, m - k) &&
					H1[l].Hash(n - i - k + 1, n - i) == HT.Hash(m - k + 1, m)) {
					Inc(ans, dp[j][i][m - k * 2]);
				}
			}
		}
	}
}

void init() {
	scanf("%s", s[0] + 1);
	scanf("%s", s[1] + 1);
	scanf("%s", t + 1);
	n = strlen(s[0] + 1);
	m = strlen(t + 1);
	pw[0] = 1;
	for (int i = 1; i <= n; ++i) {
		pw[i] = pw[i - 1] * base % mod;
	}
}

void solve() {
	if (m == 1) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j < 2; ++j) {
				ans += (s[j][i] == t[1]);
			}
		}
		printf("%d\n", ans);
		return;
	}
	if (m == 2) {
		for (int i = 1; i <= n; ++i) {
			for (int j = 0; j < 2; ++j) {
				if (s[j][i] != t[1]) continue;
				if (i > 1) ans += (s[j][i - 1] == t[2]);
				if (i < n) ans += (s[j][i + 1] == t[2]);
				if (j > 0) ans += (s[j - 1][i] == t[2]);
				if (j < 1) ans += (s[j + 1][i] == t[2]);
			}
		}
		printf("%d\n", ans);
		return;
	}
	HT.Build(m, t);
	H0[0].Build(n, s[0]);
	H0[1].Build(n, s[1]);
	reverse(s[0] + 1, s[0] + n + 1);
	reverse(s[1] + 1, s[1] + n + 1);
	H1[0].Build(n, s[0]);
	H1[1].Build(n, s[1]);
	reverse(s[0] + 1, s[0] + n + 1);
	reverse(s[1] + 1, s[1] + n + 1);
	Calc();
	printf("%d\n", ans);
	swap(H0[0], H1[0]);
	swap(H0[1], H1[1]);
	reverse(s[0] + 1, s[0] + n + 1);
	reverse(s[1] + 1, s[1] + n + 1);
	mset(dp, 0);
	Calc();
	printf("%d\n", ans);
}

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


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <assert.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cstring>
using namespace std;

const int mod = (int)1e9 + 7;
const int maxn = 2050;
char mp[2][maxn], str[maxn];
int n, m, dp[3][2][maxn][maxn];
int ext[maxn], mat[maxn][maxn];

void add(int &x, int y) {
	x = (x + y % mod) % mod;
}

void prep(int k) {
	memset(dp[k], 0, sizeof dp[k]);
	for (int t = 0; t < 2; t ++) {
		for (int i = 1; i <= n; i ++) {
			ext[i] = 0;
			while (ext[i] < min(i, m) && mp[t][i - ext[i]] == str[1 + ext[i]]) ext[i] ++;
		}

		memset(mat, 0, sizeof mat);
		for (int i = n; i > 0; i --)
			for (int j = 1; j <= m; j ++)
				mat[i][j] = str[j] != mp[!t][i] ? 0 : mat[i + 1][j + 1] + 1;

		for (int j = 0; j <= n; j ++)
			for (int l = 0; l <= m; l ++) {
				if (l == 0) {
					dp[k][!t][j][l] = 1;
					continue;
				}
				//[0][1][4][4] t=1
				if (l < 4 || l % 2 || j == 0) continue;
				if (ext[j] >= l / 2 && mat[j - l / 2 + 1][l / 2 + 1] >= l / 2)
					dp[k][!t][j][l] = 1;
			}
	}
}

void solve() { //dp[1]
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				dp[1][i][j][z] = dp[0][i][j][z];

		for (int j = 0; j < n; j ++)
		   for (int i = 0; i < 2; i ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					//goto dp[1][i][j + 1][z + 1]
					if (z + 1 <= m && str[z + 1] == mp[i][j + 1])
						add(dp[1][i][j + 1][z + 1], dp[1][i][j][z]);
					//goto dp[1][!i][j + 1][z + 2]
					if (z + 2 <= m && str[z + 1] == mp[i][j + 1] && str[z + 2] == mp[!i][j + 1])
						add(dp[1][!i][j + 1][z + 2], dp[1][i][j][z]);
				}
}

int calc() {
	prep(0);
	solve();
	reverse(str + 1, str + 1 + m);
	for (int i = 0; i < 2; i ++) reverse(mp[i] + 1, mp[i] + 1 + n);
	prep(2);

	int ans = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++) {
			add(ans, dp[1][i][j][m]);
			for (int z = 0; z < m; z ++)
				add(ans, 1ll * dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z] % mod);

		}

	return ans;
}
int main() {
	for (int i = 0; i < 2; i ++) scanf("%s", mp[i]);
	scanf("%s", str);
	n = strlen(mp[0]);
	m = strlen(str);

	for (int i = n; i > 0; i --)
		mp[0][i] = mp[0][i - 1], mp[1][i] = mp[1][i - 1];
	for (int i = m; i > 0; i --)
		str[i] = str[i - 1];
	int ans = calc();

	int two = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 1; j <= n; j ++) {
			add(two, dp[0][i][j][m]);
			add(two, dp[2][i][(n + 1) - j][m]);
		}
	for (int j = 1; j <= n; j ++) {
		if (m == 1) {
			if (mp[0][j] == str[1]) add(two, 1);
			if (mp[1][j] == str[1]) add(two, 1);
		}else if (m == 2) {
			if (mp[0][j] == str[1] && mp[1][j] == str[2]) add(two, 1);
			if (mp[1][j] == str[1] && mp[0][j] == str[2]) add(two, 1);
		}
	}
	reverse(str + 1, str + 1 + m); //!
	add(ans, calc());
	add(ans, mod - two); //!

	printf("%d\n", ans);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <assert.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cstring>
using namespace std;

const int mod = (int)1e9 + 7;
const int maxn = 2050;
char mp[2][maxn], str[maxn];
int n, m, dp[3][2][maxn][maxn];
int ext[maxn], mat[maxn][maxn];

void add(int &x, int y) {
	x = (x + y % mod) % mod;
}

void prep(int k) {
	memset(dp[k], 0, sizeof dp[k]);
	for (int t = 0; t < 2; t ++) {
		// printf("k=%d,t=%d\n",k,t);
		for (int i = 1; i <= n; i ++) {
			ext[i] = 0;
			while (ext[i] < min(i, m) && mp[t][i - ext[i]] == str[1 + ext[i]]) ext[i] ++;
			// printf("ext[%d]=%d\n",i,ext[i]);
		}

		memset(mat, 0, sizeof mat);
		for (int i = n; i > 0; i --)
			for (int j = 1; j <= m; j ++) {
				mat[i][j] = str[j] != mp[!t][i] ? 0 : mat[i + 1][j + 1] + 1;
				//if (mat[i][j] != 0)
					// printf("i=%d,j=%d, mat=%d\n",i,j,mat[i][j]);
			}

		for (int j = 0; j <= n; j ++)
			for (int l = 0; l <= m; l ++) {
				if (l == 0) {
					dp[k][!t][j][l] = 1;
				//	printf("dp[%d][%d][%d][%d]=%d\n",k,!t,j,l,dp[k][!t][j][l]);
					continue;
				}
				//[0][1][4][4] t=1
				if (l < 4 || l % 2 || j == 0) continue;
				if (ext[j] >= l / 2 && mat[j - l / 2 + 1][l / 2 + 1] >= l / 2)
					dp[k][!t][j][l] = 1;
			}
	}
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[k][i][j][z] != 0) {
					 // printf("dp[%d][%d][%d][%d]=%d\n",k,i,j,z,dp[k][i][j][z]);
				}
	 // printf("finish\n");
}

void solve() { //dp[1]
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				dp[1][i][j][z] = dp[0][i][j][z];
		for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					 // printf("dp1[%d][%d][%d]=%d\n",i,j,z,dp[1][i][j][z]);
				}
				//0,1,2 -> 1,2,4

		for (int j = 0; j < n; j ++)
		   for (int i = 0; i < 2; i ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					// printf("dp[1][%d][%d][%d] -> =%d\n",i,j,z,dp[1][i][j][z]);
					//goto dp[1][i][j + 1][z + 1]
					if (z + 1 <= m && str[z + 1] == mp[i][j + 1])
						add(dp[1][i][j + 1][z + 1], dp[1][i][j][z]);
					//goto dp[1][!i][j + 1][z + 2]
					if (z + 2 <= m && str[z + 1] == mp[i][j + 1] && str[z + 2] == mp[!i][j + 1])
						add(dp[1][!i][j + 1][z + 2], dp[1][i][j][z]);
				}
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					 // printf("dp1[%d][%d][%d]=%d\n",i,j,z,dp[1][i][j][z]);
				}
	// printf("finish\n");
}

int calc() {

	prep(0);
	solve();
	reverse(str + 1, str + 1 + m);
	for (int i = 0; i < 2; i ++) reverse(mp[i] + 1, mp[i] + 1 + n);
	prep(2);

	int ans = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++) {
			add(ans, dp[1][i][j][m]);
			if (dp[1][i][j][m]!=0) {
				// printf("add(1,%d,%d,%d)=%d\n",i,j,m,dp[1][i][j][m]);
			}
			for (int z = 0; z < m; z ++) {
				add(ans, 1ll * dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z] % mod);
				// if (1ll * dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z] % mod!=0)
					// printf("add dp[1](i,j)=%d,%d z=%d  * dp[2][i=%d][j'=%d][z'=%d]  =%d\n",
						// i,j,z,i,n+1-(i+1),m-z, dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z]);
			}
		}

	return ans;
}
int main() {
	for (int i = 0; i < 2; i ++) scanf("%s", mp[i]);
	scanf("%s", str);
	n = strlen(mp[0]);
	m = strlen(str);

	for (int i = n; i > 0; i --)
		mp[0][i] = mp[0][i - 1], mp[1][i] = mp[1][i - 1];
	for (int i = m; i > 0; i --)
		str[i] = str[i - 1];

	// printf("n=%d, m=%d\n",n,m);

	int ans = calc();

	int two = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 1; j <= n; j ++) {
			add(two, dp[0][i][j][m]);
			add(two, dp[2][i][(n + 1) - j][m]);
			// printf("i=%d,j=%d, now two=%d\n",i,j,two);
		}
	// for (int i = 0; i < 2; i ++) {
	//	for (int j = 1; j <= n; j ++) printf("%c", mp[i][j]);
			// printf("\n");
	// }
	for (int j = 1; j <= n; j ++) {
		if (m == 1) {
			if (mp[0][j] == str[1]) add(two, 1);
			if (mp[1][j] == str[1]) add(two, 1);
		}else if (m == 2) {
			if (mp[0][j] == str[1] && mp[1][j] == str[2]) add(two, 1);
			if (mp[1][j] == str[1] && mp[0][j] == str[2]) add(two, 1);
		}
	}
	add(ans, calc());
	 // printf("ans=%d,two=%d\n",ans,two);
	add(ans, mod - two);

	printf("%d\n", ans);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <assert.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cstring>
using namespace std;

const int mod = (int)1e9 + 7;
const int maxn = 2050;
char mp[2][maxn], str[maxn];
int n, m, dp[3][2][maxn][maxn];
int ext[maxn], mat[maxn][maxn];

void add(int &x, int y) {
	x = (x + y % mod) % mod;
}

void prep(int k) {
	memset(dp[k], 0, sizeof dp[k]);
	for (int t = 0; t < 2; t ++) {
		// printf("k=%d,t=%d\n",k,t);
		for (int i = 1; i <= n; i ++) {
			ext[i] = 0;
			while (ext[i] < min(i, m) && mp[t][i - ext[i]] == str[1 + ext[i]]) ext[i] ++;
			// printf("ext[%d]=%d\n",i,ext[i]);
		}

		memset(mat, 0, sizeof mat);
		for (int i = n; i > 0; i --)
			for (int j = 1; j <= m; j ++) {
				mat[i][j] = str[j] != mp[!t][i] ? 0 : mat[i + 1][j + 1] + 1;
				//if (mat[i][j] != 0)
					// printf("i=%d,j=%d, mat=%d\n",i,j,mat[i][j]);
			}

		for (int j = 0; j <= n; j ++)
			for (int l = 0; l <= m; l ++) {
				if (l == 0) {
					dp[k][!t][j][l] = 1;
				//	printf("dp[%d][%d][%d][%d]=%d\n",k,!t,j,l,dp[k][!t][j][l]);
					continue;
				}
				//[0][1][4][4] t=1
				if (l < 4 || l % 2 || j == 0) continue;
				if (ext[j] >= l / 2 && mat[j - l / 2 + 1][l / 2 + 1] >= l / 2)
					dp[k][!t][j][l] = 1;
			}
	}
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[k][i][j][z] != 0) {
					 // printf("dp[%d][%d][%d][%d]=%d\n",k,i,j,z,dp[k][i][j][z]);
				}
	 // printf("finish\n");
}

void solve() { //dp[1]
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				dp[1][i][j][z] = dp[0][i][j][z];
		for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					 // printf("dp1[%d][%d][%d]=%d\n",i,j,z,dp[1][i][j][z]);
				}
				//0,1,2 -> 1,2,4

		for (int j = 0; j < n; j ++)
		   for (int i = 0; i < 2; i ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					// printf("dp[1][%d][%d][%d] -> =%d\n",i,j,z,dp[1][i][j][z]);
					//goto dp[1][i][j + 1][z + 1]
					if (z + 1 <= m && str[z + 1] == mp[i][j + 1])
						add(dp[1][i][j + 1][z + 1], dp[1][i][j][z]);
					//goto dp[1][!i][j + 1][z + 2]
					if (z + 2 <= m && str[z + 1] == mp[i][j + 1] && str[z + 2] == mp[!i][j + 1])
						add(dp[1][!i][j + 1][z + 2], dp[1][i][j][z]);
				}
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					 // printf("dp1[%d][%d][%d]=%d\n",i,j,z,dp[1][i][j][z]);
				}
	// printf("finish\n");
}

int calc() {
	/*
	printf("now\n");
	for (int i=0;i<2;i++) {
		for (int j=1;j<=n;j++)printf("%c",mp[i][j]);
			printf("\n");
	}
	for (int j=1;j<=m;j++) printf("%c", str[j]);
		printf("\n");
	*/
	prep(0);
	solve();
	reverse(str + 1, str + 1 + m);
	for (int i = 0; i < 2; i ++) reverse(mp[i] + 1, mp[i] + 1 + n);
	prep(2);

	int ans = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++) {
			add(ans, dp[1][i][j][m]);
			if (dp[1][i][j][m]!=0) {
				// printf("add(1,%d,%d,%d)=%d\n",i,j,m,dp[1][i][j][m]);
			}
			for (int z = 0; z < m; z ++) {
				add(ans, 1ll * dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z] % mod);
				// if (1ll * dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z] % mod!=0)
					// printf("add dp[1](i,j)=%d,%d z=%d  * dp[2][i=%d][j'=%d][z'=%d]  =%d\n",
						// i,j,z,i,n+1-(i+1),m-z, dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z]);
			}
		}

	return ans;
}
int main() {
	for (int i = 0; i < 2; i ++) scanf("%s", mp[i]);
	scanf("%s", str);
	n = strlen(mp[0]);
	m = strlen(str);

	for (int i = n; i > 0; i --)
		mp[0][i] = mp[0][i - 1], mp[1][i] = mp[1][i - 1];
	for (int i = m; i > 0; i --)
		str[i] = str[i - 1];

	// printf("n=%d, m=%d\n",n,m);

	int ans = calc();

	int two = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 1; j <= n; j ++) {
			add(two, dp[0][i][j][m]);
			add(two, dp[2][i][(n + 1) - j][m]);
			// printf("i=%d,j=%d, now two=%d\n",i,j,two);
		}
	// for (int i = 0; i < 2; i ++) {
	//	for (int j = 1; j <= n; j ++) printf("%c", mp[i][j]);
			// printf("\n");
	// }
	for (int j = 1; j <= n; j ++) {
		if (m == 1) {
			if (mp[0][j] == str[1]) add(two, 1);
			if (mp[1][j] == str[1]) add(two, 1);
		}else if (m == 2) {
			if (mp[0][j] == str[1] && mp[1][j] == str[2]) add(two, 1);
			if (mp[1][j] == str[1] && mp[0][j] == str[2]) add(two, 1);
		}
	}
	reverse(str + 1, str + 1 + m);
	add(ans, calc());
	// printf("ans=%d,two=%d\n",ans,two);
	add(ans, mod - two);

	printf("%d\n", ans);
	return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <assert.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <cstring>
using namespace std;

const int mod = (int)1e9 + 7;
const int maxn = 2050;
char mp[2][maxn], str[maxn];
int n, m, dp[3][2][maxn][maxn];
int ext[maxn], mat[maxn][maxn];

void add(int &x, int y) {
	x = (x + y % mod) % mod;
}

void prep(int k) {
	memset(dp[k], 0, sizeof dp[k]);
	for (int t = 0; t < 2; t ++) {
		// printf("k=%d,t=%d\n",k,t);
		for (int i = 1; i <= n; i ++) {
			ext[i] = 0;
			while (ext[i] < min(i, m) && mp[t][i - ext[i]] == str[1 + ext[i]]) ext[i] ++;
			// printf("ext[%d]=%d\n",i,ext[i]);
		}

		memset(mat, 0, sizeof mat);
		for (int i = n; i > 0; i --)
			for (int j = 1; j <= m; j ++) {
				mat[i][j] = str[j] != mp[!t][i] ? 0 : mat[i + 1][j + 1] + 1;
				//if (mat[i][j] != 0)
					// printf("i=%d,j=%d, mat=%d\n",i,j,mat[i][j]);
			}

		for (int j = 0; j <= n; j ++)
			for (int l = 0; l <= m; l ++) {
				if (l == 0) {
					dp[k][!t][j][l] = 1;
				//	printf("dp[%d][%d][%d][%d]=%d\n",k,!t,j,l,dp[k][!t][j][l]);
					continue;
				}
				//[0][1][4][4] t=1
				if (l < 4 || l % 2 || j == 0) continue;
				if (ext[j] >= l / 2 && mat[j - l / 2 + 1][l / 2 + 1] >= l / 2)
					dp[k][!t][j][l] = 1;
			}
	}
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[k][i][j][z] != 0) {
					// printf("dp[%d][%d][%d][%d]=%d\n",k,i,j,z,dp[k][i][j][z]);
				}
	// printf("finish\n");
}

void solve() { //dp[1]
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				dp[1][i][j][z] = dp[0][i][j][z];
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j < n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					//goto dp[1][i][j + 1][z + 1]
					if (z + 1 <= m && str[z + 1] == mp[i][j + 1])
						add(dp[1][i][j + 1][z + 1], dp[1][i][j][z]);
					//goto dp[1][!i][j + 1][z + 2]
					if (z + 2 <= m && str[z + 1] == mp[i][j + 1] && str[z + 2] == mp[!i][j + 1])
						add(dp[1][!i][j + 1][z + 2], dp[1][i][j][z]);
				}
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++)
			for (int z = 0; z <= m; z ++)
				if (dp[1][i][j][z] != 0) {
					// printf("dp1[%d][%d][%d]=%d\n",i,j,z,dp[1][i][j][z]);
				}
	// printf("finish\n");
}

int calc() {

	prep(0);
	solve();
	reverse(str + 1, str + 1 + m);
	for (int i = 0; i < 2; i ++) reverse(mp[i] + 1, mp[i] + 1 + n);
	prep(2);

	int ans = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 0; j <= n; j ++) {
			add(ans, dp[1][i][j][m]);
			if (dp[1][i][j][m]!=0) {
				// printf("add(1,%d,%d,%d)=%d\n",i,j,m,dp[1][i][j][m]);
			}
			for (int z = 0; z < m; z ++) {
				add(ans, 1ll * dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z] % mod);
				// if (1ll * dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z] % mod!=0)
					// printf("add dp[1](i,j)=%d,%d z=%d  * dp[2][i=%d][j'=%d][z'=%d]  =%d\n",
						// i,j,z,i,n+1-(i+1),m-z, dp[1][i][j][z] * dp[2][i][n + 1 - (j + 1)][m - z]);
			}
		}

	return ans;
}
int main() {
	for (int i = 0; i < 2; i ++) scanf("%s", mp[i]);
	scanf("%s", str);
	n = strlen(mp[0]);
	m = strlen(str);

	for (int i = n; i > 0; i --)
		mp[0][i] = mp[0][i - 1], mp[1][i] = mp[1][i - 1];
	for (int i = m; i > 0; i --)
		str[i] = str[i - 1];

	// printf("n=%d, m=%d\n",n,m);

	int ans = calc();

	int two = 0;
	for (int i = 0; i < 2; i ++)
		for (int j = 1; j <= n; j ++) {
			add(two, dp[0][i][j][m]);
			add(two, dp[2][i][(n + 1) - j][m]);
			// printf("i=%d,j=%d, now two=%d\n",i,j,two);
		}
	// for (int i = 0; i < 2; i ++) {
	//	for (int j = 1; j <= n; j ++) printf("%c", mp[i][j]);
			// printf("\n");
	// }
	for (int j = 1; j <= n; j ++) {
		if (m == 1) {
			if (mp[0][j] == str[1]) add(two, 1);
			if (mp[1][j] == str[1]) add(two, 1);
		}else if (m == 2) {
			if (mp[0][j] == str[1] && mp[1][j] == str[2]) add(two, 1);
			if (mp[1][j] == str[1] && mp[0][j] == str[2]) add(two, 1);
		}
	}
	add(ans, calc());
	// printf("ans=%d,two=%d\n",ans,two);
	add(ans, mod - two);

	printf("%d\n", ans);
	return 0;
}


#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int P = 1e9 + 7 , N = 2005;
string s[2] , w;
int n , m;
void pp(int &x,int d){x += d;if(x >= P) x -= P;}

typedef vector<vi> Board;
Board r[2] , l[2];
void cal(Board&dp,string &s,string &w){
    dp = Board(n + 1, vi(m + 1, 0));
    for(int i=n-1;i>=0;--i)
        for(int j=m-1;j>=0;--j)
            dp[i][j] = s[i] == w[j] ? 1 + dp[i+1][j+1] : 0;
}
int f[2][N][N];
int solve(int first){
    rep(i,0,2) cal(r[i] , s[i] , w);
    string revw = w;reverse(all(revw));
    rep(i,0,2) cal(l[i] , s[i] , revw);
    memset(f , 0 , sizeof(f));
    int ans = 0;
    rep(i,0,n){
        rep(j,0,2) f[j][i][0] = 1;
        rep(j,0,2) for(int ex=2,up=min(n-i,m/2);ex<=up;++ex)
            if(r[j][i][m - 2*ex] >= ex && l[j^1][i][0] >= ex)
                if(ex * 2 < m || first)
                    pp(ans , f[j][i][m - 2*ex]);
        rep(j,0,2) rep(k,0,m) if(s[j][i] == w[k]){
            pp(f[j][i+1][k+1] , f[j][i][k]);
            if(k + 2 <= m && s[j^1][i] == w[k+1])
                pp(f[j^1][i+1][k+2] , f[j][i][k]);
        }
        rep(j,0,2) for(int ex=2,up=min(i+1,m/2);ex<=up;++ex)
            if(l[j][i-ex+1][m-ex] >= ex && r[j^1][i-ex+1][ex] >= ex)
                if(ex * 2 < m || first)
                    pp(f[j][i+1][ex*2] , 1);
    }
    rep(i,0,n+1) rep(j,0,2) pp(ans , f[j][i][m]);
    return ans;
}

int main(){
    cin >> s[0] >> s[1] >> w;
    n = sz(s[0]);m = sz(w);
    int ans = solve(1);
    if(m > 1){
        rep(i,0,2) reverse(all(s[i]));
        pp(ans , solve(0));
        if(m == 2) rep(i,0,n) rep(j,0,2) if(s[i][j]==w[0]&&s[i][j^1]==w[1])
            pp(ans , P - 1);
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int P = 1e9 + 7 , N = 2005;
string s[2] , w;
int n , m;
void pp(int &x,int d){x += d;if(x >= P) x -= P;}

typedef vector<vi> Board;
Board r[2] , l[2];
void cal(Board&dp,string &s,string &w){
    dp = Board(n + 1, vi(m + 1, 0));
    for(int i=n-1;i>=0;--i)
        for(int j=m-1;j>=0;--j)
            dp[i][j] = s[i] == w[j] ? 1 + dp[i+1][j+1] : 0;
}
int f[2][N][N];
int solve(int first){
    rep(i,0,2) cal(r[i] , s[i] , w);
    string revw = w;reverse(all(revw));
    rep(i,0,2) cal(l[i] , s[i] , revw);
    memset(f , 0 , sizeof(f));
    int ans = 0;
    rep(i,0,n){
        rep(j,0,2) f[j][i][0] = 1;
        rep(j,0,2) for(int ex=2,up=min(n-i,m/2);ex<=up;++ex)
            if(r[j][i][m - 2*ex] >= ex && l[j^1][i][0] >= ex)
                if(ex * 2 < m || first)
                    pp(ans , f[j][i][m - 2*ex]);
        rep(j,0,2) rep(k,0,m) if(s[j][i] == w[k]){
            pp(f[j][i+1][k+1] , f[j][i][k]);
            if(k + 2 <= m && s[j^1][i] == w[k+1])
                pp(f[j^1][i+1][k+2] , f[j][i][k]);
        }
        rep(j,0,2) for(int ex=2,up=min(i+1,m/2);ex<=up;++ex)
            if(l[j^1][i-ex+1][m-ex] >= ex && r[j][i-ex+1][ex] >= ex)
                if(ex * 2 < m || first)
                    pp(f[j][i+1][ex*2] , 1);
    }
    rep(i,0,n+1) rep(j,0,2) pp(ans , f[j][i][m]);
    return ans;
}

int main(){
    cin >> s[0] >> s[1] >> w;
    n = sz(s[0]);m = sz(w);
    int ans = solve(1);
    if(m > 1){
        rep(i,0,2) reverse(all(s[i]));
        pp(ans , solve(0));
        if(m == 2) rep(i,0,2) rep(j,0,n) if(s[i][j]==w[0]&&s[i^1][j]==w[1])
            pp(ans , P - 1);
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int P = 1e9 + 7 , N = 2005;
string s[2] , w;
int n , m;
void pp(int &x,int d){x += d;if(x >= P) x -= P;}

typedef vector<vi> Board;
Board r[2] , l[2];
void cal(Board&dp,string &s,string &w){
    dp = Board(n + 1, vi(m + 1, 0));
    for(int i=n-1;i>=0;--i)
        for(int j=m-1;j>=0;--j)
            dp[i][j] = s[i] == w[j] ? 1 + dp[i+1][j+1] : 0;
}
int f[2][N][N];
int solve(int first){
    rep(i,0,2) cal(r[i] , s[i] , w);
    string revw = w;reverse(all(revw));
    rep(i,0,2) cal(l[i] , s[i] , revw);
    memset(f , 0 , sizeof(f));
    int ans = 0;
    rep(i,0,n){
        rep(j,0,2) f[j][i][0] = 1;
        rep(j,0,2) for(int ex=2,up=min(n-i,m/2);ex<=up;++ex)
            if(r[j][i][m - 2*ex] >= ex && l[j^1][i][0] >= ex)
                if(ex * 2 < m || first)
                    pp(ans , f[j][i][m - 2*ex]);
        rep(j,0,2) rep(k,0,m) if(s[j][i] == w[k]){
            pp(f[j][i+1][k+1] , f[j][i][k]);
            if(k + 2 <= m && s[j^1][i] == w[k+1])
                pp(f[j^1][i+1][k+2] , f[j][i][k]);
        }
        rep(j,0,2) for(int ex=2,up=min(i+1,m/2);ex<=up;++ex)
            if(l[j][i-ex+1][m-ex] >= ex && r[j^1][i-ex+1][ex] >= ex)
                if(ex * 2 < m || first)
                    pp(f[j][i+1][ex*2] , 1);
    }
    rep(i,0,n+1) rep(j,0,2) pp(ans , f[j][i][m]);
    return ans;
}

int main(){
    cin >> s[0] >> s[1] >> w;
    n = sz(s[0]);m = sz(w);
    int ans = solve(1);
    if(m > 1){
        rep(i,0,2) reverse(all(s[i]));
        pp(ans , solve(0));
        if(m == 2) rep(i,0,2) rep(j,0,n) if(s[i][j]==w[0]&&s[i^1][j]==w[1])
            pp(ans , P - 1);
    }
    printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define For(i,j,k) for(int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for(int i=(int)(j);i>=(int)(k);i--)
inline ll read(){
    ll x=0;char ch=getchar();bool f=0;
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    return f?-x:x;
}
void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>=10) write(x/10);putchar(x%10+'0');
}
void writeln(ll x){write(x);puts("");}
void writep(ll x){write(x);putchar(' ');}

int const N=2e3+3;
int const P=131;
int const Mo=1e9+7;
int n,m,ans,res,f[2][N][N],g[2][N][N];
int base[N],Hash[N],Hash1[2][N],Hash2[2][N];
char a[2][N],b[N];

int get1(int l,int r,int i){
	return Hash1[i][r]-Hash1[i][l-1]*base[r-l+1];
}

int get2(int l,int r,int i){
	return Hash2[i][l]-Hash2[i][r+1]*base[r-l+1];
}

int merge(int x,int y,int len){
	return x*base[len]+y;
}

void dp(){
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	//part 1
	for (int i=1;i<=m;i++) Hash[i]=Hash[i-1]*P+b[i]-'a'+1;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			for (int k=j,len=2*(j-k+1);k&&len<=m;k--,len+=2)
				if (merge(get2(k,j,i^1),get1(k,j,i),j-k+1)==Hash[len]){
					if (len==m) res++;
					else g[i][j][len]=1;
				}
	//part 2
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			if (a[i][j]==b[1]) g[i][j][1]=1;
	for (int k=1;k<m;k++)
		for (int i=0;i<=1;i++)
			for (int j=1;j<=n;j++){
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+g[i][j][k])%Mo;
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+f[i][j][k])%Mo;
				if (a[i^1][j]==b[k+1]) g[i^1][j][k+1]=(g[i^1][j][k+1]+f[i][j][k])%Mo;
			}
	//part 3
	for (int i=m;i;i--) Hash[i]=Hash[i+1]*P+b[i]-'a'+1;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++){
			if (a[i][j]==b[m]){
				if (m==1) ans=(ans+1)%Mo;
				else ans=(ans+f[i][j-1][m-1]+g[i][j-1][m-1])%Mo;
			}
			for (int k=j,len=2*(k-j+1);k<=n&&len<=m;k++,len+=2){
				if (merge(get1(j,k,i^1),get2(j,k,i),k-j+1)==Hash[m-len+1]){
					if (len==m&&m!=2) res++;
					else ans=(ans+f[i][j-1][m-len]+g[i][j-1][m-len])%Mo;
				}
			}
		}
}

signed main(){
	scanf("%s",a[0]+1);
	scanf("%s",a[1]+1);
	scanf("%s",b+1);
	n=strlen(a[0]+1);
	m=strlen(b+1);
	base[0]=1;
	for (int i=1;i<=max(n,m);i++) base[i]=base[i-1]*P;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++) Hash1[i][j]=Hash1[i][j-1]*P+a[i][j]-'a'+1;
	for (int i=0;i<=1;i++)
		for (int j=n;j;j--) Hash2[i][j]=Hash2[i][j+1]*P+a[i][j]-'a'+1;
	dp();
	reverse(b+1,b+m+1);
	dp();
	ans=(ans+res/2)%Mo;
	if (m==1) ans>>=1;
	writeln(ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define For(i,j,k) for(int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for(int i=(int)(j);i>=(int)(k);i--)
inline ll read(){
    ll x=0;char ch=getchar();bool f=0;
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    return f?-x:x;
}
void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>=10) write(x/10);putchar(x%10+'0');
}
void writeln(ll x){write(x);puts("");}
void writep(ll x){write(x);putchar(' ');}

int const N=2e3+3;
int const P=233;
int const Mo=1e9+7;
int n,m,ans,res,f[2][N][N],g[2][N][N];
int base[N],Hash[N],Hash1[2][N],Hash2[2][N];
char a[2][N],b[N];

int get1(int l,int r,int i){
	return Hash1[i][r]-Hash1[i][l-1]*base[r-l+1];
}

int get2(int l,int r,int i){
	return Hash2[i][l]-Hash2[i][r+1]*base[r-l+1];
}

int merge(int x,int y,int len){
	return x*base[len]+y;
}

void dp(){
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	//part 1
	for (int i=1;i<=m;i++) Hash[i]=Hash[i-1]*P+b[i]-'a'+1;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			for (int k=j,len=2*(j-k+1);k&&len<=m;k--,len+=2)
				if (merge(get2(k,j,i^1),get1(k,j,i),j-k+1)==Hash[len]){
					if (len==m) res++;
					else g[i][j][len]=1;
				}
	//part 2
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			if (a[i][j]==b[1]) g[i][j][1]=1;
	for (int k=1;k<m;k++)
		for (int i=0;i<=1;i++)
			for (int j=1;j<=n;j++){
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+g[i][j][k])%Mo;
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+f[i][j][k])%Mo;
				if (a[i^1][j]==b[k+1]) g[i^1][j][k+1]=(g[i^1][j][k+1]+f[i][j][k])%Mo;
			}
	//part 3
	for (int i=m;i;i--) Hash[i]=Hash[i+1]*P+b[i]-'a'+1;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++){
			if (a[i][j]==b[m]){
				if (m==1) ans=(ans+1)%Mo;
				else ans=(ans+f[i][j-1][m-1]+g[i][j-1][m-1])%Mo;
			}
			for (int k=j,len=2*(k-j+1);k<=n&&len<=m;k++,len+=2){
				if (merge(get1(j,k,i^1),get2(j,k,i),k-j+1)==Hash[m-len+1]){
					if (len==m&&m!=2) res++;
					else ans=(ans+f[i][j-1][m-len]+g[i][j-1][m-len])%Mo;
				}
			}
		}
}

signed main(){
	scanf("%s",a[0]+1);
	scanf("%s",a[1]+1);
	scanf("%s",b+1);
	n=strlen(a[0]+1);
	m=strlen(b+1);
	base[0]=1;
	for (int i=1;i<=max(n,m);i++) base[i]=base[i-1]*P;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++) Hash1[i][j]=Hash1[i][j-1]*P+a[i][j]-'a'+1;
	for (int i=0;i<=1;i++)
		for (int j=n;j;j--) Hash2[i][j]=Hash2[i][j+1]*P+a[i][j]-'a'+1;
	dp();
	reverse(b+1,b+m+1);
	dp();
	ans=(ans+res/2)%Mo;
	if (m==1) ans>>=1;
	writeln(ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define ull unsigned long long
#define For(i,j,k) for(int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for(int i=(int)(j);i>=(int)(k);i--)
inline ll read(){
    ll x=0;char ch=getchar();bool f=0;
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    return f?-x:x;
}
void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>=10) write(x/10);putchar(x%10+'0');
}
void writeln(ll x){write(x);puts("");}
void writep(ll x){write(x);putchar(' ');}

int const N=2e3+3;
int const P=233;
int const Mo=1e9+7;
int n,m,ans,res,f[2][N][N],g[2][N][N];
ull base[N],Hash[N],Hash1[2][N],Hash2[2][N];
char a[2][N],b[N];

ull get1(int l,int r,int i){
	return Hash1[i][r]-Hash1[i][l-1]*base[r-l+1];
}

ull get2(int l,int r,int i){
	return Hash2[i][l]-Hash2[i][r+1]*base[r-l+1];
}

ull merge(ull x,ull y,int len){
	return x*base[len]+y;
}

void dp(){
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	//part 1
	for (int i=1;i<=m;i++) Hash[i]=Hash[i-1]*P+b[i]-'a';
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			for (int k=j,len=2*(j-k+1);k&&len<=m;k--,len+=2)
				if (merge(get2(k,j,i^1),get1(k,j,i),j-k+1)==Hash[len]){
					if (len==m) res++;
					else g[i][j][len]=1;
				}
	//part 2
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			if (a[i][j]==b[1]) g[i][j][1]=1;
	for (int k=1;k<m;k++)
		for (int i=0;i<=1;i++)
			for (int j=1;j<=n;j++){
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+g[i][j][k])%Mo;
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+f[i][j][k])%Mo;
				if (a[i^1][j]==b[k+1]) g[i^1][j][k+1]=(g[i^1][j][k+1]+f[i][j][k])%Mo;
			}
	//part 3
	for (int i=m;i;i--) Hash[i]=Hash[i+1]*P+b[i]-'a';
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++){
			if (a[i][j]==b[m]){
				if (m==1) ans=(ans+1)%Mo;
				else ans=(ans+f[i][j-1][m-1]+g[i][j-1][m-1])%Mo;
			}
			for (int k=j,len=2*(k-j+1);k<=n&&len<=m;k++,len+=2){
				if (merge(get1(j,k,i^1),get2(j,k,i),k-j+1)==Hash[m-len+1]){
					if (len==m&&m!=2) res++;
					else ans=(ans+f[i][j-1][m-len]+g[i][j-1][m-len])%Mo;
				}
			}
		}
}

signed main(){
	scanf("%s",a[0]+1);
	scanf("%s",a[1]+1);
	scanf("%s",b+1);
	n=strlen(a[0]+1);
	m=strlen(b+1);
	base[0]=1;
	for (int i=1;i<=n;i++) base[i]=base[i-1]*P;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++) Hash1[i][j]=Hash1[i][j-1]*P+a[i][j]-'a';
	for (int i=0;i<=1;i++)
		for (int j=n;j;j--) Hash2[i][j]=Hash2[i][j+1]*P+a[i][j]-'a';
	dp();
	reverse(b+1,b+m+1);
	dp();
	ans=(ans+res/2)%Mo;
	if (m==1) ans>>=1;
	writeln(ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define For(i,j,k) for(int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for(int i=(int)(j);i>=(int)(k);i--)
inline ll read(){
    ll x=0;char ch=getchar();bool f=0;
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=1;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
    return f?-x:x;
}
void write(ll x){
    if(x<0) putchar('-'),x=-x;
    if(x>=10) write(x/10);putchar(x%10+'0');
}
void writeln(ll x){write(x);puts("");}
void writep(ll x){write(x);putchar(' ');}

int const N=2e3+3;
int const P=233;
int const Mo=1e9+7;
int n,m,ans,res,f[2][N][N],g[2][N][N];
int base[N],Hash[N],Hash1[2][N],Hash2[2][N];
char a[2][N],b[N];

int get1(int l,int r,int i){
	return Hash1[i][r]-Hash1[i][l-1]*base[r-l+1];
}

int get2(int l,int r,int i){
	return Hash2[i][l]-Hash2[i][r+1]*base[r-l+1];
}

int merge(int x,int y,int len){
	return x*base[len]+y;
}

void dp(){
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	//part 1
	for (int i=1;i<=m;i++) Hash[i]=Hash[i-1]*P+b[i]-'a';
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			for (int k=j,len=2*(j-k+1);k&&len<=m;k--,len+=2)
				if (merge(get2(k,j,i^1),get1(k,j,i),j-k+1)==Hash[len]){
					if (len==m) res++;
					else g[i][j][len]=1;
				}
	//part 2
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++)
			if (a[i][j]==b[1]) g[i][j][1]=1;
	for (int k=1;k<m;k++)
		for (int i=0;i<=1;i++)
			for (int j=1;j<=n;j++){
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+g[i][j][k])%Mo;
				if (j<n&&a[i][j+1]==b[k+1]) f[i][j+1][k+1]=(f[i][j+1][k+1]+f[i][j][k])%Mo;
				if (a[i^1][j]==b[k+1]) g[i^1][j][k+1]=(g[i^1][j][k+1]+f[i][j][k])%Mo;
			}
	//part 3
	for (int i=m;i;i--) Hash[i]=Hash[i+1]*P+b[i]-'a';
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++){
			if (a[i][j]==b[m]){
				if (m==1) ans=(ans+1)%Mo;
				else ans=(ans+f[i][j-1][m-1]+g[i][j-1][m-1])%Mo;
			}
			for (int k=j,len=2*(k-j+1);k<=n&&len<=m;k++,len+=2){
				if (merge(get1(j,k,i^1),get2(j,k,i),k-j+1)==Hash[m-len+1]){
					if (len==m&&m!=2) res++;
					else ans=(ans+f[i][j-1][m-len]+g[i][j-1][m-len])%Mo;
				}
			}
		}
}

signed main(){
	scanf("%s",a[0]+1);
	scanf("%s",a[1]+1);
	scanf("%s",b+1);
	n=strlen(a[0]+1);
	m=strlen(b+1);
	base[0]=1;
	for (int i=1;i<=n;i++) base[i]=base[i-1]*P;
	for (int i=0;i<=1;i++)
		for (int j=1;j<=n;j++) Hash1[i][j]=Hash1[i][j-1]*P+a[i][j]-'a';
	for (int i=0;i<=1;i++)
		for (int j=n;j;j--) Hash2[i][j]=Hash2[i][j+1]*P+a[i][j]-'a';
	dp();
	reverse(b+1,b+m+1);
	dp();
	ans=(ans+res/2)%Mo;
	if (m==1) ans>>=1;
	writeln(ans);
	return 0;
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;

char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
int read(char *s)
{
	char c=nc(); int len=0;
	for (;!(c>='a' && c<='z');c=nc());
	for (;c>='a' && c<='z';s[++len]=c,c=nc()); s[++len]=0; return len-1;
}

const int P=1e9+7;

void add(int &x,int y)
{
	x+=y;
	if(x>=P) x-=P;
	return;
}

long long S=31,MOD=987654321,seed[2010];

void Pre(int n)
{
	seed[0]=1;
	for(int i=1;i<=n;i++)
	  seed[i]=seed[i-1]*S%MOD;
	return;
}

struct Hash
{
	long long h[2010];
	void make(int n,char *s)
	{
		h[0]=0;
		for (int i=1;i<=n;i++)
			h[i]=(h[i-1]*S+s[i]-'a')%MOD;
	}
	long long cut(int l,int r)
	{
		return (h[r]+MOD-h[l-1]*seed[r-l+1]%MOD)%MOD;
	}
}pre[2],suf[2],ss;

int i,j,n,m,ans,f[2][2010][2010];
char a[2][2010],s[2010];

int Solve(int flag)
{
	int ret=0;
	memset(f,0,sizeof(f));
	for(int j=1;j<=n;j++)
	{
		f[0][j][0]=f[1][j][0]=1;
		for(int i=0;i<2;i++)
		  for(int k=2;k<=min(n-j+1,m/2);k++)
			if(ss.cut(m-2*k+1,m-k)==pre[i].cut(j,j+k-1) && ss.cut(m-k+1,m)==suf[i^1].cut(n-(j+k-1)+1,n-j+1))
			  if(2*k!=m || flag)
				add(ret,f[i][j][m-2*k]);
		for(int i=0;i<2;i++)
		  for(int k=2;k<=min(j,m/2);k++)
			if(ss.cut(k+1,2*k)==pre[i].cut(j-k+1,j) && ss.cut(1,k)==suf[i^1].cut(n-j+1,n-(j-k+1)+1))
			  if(2*k!=m || flag)
				add(f[i][j+1][2*k],1);
		for(int i=0;i<2;i++)
		  for(int k=0;k<m;k++)
			if(a[i][j]==s[k+1])
			{
				add(f[i][j+1][k+1],f[i][j][k]);
				if (k+2<=m && a[i^1][j]==s[k+2])
				add(f[i^1][j+1][k+2],f[i][j][k]);
			}
		for(int i=0;i<2;i++)
		  add(ret,f[i][j+1][m]);
	}
	return ret;
}

int main()
{
	freopen("dfc.in","r",stdin);
	freopen("dfc.out","w",stdout);
	Pre(2000);
	n=read(a[0]);
	read(a[1]);
	for(i=0;i<2;i++)
	{
		pre[i].make(n,a[i]);
		reverse(a[i]+1,a[i]+n+1);
		suf[i].make(n,a[i]);
		reverse(a[i]+1,a[i]+n+1);
	}
	m=read(s);
	ss.make(m,s);
	add(ans,Solve(1));
	if (m>1)
	{
		reverse(s+1,s+m+1);
		ss.make(m,s);
		add(ans,Solve(0));
		if(m==2)
		  for(j=1;j<=n;j++)
			for(i=0;i<2;i++)
			  if((a[i][j]==s[1])&&(a[i^1][j]==s[2]))
				add(ans,P-1);
	}
	cout<<ans;
	return 0;
}


#pragma GCC optimize(3)
#include<bits/stdc++.h>
using namespace std;

char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
int read(char *s)
{
	char c=nc(); int len=0;
	for (;!(c>='a' && c<='z');c=nc());
	for (;c>='a' && c<='z';s[++len]=c,c=nc()); s[++len]=0; return len-1;
}

const int P=1e9+7;

void add(int &x,int y)
{
	x+=y;
	if(x>=P) x-=P;
	return;
}

long long S=31,MOD=987654321,seed[2010];

void Pre(int n)
{
	seed[0]=1;
	for(int i=1;i<=n;i++)
	  seed[i]=seed[i-1]*S%MOD;
	return;
}

struct Hash
{
	long long h[2010];
	void make(int n,char *s)
	{
		h[0]=0;
		for (int i=1;i<=n;i++)
			h[i]=(h[i-1]*S+s[i]-'a')%MOD;
	}
	long long cut(int l,int r)
	{
		return (h[r]+MOD-h[l-1]*seed[r-l+1]%MOD)%MOD;
	}
}pre[2],suf[2],ss;

int i,j,n,m,ans,f[2][2010][2010];
char a[2][2010],s[2010];

int Solve(int flag)
{
	int ret=0;
	memset(f,0,sizeof(f));
	for(int j=1;j<=n;j++)
	{
		f[0][j][0]=f[1][j][0]=1;
		for(int i=0;i<2;i++)
		  for(int k=2;k<=min(n-j+1,m/2);k++)
			if(ss.cut(m-2*k+1,m-k)==pre[i].cut(j,j+k-1) && ss.cut(m-k+1,m)==suf[i^1].cut(n-(j+k-1)+1,n-j+1))
			  if(2*k!=m || flag)
				add(ret,f[i][j][m-2*k]);
		for(int i=0;i<2;i++)
		  for(int k=2;k<=min(j,m/2);k++)
			if(ss.cut(k+1,2*k)==pre[i].cut(j-k+1,j) && ss.cut(1,k)==suf[i^1].cut(n-j+1,n-(j-k+1)+1))
			  if(2*k!=m || flag)
				add(f[i][j+1][2*k],1);
		for(int i=0;i<2;i++)
		  for(int k=0;k<m;k++)
			if(a[i][j]==s[k+1])
			{
				add(f[i][j+1][k+1],f[i][j][k]);
				if (k+2<=m && a[i^1][j]==s[k+2])
				add(f[i^1][j+1][k+2],f[i][j][k]);
			}
		for(int i=0;i<2;i++)
		  add(ret,f[i][j+1][m]);
	}
	return ret;
}

int main()
{
//	freopen("dfc.in","r",stdin);
//	freopen("dfc.out","w",stdout);
	Pre(2000);
	n=read(a[0]);
	read(a[1]);
	for(i=0;i<2;i++)
	{
		pre[i].make(n,a[i]);
		reverse(a[i]+1,a[i]+n+1);
		suf[i].make(n,a[i]);
		reverse(a[i]+1,a[i]+n+1);
	}
	m=read(s);
	ss.make(m,s);
	add(ans,Solve(1));
	if (m>1)
	{
		reverse(s+1,s+m+1);
		ss.make(m,s);
		add(ans,Solve(0));
		if(m==2)
		  for(j=1;j<=n;j++)
			for(i=0;i<2;i++)
			  if((a[i][j]==s[1])&&(a[i^1][j]==s[2]))
				add(ans,P-1);
	}
	cout<<ans;
	return 0;
}