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

#include <bits/stdc++.h>

#define debug(a) cout << #a << " " << a << endl;
#define _debug(a, i)                                                           \\
  cout << #a << "[" << i << "]"                                                \\
       << " = " << a[i] << endl;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(arr) ll(arr.size())

using namespace std;

using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 9;
const double PI = 4.0 * atan(1.0);

ll powmod(ll a, ll b, const ll MOD) {
  ll res = 1;
  a %= MOD;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll n, const ll p) { return powmod(n, p - 2, p); }
ll div_floor(ll a, ll b) { return a / b - (((a ^ b) < 0) and a % b); }
ll div_ceil(ll a, ll b) { return a / b + (((a ^ b) >= 0) and a % b); }
ll kCn(ll k, ll n, ll f[]) {
  return (f[n] * modInverse((f[n - k] * f[k]) % MOD, MOD)) % MOD;
}

int main() {
  // #ifndef ONLINE_JUDGE
  //	(void)freopen("input.txt", "r", stdin);
  //	(void)freopen("output.txt", "w", stdout);
  // #endif // !ONLINE_JUDGE
  fastio;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int pos = 1;
  map<ll, ll> mp;
  ll len = 0;
  ll i;
  mp[1] = 0;
  for (i = 1; i <= k; ++i) {
    pos = a[pos];
    if (i == k) {
      cout << pos;
      return 0;
    }
    if (mp[pos]) {
      len = i - mp[pos];
      break;
    } else
      mp[pos] = i;
  }
  k -= mp[pos];
  k %= len;
  for (i = 0; i < k; ++i) {
    pos = a[pos];
  }

  cout << pos;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define debug(a) cout << #a << " " << a << endl;
#define _debug(a, i)                                                           \\
  cout << #a << "[" << i << "]"                                                \\
       << " = " << a[i] << endl;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(arr) ll(arr.size())

using namespace std;

using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 9;
const double PI = 4.0 * atan(1.0);

ll powmod(ll a, ll b, const ll MOD) {
  ll res = 1;
  a %= MOD;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return (a * b) / gcd(a, b); }
ll modInverse(ll n, const ll p) { return powmod(n, p - 2, p); }
ll div_floor(ll a, ll b) { return a / b - (((a ^ b) < 0) and a % b); }
ll div_ceil(ll a, ll b) { return a / b + (((a ^ b) >= 0) and a % b); }
ll kCn(ll k, ll n, ll f[]) {
  return (f[n] * modInverse((f[n - k] * f[k]) % MOD, MOD)) % MOD;
}

ll dp[200005];

int main() {
  // #ifndef ONLINE_JUDGE
  //	(void)freopen("input.txt", "r", stdin);
  //	(void)freopen("output.txt", "w", stdout);
  // #endif // !ONLINE_JUDGE
  fastio;
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  int pos = 1;
  map<ll, ll> mp;
  ll len = 0;
  ll i;
  mp[1] = 0;
  for (i = 1; i <= k; ++i) {
    pos = a[pos];
    if (mp[pos]) {
      len = i - mp[pos];
      k -= mp[pos];
      break;
    }
    mp[pos] = i;
  }
  k %= len;
  for (i = 0; i < k; ++i) {
    pos = a[pos];
  }

  cout << pos;
  return 0;
}