Problem p02684 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define sz(a) (int)a.size()
#define re return
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define gr greater<int>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

using namespace std;

typedef long long ll;
typedef string str;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;

ll n, k, a[200001], ans = 1, b[200001];
vll v;
set<ll> s;

///!!!<3<3<3<3<3!!!
void ProdamGaraje() {
  cin >> n >> k;
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  if (n >= k) {
    for (ll i = 0; i < k; ++i)
      ans = a[ans - 1];
    cout << ans;
    re;
  }
  v.pb(1);
  while (1) {
    s.insert(a[ans - 1]);
    ans = a[ans - 1];
    v.pb(ans);
    if (s.find(a[ans - 1]) != s.end())
      break;
  }
  for (ll i = 0; i < sz(v); ++i) {
    b[i] = a[ans - 1];
    ans = a[ans - 1];
  }
  ll d = sz(v) - 1;
  while (b[d] != b[0])
    d--;
  ans = 1;
  while (b[0] != ans) {
    k--;
    ans = a[ans - 1];
  }
  cout << b[k % d];
}
///!!!<3<3<3<3<3!!!
int main() {
  // freopen ("input.txt", "r", stdin);
  // freopen ("output.txt", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  // cin >> tt;
  while (tt--)
    ProdamGaraje();
}
/// GOOOOOOSE!!!!
/*
     ▄▀▀▀▄
▄███▀░0░░░▌
    ▌░░░░░▐
    ▌░░░░░▐
     ▌▌ ▌▌
     ▌▌ ▌▌
     ▌▌ ▌▌
     ▌▌ ▌▌
     ▌▌ ▌▌
   ▄▄▌▌▄▌▌        */


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define sz(a) (int)a.size()
#define re return
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define gr greater<int>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("fast-math")

using namespace std;

typedef long long ll;
typedef string str;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<int> vi;

ll n, k, a[200001], ans = 1, b[200001];
vll v;
set<int> s;

///!!!<3<3<3<3<3!!!
void ProdamGaraje() {
  cin >> n >> k;
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  v.pb(1);
  while (1) {
    s.insert(a[ans - 1]);
    ans = a[ans - 1];
    v.pb(ans);
    if (s.find(a[ans - 1]) != s.end())
      break;
  }
  for (ll i = 0; i < sz(v); ++i) {
    b[i] = a[ans - 1];
    ans = a[ans - 1];
  }
  ll d = sz(v) - 1;
  while (b[d] != b[0])
    d--;
  ans = 1;
  while (b[0] != ans) {
    k--;
    ans = a[ans - 1];
  }
  cout << b[k % d];
}
///!!!<3<3<3<3<3!!!
int main() {
  // freopen ("input.txt", "r", stdin);
  // freopen ("output.txt", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  // cin >> tt;
  while (tt--)
    ProdamGaraje();
}
/// GOOOOOOSE!!!!
/*
     ▄▀▀▀▄
▄███▀░0░░░▌
    ▌░░░░░▐
    ▌░░░░░▐
     ▌▌ ▌▌
     ▌▌ ▌▌
     ▌▌ ▌▌
     ▌▌ ▌▌
     ▌▌ ▌▌
   ▄▄▌▌▄▌▌        */