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

#include <bits/stdc++.h>
using ll = long long;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()

#define INF (ll)1e9 // 10^9:∞
#define LLINF (ll)1e12
#define MOD (ll)(1e9 + 7) // 10^9+7:合同式の法
#define PI 3.141592653589
#define PB push_back
#define F first
#define S second
#define __MAGIC__                                                              \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);

#define YESNO(T)                                                               \\
  if (T) {                                                                     \\
    cout << "YES" << endl;                                                     \\
  } else {                                                                     \\
    cout << "NO" << endl;                                                      \\
  }
#define yesno(T)                                                               \\
  if (T) {                                                                     \\
    cout << "yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "no" << endl;                                                      \\
  }
#define YesNo(T)                                                               \\
  if (T) {                                                                     \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }

#define Graph vector<vector<int>>
#define PII pair<int, int>
#define VI vector<int>
#define VVI vector<vector<int>>
#define VPII vector<pair<int, int>>

#define DDD fixed << setprecision(10)
#define endl "\\n"

using namespace std;
/*..................DEFINE GLOBAL VARIABLES...................*/

/*.....................DEFINE FUNCTIONS ......................*/

/*.........................kemkemG0...........................*/
signed main() {
  __MAGIC__

  ll N, K;
  cin >> N >> K;

  vector<int> a(N);
  REP(i, N) cin >> a[i];

  vector<int> seen(N + 1, -1); // seen[i]:=街iに来た番数
  vector<int> repeat;

  int c = 1, l = 0;

  {
    int v = 1;
    while (seen[v] == -1) {
      seen[v] = repeat.size();
      repeat.emplace_back(v);
      v = a[v - 1]; // 0indexだからv-1
    }
    c = repeat.size() - seen[v];
    l = seen[v];
  }

  if (K < l)
    cout << repeat[K] << endl;
  else {
    K -= l;
    K %= c;
    cout << repeat[l + K] << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using ll = long long;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()

#define INF (ll)1e9 // 10^9:∞
#define LLINF (ll)1e12
#define MOD (ll)(1e9 + 7) // 10^9+7:合同式の法
#define PI 3.141592653589
#define PB push_back
#define F first
#define S second
#define __MAGIC__                                                              \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(nullptr);

#define YESNO(T)                                                               \\
  if (T) {                                                                     \\
    cout << "YES" << endl;                                                     \\
  } else {                                                                     \\
    cout << "NO" << endl;                                                      \\
  }
#define yesno(T)                                                               \\
  if (T) {                                                                     \\
    cout << "yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "no" << endl;                                                      \\
  }
#define YesNo(T)                                                               \\
  if (T) {                                                                     \\
    cout << "Yes" << endl;                                                     \\
  } else {                                                                     \\
    cout << "No" << endl;                                                      \\
  }

#define Graph vector<vector<int>>
#define PII pair<int, int>
#define VI vector<int>
#define VVI vector<vector<int>>
#define VPII vector<pair<int, int>>

#define DDD fixed << setprecision(10)
#define endl "\\n"

using namespace std;
/*..................DEFINE GLOBAL VARIABLES...................*/

/*.....................DEFINE FUNCTIONS ......................*/

/*.........................kemkemG0...........................*/
signed main() {
  __MAGIC__

  ll N, K;
  cin >> N >> K;

  vector<int> a(N);
  REP(i, N) cin >> a[i];

  vector<int> seen(N + 1, -1); // seen[i]:=街iに来た番数
  vector<int> repeat;

  int c = 1, l = 0;

  {
    int v = 1;
    while (seen[v] == -1) {
      seen[v] = repeat.size();
      repeat.emplace_back(v);
      v = a[v - 1]; // 0indexだからv-1
    }
    c = seen.size() - repeat[v];
    l = repeat[v];
  }

  if (K < l)
    cout << seen[K] << endl;
  else {
    K -= 1;
    K %= c;
    cout << seen[l + K] << endl;
  }

  return 0;
}