#include <bits/stdc++.h>
using namespace std;
int A[105],B[105];
int main(){
    int n,a,b,ans;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>A[i];
    }
    for(int i=0;i<n;i++){
        cin>>B[i];
    }
    sort(A,A+n);
    a=A[n-1];
    sort(B,B+n);
    b=B[0];
    ans=b-a+1;
    if(ans>=0)cout<<ans;
    else cout<<0;
}

#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T, class ...U> void kout (T v, U ...e) { cerr << v << ' ', kout(e...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 300010;

const int in_t = 1, out_t = 2, no_t = 0, p = 998244353;

int vis[MAX_N], nxt[MAX_N], n;

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

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

	ll res = 1;

	for (int i = 1;i <= n;++i) {
		vector<int> stk;
		bool bad = false;
		for (int x = i;;x = nxt[x]) {
			if (vis[x] == out_t) {
				bad = true;
				break;
			}
			if (vis[x] == in_t) break;
			stk.pb(x);
			vis[x] = in_t;
		}
		if (!bad) {
			res = res * 2 % p;
		}
		for (int u : stk)
			vis[u] = out_t;
	}

	--res;
	if (res < 0) res += p;

	cout << res << '\n';
}
