#include "bits/stdc++.h"

using namespace std;

using vi = vector<int>;

using vvi = vector<vi>;

#define pb push_back

#define sz size()

#define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i))

void solve() {
    int n, m; cin >> n >> m;
    vi v(n); vvi track(1500001);
    ice (i, 0, 1500001) {
        track[i].pb(-1);
    }
    ice (i, 0, n) {
        cin >> v[i];
        track[v[i]].pb(i);
    }
    ice (i, 0, 1500001) {
        track[i].pb(n);
    }
    ice (i, 0, 1500001) {
        bool ok = true;
        ice (j, 1, track[i].sz) {
            ok &= (track[i][j] - track[i][j - 1] <= m);
        }
        if (!ok) {
            cout << i;
            return;
        }
    }
}

int main() {
    solve();
}
#include <bits/stdc++.h>
using namespace std;
#define poi 400100
typedef long long ll;
typedef double db;
const int mod = 998244353;
int f[poi];
inline int re()
{
    char c = getchar();
    int x = 0, k = 1;
    while (c < '0' || c > '9')
    {
        if (c == '-')
            k = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
        x = (x << 1) + (x << 3) + c - '0', c = getchar();
    return k * x;
}
void wr(int x)
{
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        wr(x / 10);
    putchar(x % 10 + '0');
}
int fin(int x) { return f[x] == x ? x : f[x] = fin(f[x]); }
inline void bin(int x, int y)
{
    x = fin(x), y = fin(y);
    f[x] = y;
}
inline int ksm(int x, int y)
{
    int ans = 1;
    for (; y; y >>= 1)
    {
        if (y & 1)
            ans = ((ll)ans * x) % mod;
        x = ((ll)x * x) % mod;
    }
    return ans;
}
signed main()
{
    int n = re(), tot = 0;
    for (int i = 1; i <= n; i++)
        f[i] = i;
    for (int i = 1; i <= n; i++)
    {
        int qqq = re();
        bin(i, qqq);
    }
    for (int i = 1; i <= n; i++)
        if (fin(i) == i)
            tot++;
    wr(ksm(2, tot) - 1);
    return 0;
}