#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;

int nt[200009], id[200009], ds[200009];
bool rm[200009];
queue<int> q;

int uf(int n)
{
	if (ds[n] == n) return n;
	return ds[n] = uf(ds[n]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> nt[i];
		id[nt[i]]++;
		ds[i] = i;
	}
	for (int i = 1; i <= n; i++) 
		if (!id[i]) {
			rm[i] = true;
			q.push(i);
		}
	while (!q.empty()) {
		int hn = q.front(); q.pop();
		id[nt[hn]]--;
		if (!id[nt[hn]]) {
			rm[nt[hn]] = true;
			q.push(nt[hn]);
		}
	}
	for (int i = 1; i <= n; i++)
		if (!rm[i]) {
			int ua = uf(i), ub = uf(nt[i]);
			if (ua != ub)
				ds[ua] = ub;
		}
	int c = 0;
	for (int i = 1; i <= n; i++)
		if (!rm[i] && ds[i] == i)
			c++;
	long long ans = 1, md = 998244353;
	for (int i = 0; i < c; i++)
		ans = (ans * 2) % md;
	ans = (ans - 1 + md) % md;
	cout << ans << '\n';
	return 0;
}

#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	if ((tmp & ma0) ^ ma0) {
		int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
		tmp = tmp << dig & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += (72 - dig >> 3);
	}
	else {
		tmp = tmp & ma1;
		tmp = tmp * 10 + (tmp >> 8) & ma2;
		tmp = tmp * 100 + (tmp >> 16) & ma3;
		tmp = tmp * 10000 + (tmp >> 32) & ma4;
		ci += 8;
		if ((ct = *ci++) >= '0') {
			tmp = tmp * 10 + ct - '0';
			if ((ct = *ci++) >= '0') {
				tmp = tmp * 10 + ct - '0';
				ci++;
			}
		}
	}
	return tmp;
}
ll tmp[400010];
void pakuri_sort(int N, ll A[]) {
	const int b = 8;
	rep(k, 4) {
		int kazu[1 << b] = {}, kazu2[1 << b] = {};
		rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
		for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
		k++;
		rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
		rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
		for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
	}
}

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


	int N = getint(), M = getint();
	ll XY[200000];
	rep(i, M) {
		int x = getint();
		XY[i] = x | (ll)getint() << 32;
	}
	pakuri_sort(M, XY);

	auto ST = tmp + 200000;
	ST[0] = -1;
	const ll ma = (1ll << 32) - 1;
	int m = 0;
	while (m < M) {
		int m0 = m;
		int x = (XY[m] & ma);
		while (m < M && (XY[m] & ma) == x) {
			int y = (XY[m] >> 32) - N;
			if (y < -M || y > M) XY[m] = 0;
			else if (ST[y - 1] < 0 | ST[y + 1] < 0) XY[m] |= 1ll;
			else if (ST[y] < 0) XY[m] &= (~1ll);
			else XY[m] = 0;
			m++;
		}
		for (int p = m0; p < m; p++) {
			int y = (XY[p] >> 32) - N;
			if (XY[p] & 1) ST[y] = -1;
			else if (XY[p]) ST[y] = 0;

		}
	}
	int kotae = 0;
	for (int i = -M; i <= M; i++) kotae += (ST[i] < 0);
	co(kotae);

	Would you please return 0;
}