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

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int ans = 0;
int N, M, Q;

template <typename T> void showall(vector<T> v) {
  rep(i, 0, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

void dfs(vector<ll> ls, vector<vector<ll>> way, int) {
  ll cur = ls.back();
  ls.pop_back();

  rep(i, 0, N) {
    if (way[cur][i] != 0) {
    }
  }
}

int main() {
  ll N, K;

  cin >> N >> K;

  vector<ll> A(N);
  rep(i, 0, N) { cin >> A[i]; }
  vector<int> B(N, -1);
  ll now = 1, init, cnt = 0;
  B[now - 1] = 0;
  ll mod = 0;

  while (true) {
    cnt++;
    now = A[now - 1];
    if (B[now - 1] == -1) {
      B[now - 1] = cnt;
    } else {
      mod = cnt - B[now - 1];
      init = B[now - 1];
      break;
    }
  }

  if (K < init) {
    now = 1;
    while (true) {
      if (K == 0) {
        break;
      }
      now = A[now - 1];
      K--;
    }
    cout << now << endl;
    return 0;
  }

  K -= init;
  K %= mod;

  while (true) {
    if (K == 0) {
      break;
    }
    now = A[now - 1];
    K--;
  }

  cout << now << endl;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

using ll = long long int;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()

#define pi 3.14159265358979

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll cnt_rep = 0;
ll powmod(ll N, ll P, ll M) {
  cnt_rep++;
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = powmod(N, P / 2, M);
    return t * t % M;
  }
  return (N * powmod(N, P - 1, M)) % M;
}

ll gcd(ll a, ll b) {
  ll resi, g, l;
  if (a > b) {
    g = a;
    l = b;
  } else if (b > a) {
    g = b;
    l = a;
  } else {
    return a;
  }
  resi = g % l;
  if (resi != 0) {
    return gcd(l, resi);
  } else {
    return l;
  }
}

template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

int ans = 0;
int N, M, Q;

template <typename T> void showall(vector<T> v) {
  rep(i, 0, v.size()) { cout << v[i] << " "; }
  cout << endl;
}

void dfs(vector<ll> ls, vector<vector<ll>> way, int) {
  ll cur = ls.back();
  ls.pop_back();

  rep(i, 0, N) {
    if (way[cur][i] != 0) {
    }
  }
}

int main() {
  ll N, K;

  cin >> N >> K;

  vector<ll> A(N);
  rep(i, 0, N) { cin >> A[i]; }
  vector<int> B(N, 0);
  ll now = 1, init, cnt = 0;
  B[now - 1] = 1;
  ll mod = 0;

  while (true) {
    cnt++;
    now = A[now - 1];
    if (B[now - 1] == 0) {
      B[now - 1] = cnt;
    } else {
      mod = cnt - B[now - 1];
      init = B[now - 1];
      break;
    }
  }

  K -= init;
  K %= mod;

  while (true) {
    if (K == 0) {
      break;
    }
    now = A[now - 1];
    K--;
  }

  cout << now << endl;
}