Problem p02737 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rs resize
#define DBG(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

const ld PI = acos(-1.0);
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> int sz(const T &x) {
  return (int)x.size(); // copy the ampersand(&)!
}

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "\n[";
  for (T &x : v)
    os << x << ',';
  return os << "]\n";
}

struct pairhash {
public:
  template <typename T1, typename T2>
  size_t operator()(const pair<T1, T2> &p) const {
    size_t lhs = hash<T1>()(p.x);
    size_t rhs = hash<T2>()(p.y);
    return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
  }
};

const ll MOD = 998244353;
const ll BASE = 1000000000000000000 % MOD;

// vector<vvi> adj;
vvi nim;

void run() {
  ll N;
  cin >> N;

  ll maxNim = 0;
  for (ll i = 0; i < 3; i++) {
    vvi adj(N);
    nim.pb(vi(N, 0));
    ll M;
    cin >> M;
    for (ll j = 0; j < M; j++) {
      ll a, b;
      cin >> a >> b;
      a--;
      b--;
      if (a > b)
        swap(a, b);
      adj[a].pb(b);
    }

    for (ll j = N - 1; j >= 0; j--) {
      vector<bool> U(adj[j].size() + 2, false);
      for (ll k : adj[j]) {
        U[nim[i][k]] = true;
      }
      for (ll k = 0; k < U.size(); k++) {
        if (!U[k]) {
          // cout << j << ": " << k << endl;
          nim[i][j] = k;
          break;
        }
      }
      maxNim = max(maxNim, nim[i][j] + 1);
    }
  }

  vvi nimScore(3, vi(maxNim, 0));
  for (ll i = 0; i < 3; i++) {
    ll p = 1;
    for (ll j = 0; j < N; j++) {
      p *= BASE;
      p %= MOD;
      nimScore[i][nim[i][j]] += p;
      nimScore[i][nim[i][j]] %= MOD;
    }
  }

  ll res = 0;
  for (ll i1 = 0; i1 < maxNim; i1++) {
    for (ll i2 = 0; i2 < maxNim; i2++) {
      if ((i1 ^ i2) < maxNim && (i1 ^ i2) >= 0) {
        res += (nimScore[0][i1] * nimScore[1][i2]) % MOD * nimScore[2][i1 ^ i2];
        res %= MOD;
      }
    }
  }
  // cout << maxNim << endl;
  cout << res << endl;
  // cout << nimScore[0][0] << ", " << nimScore[1][0] << ", " << nimScore[2][0]
  // << endl;
}

signed main() {
  // DON'T MIX "scanf" and "cin"!
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);
  run();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;

#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define REP(i, n) rep(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rs resize
#define DBG(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

const ld PI = acos(-1.0);
template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T> int sz(const T &x) {
  return (int)x.size(); // copy the ampersand(&)!
}

template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
  os << "\n[";
  for (T &x : v)
    os << x << ',';
  return os << "]\n";
}

struct pairhash {
public:
  template <typename T1, typename T2>
  size_t operator()(const pair<T1, T2> &p) const {
    size_t lhs = hash<T1>()(p.x);
    size_t rhs = hash<T2>()(p.y);
    return lhs ^ (rhs + 0x9e3779b9 + (lhs << 6) + (lhs >> 2));
  }
};

const ll MOD = 998244353;
const ll BASE = 1000000000000000000 % MOD;

// vector<vvi> adj;
vvi nim;

void run() {
  ll N;
  cin >> N;

  ll maxNim = 0;
  for (ll i = 0; i < 3; i++) {
    vvi adj(N);
    nim.pb(vi(N, 0));
    ll M;
    cin >> M;
    for (ll j = 0; j < M; j++) {
      ll a, b;
      cin >> a >> b;
      a--;
      b--;
      if (a > b)
        swap(a, b);
      adj[a].pb(b);
    }

    for (ll j = N - 1; j >= 0; j--) {
      vector<bool> U(adj[j].size() + 2, false);
      for (ll k : adj[j]) {
        if (nim[i][k] < U.size())
          U[nim[i][k]] = true;
      }
      for (ll k = 0; k < U.size(); k++) {
        if (!U[k]) {
          // cout << j << ": " << k << endl;
          nim[i][j] = k;
          break;
        }
      }
      maxNim = max(maxNim, nim[i][j] + 1);
    }
  }

  vvi nimScore(3, vi(maxNim, 0));
  for (ll i = 0; i < 3; i++) {
    ll p = 1;
    for (ll j = 0; j < N; j++) {
      p *= BASE;
      p %= MOD;
      nimScore[i][nim[i][j]] += p;
      nimScore[i][nim[i][j]] %= MOD;
    }
  }

  ll res = 0;
  for (ll i1 = 0; i1 < maxNim; i1++) {
    for (ll i2 = 0; i2 < maxNim; i2++) {
      if ((i1 ^ i2) < maxNim && (i1 ^ i2) >= 0) {
        res += (nimScore[0][i1] * nimScore[1][i2]) % MOD * nimScore[2][i1 ^ i2];
        res %= MOD;
      }
    }
  }
  // cout << maxNim << endl;
  cout << res << endl;
  // cout << nimScore[0][0] << ", " << nimScore[1][0] << ", " << nimScore[2][0]
  // << endl;
}

signed main() {
  // DON'T MIX "scanf" and "cin"!
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);
  run();
  return 0;
}