#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <memory.h>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <ctime>
#include <iostream>
#include <functional>
#include <complex>
#include <stdlib.h>
#include <fstream>
#include <random>
#include <csignal>
#include <chrono> 
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <limits>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<pii, int> p3i;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<p3i> v3i;
typedef vector<vii> vvii;
typedef vector<p3i> vp3i;
typedef long double ld;
typedef vector<ld> vld;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define REPD(i, n) for (int (i) = (n) - 1; (i) >= 0; (i)--)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define FORD(i,a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define rv(v) reverse(all(v))
#define CL(v, val) memset((v), (val), sizeof((v)))
#define SORT(a) sort(all(a))
#define un(v) SORT(v), (v).resize(unique(all(v)) - (v).begin())
#define eps 1.0e-7
#define X first
#define Y second
#define bit(n) (1 << (n))	
#define bit64(n) (ll(1) << (n))
#define sqr(x) ((x) * (x))
#define N 200005

vector<vi> g;

int c[N];

int cycles = 0;

void dfs(int v) {
	c[v] = 1;

	REP(i, sz(g[v])) {
		int u = g[v][i];

		if (c[u] == 1) {
			cycles++;
		} else
			if (c[u] == 0) {
				dfs(u);
			}
	}

	c[v] = 2;
}

ll mod = 998244353ll;

ll binpow(ll a, ll n) {
	ll res = 1;
	while (n) {
		if (n & 1) {
			res = (res * a) % mod;
		}

		a = (a * a) % mod;
		n /= 2;
	}

	return res;
}

int main(void) {
	CL(c, 0);
	int n;
	scanf("%d", &n);
	g.resize(n);
	REP(i, n) {
		int x;
		scanf("%d", &x);
		x--;
		g[i].pb(x);
	}

	REP(i, n) {
		if (c[i] == 0) {
			dfs(i);
		}
	}

	ll res = binpow(2ll, cycles);
	res = (res - 1 + mod) % mod;
	printf("%lld\n", res);
}

#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#include <map>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define reps(i, s, n) for (int i = s; i < (int)(n); ++i)
#define print(x) cout << x << "\n"
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))

typedef long long ll;

using namespace std;
using vi = vector<int>;
using vl = vector<ll>;
using Graph = vector<vector<int> >;

int main(){
    ll n; cin >> n;
    // cout << n << endl;
    vector<int> a;
    while(n>10){
        int b;
        b = n%10;
        a.push_back(b);
        n /= 10;
    }
    a.push_back(n);
    int bs;
    bs = a.size();
    // rep(i,bs) cout << a[i] << endl;
    int amod = accumulate(ALL(a),0);
    amod %= 3;
    if(amod==0) cout << 0 << endl;
    if(amod==1&&bs!=1){
        int aa=0;
        bool bb=false;
        rep(i,bs){
            if(a[i]%3==1) {cout << 1 << endl;bb=true;break;}
            else if(a[i]%3==2) aa++;
        }
        if(!bb){
            if(aa==bs) cout << -1 << endl;
            else if(aa==1) cout << 1 << endl;
            else cout << 2 << endl;
        }
    }
    if(amod==1 && bs==1) cout << -1 << endl;
    if(amod==2 && bs!=1){
        int aa=0;
        bool bb=false;
        rep(i,bs){
            if(a[i]%3==2) {cout << 1 << endl;bb=true;break;}
            if(a[i]%3==1) aa++;
        }
        if(!bb){
            if(aa == bs) cout << -1 << endl;
            else cout << 2 << endl;
        }
    }
    if(amod==2&&bs==1) cout << -1 << endl;

}