Problem p02737 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

using std::fixed;
using std::setprecision;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }

ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }

namespace MySpace {};

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

const int N = 510200;
const long long MOD = 998244353;
const long long INF = 1e18;

int n, m;
int val[3][N];
vector<int> g[N];

long long inq(long long x, long long y) {
  if (y == 0)
    return 1;
  long long l = inq(x, y / 2);
  if (y % 2)
    return l * l % MOD * x % MOD;
  return l * l % MOD;
}

void run1(int id) {
  for (int i = 0; i <= n; i++)
    g[i].clear();
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> gr(n + 1);
  for (int i = n; i >= 1; i--) {
    vector<int> cnt(g[i].size() + 2);
    for (auto u : g[i]) {
      if (i < u)
        cnt[gr[u]]++;
    }
    for (int j = 0; j < g[i].size() + 2; j++) {
      if (cnt[j] == 0) {
        gr[i] = j;
        val[id][j] = (val[id][j] + inq(INF % MOD, i)) % MOD;
        break;
      }
    }
  }
}

signed main() {
  srand(time(NULL));
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  cin >> n;
  run1(0);
  run1(1);
  run1(2);
  long long ans = 0;
  int M = 1000;
  for (int x = 0; x < M; x++) {
    for (int y = 0; y < M; y++) {
      int z = x ^ y;
      ans = (ans + val[0][x] * val[1][y] % MOD * val[2][z]) % MOD;
    }
  }
  cout << ans;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

using std::bitset;
using std::deque;
using std::iterator;
using std::map;
using std::multimap;
using std::multiset;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::vector;

using std::fill;
using std::ios_base;
using std::max_element;
using std::min_element;
using std::reverse;
using std::sort;
using std::stable_sort;
using std::swap;
using std::unique;

using std::fixed;
using std::setprecision;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef string S;

ll min(ll a, ll b) { return a < b ? a : b; }
ll min(int a, ll b) { return a < b ? a : b; }
ll min(ll a, int b) { return a < b ? a : b; }
ll min(int a, int b) { return a < b ? a : b; }

ll max(ll a, ll b) { return a > b ? a : b; }
ll max(int a, ll b) { return a > b ? a : b; }
ll max(ll a, int b) { return a > b ? a : b; }
ll max(int a, int b) { return a > b ? a : b; }

namespace MySpace {};

#define F(i, n) for (int(i) = 0; (i) != (n); (i)++)
#define fi first
#define se second
#define re return
#define all(x) (x).begin(), (x).end()
#define int long long

const int N = 510200;
const long long MOD = 998244353;
const long long INF = 1e18;

int n, m;
int val[3][N];
vector<int> g[N];

long long inq(long long x, long long y) {
  if (y == 0)
    return 1;
  long long l = inq(x, y / 2);
  if (y % 2)
    return l * l % MOD * x % MOD;
  return l * l % MOD;
}

void run1(int id) {
  for (int i = 0; i <= n; i++)
    g[i].clear();
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  vector<int> gr(n + 1);
  for (int i = n; i >= 1; i--) {
    vector<int> cnt(g[i].size() + 2);
    for (auto u : g[i]) {
      if (i < u && gr[u] < g[i].size() + 2)
        cnt[gr[u]]++;
    }
    for (int j = 0; j < g[i].size() + 2; j++) {
      if (cnt[j] == 0) {
        gr[i] = j;
        val[id][j] = (val[id][j] + inq(INF % MOD, i)) % MOD;
        break;
      }
    }
  }
}

signed main() {
  srand(time(NULL));
  // ios_base::sync_with_stdio(false);
  // cin.tie(0);
  // cout.tie(0);
  cin >> n;
  run1(0);
  run1(1);
  run1(2);
  long long ans = 0;
  int M = 1000;
  for (int x = 0; x < M; x++) {
    for (int y = 0; y < M; y++) {
      int z = x ^ y;
      ans = (ans + val[0][x] * val[1][y] % MOD * val[2][z]) % MOD;
    }
  }
  cout << ans;
}