Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Monotonic Renumeration

#include <atcoder/modint>
#include <bits/stdc++.h>

using namespace std;
using namespace atcoder;

using mint = modint998244353;

void solve(vector<int> &a) {
    int n = a.size();

    set<int> unlocked, locked;
    locked.insert(0);
    for (int i = 1; i < n; i++) {
        unlocked.insert(i);
    }

    map<int, int> last_occ;
    for (int i = 0; i < n; i++) {
        last_occ[a[i]] = i;
    }

    for (int i = 0; i < n; i++) {
        auto itr = unlocked.upper_bound(i);
        while (itr != unlocked.end() && *itr <= last_occ[a[i]]) {
            locked.insert(*itr);
            itr = unlocked.erase(itr);
        }
    }

    int sz = unlocked.size();
    cout << mint(2).pow(sz).val() << "\n";
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    solve(a);
    return 0;
}