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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, b, a) for(int i = b - 1; i >= a; i--)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef unsigned long long ull;

unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 eng(seed);

ll random2(){
    return (1ll << 31ll)*eng()+eng();
}

ll n,m,k,q,T;

const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod =  998244353;

const int MAXN = 200001;

vector<vi> C(MAXN, vi());
bool mark[MAXN] = {0};

void dfs(ll i){
    if(mark[i])return;
    mark[i] = 1;
    trav(y, C[i]){
        dfs(y);
    }
}

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

    //freopen("fhc.txt","r",stdin);
    //freopen("autput.txt","w",stdout);

    ll a,b,c,d;

    cin >> n;
    rep(c1,0,n){
        cin >> a;
        a--;
        C[a].push_back(c1);
        C[c1].push_back(a);
    }
    ll ans = 1;
    rep(c1,0,n){
        if(!mark[c1]){
            dfs(c1);
            ans *= 2;
            ans %= mod;
        }
    }

    cout << (ans+mod-1)%mod << "\n";

    return 0;
}

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define visit asdf
const int N=2e5+5;

int n;
int a[N],b[N],c[N],p[N];
vector< vector<int> > v;
bool visit[N];

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",a+i);
	for(int i=1;i<=n;i++) scanf("%d",b+i);
	for(int i=1;i<=n;i++) scanf("%d",p+i);
	for(int i=1;i<=n;i++)
		if(a[i]<=b[p[i]] && p[i]!=i) return puts("-1"),0;
	for(int i=1;i<=n;i++) c[i]=b[p[i]];
	for(int i=1;i<=n;i++) b[i]=c[i];
	
	for(int i=1;i<=n;i++)
	{
		if(visit[i]) continue;
		vector<int> vec;
		vec.pb(i); visit[i]=1;
		int j=i;
		while(p[j]!=i)
		{
			vec.pb(p[j]);
			visit[p[j]]=1;
			j=p[j];
		}
		v.pb(vec);
	}
	printf("%d\n",n-v.size());
	for(int ii=0;ii<v.size();ii++)
	{
		vector<int> vec=v[ii];
		int mn=1e9,u;
		for(int i=0;i<vec.size();i++)
			mn=min(mn,b[vec[i]]);
		for(int i=0;i<vec.size();i++)
			if(b[vec[i]]==mn) u=i;
		for(int i=vec.size()+u;i>u+1;i--)
			printf("%d %d\n",vec[i%vec.size()],vec[(i-1)%vec.size()]);
	}
	
	return 0;
}