Problem: Given are simple undirected graphs X, Y, Z, with N vertices each and M_1, M_2, M_3 edges, respectively. The vertices in X, Y, Z are respectively called x_1, x_2, \dots, x_N, y_1, y_2, \dots, y_N, z_1, z_2, \dots, z_N. The edges in X, Y, Z are respectively (x_{a_i}, x_{b_i}), (y_{c_i}, y_{d_i}), (z_{e_i}, z_{f_i}).  Based on X, Y, Z, we will build another undirected graph W with N^3 vertices. There are N^3 ways to choose a vertex from each of the graphs X, Y, Z. Each of these choices corresponds to the vertices in W one-to-one. Let (x_i, y_j, z_k) denote the vertex in W corresponding to the choice of x_i, y_j, z_k.  We will span edges in W as follows:    * For each edge (x_u, x_v) in X and each w, l, span an edge between (x_u, y_w, z_l) and (x_v, y_w, z_l).   * For each edge (y_u, y_v) in Y and each w, l, span an edge between (x_w, y_u, z_l) and (x_w, y_v, z_l).   * For each edge (z_u, z_v) in Z and each w, l, span an edge between (x_w, y_l, z_u) and (x_w, y_l, z_v).  Then, let the weight of the vertex (x_i, y_j, z_k) in W be 1,000,000,000,000,000,000^{(i +j + k)} = 10^{18(i + j + k)}. Find the maximum possible total weight of the vertices in an ) in W, and print that total weight modulo 998,244,353.

Submission attempts (in order):
from collections import defaultdict

M = 998244353
B = pow(10, 18, M)
N = int(input())

def ext_euc(a, b):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, b
  while z1 != 1:
    d, m = divmod(z2,z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1, y1

def inv_mod(a, b, m):
  x, y = ext_euc(a, m)
  return (x * b % m)

def mex(s):
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(e):
  g = {}
  sum_g = defaultdict(int)
  sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M)
  for i in range(N, 0, -1):
    if i not in e:
      continue
    m = mex({g.get(j, 0) for j in e[i]})
    if m:
      g[i] = m
  x = 1
  for i in range(1, N+1)
    x = (x * B) % M
    if g[i]:
      sum_g[g[i]] += x
      sum_g[0] -= x
  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(set)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].add(b)
  return e

def solve(N, edge):
  sum_g = list(map(calc_grundy, edge))
  ret = 0
  for gx, sx in sum_g[0].items():
    for gy, sy in sum_g[1].items():
      gz = gx^gy
      sz = sum_g[2][gz]
      if sz:
        ret += (sx*sy*sz) % M
  return ret%M


edge = [read_edge() for i in range(3)]

print(solve(N, edge))

from collections import defaultdict

M = 998244353
B = pow(10, 18, M)
N = int(input())

def ext_euc(a, b):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, b
  while z1 != 1:
    d, m = divmod(z2,z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1, y1

def inv_mod(a, b, m):
  x, y = ext_euc(a, m)
  return (x * b % m)

def mex(s):
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(e):
  g = {}
  sum_g = defaultdict(int)
  sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M)
  for i in range(N, 0, -1):
    if i not in e:
      continue
    m = mex({g.get(j, 0) for j in e[i]})
    if m:
      g[i] = m
  x = 1
  for i in range(1, N+1):
    x = (x * B) % M
    if g[i]:
      sum_g[g[i]] += x
      sum_g[0] -= x
  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(set)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].add(b)
  return e

def solve(N, edge):
  sum_g = list(map(calc_grundy, edge))
  ret = 0
  for gx, sx in sum_g[0].items():
    for gy, sy in sum_g[1].items():
      gz = gx^gy
      sz = sum_g[2][gz]
      if sz:
        ret += (sx*sy*sz) % M
  return ret%M


edge = [read_edge() for i in range(3)]

print(solve(N, edge))

from collections import defaultdict

M = 998244353
B = pow(10, 18, M)
N = int(input())

def geometric_mod(a, r, m, n):
  x = a
  for i in range(n):
    yield x
    x = (x*r)%m

BB = list(geometric_mod(1, B, M, N+2))

def ext_euc(a, b):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, b
  while z1 != 1:
    d, m = divmod(z2,z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1, y1

def inv_mod(a, b, m):
  x, y = ext_euc(a, m)
  return (x * b % m)

def mex(s):
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(e):
  e = set(e)
  g = {}
  sum_g = defaultdict(int)
  sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M)
  for i in range(N, 0, -1):
    if i not in e:
      continue
    m = mex({g.get(j, 0) for j in e[i]})
    if m:
      g[i] = m
      sum_g[g[i]] = (sum_g[g[i]] + BB[i]) % M
      sum_g[0]  = (sum_g[0] - BB[i]) % M

  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(list)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].append(b)
  return e

def solve(N, edge):
  sum_g = list(map(calc_grundy, edge))
  ret = 0
  for gx, sx in sum_g[0].items():
    for gy, sy in sum_g[1].items():
      gz = gx^gy
      sz = sum_g[2][gz]
      if sz:
        ret = (ret + sx*sy*sz) % M
  return ret


edge = [read_edge() for i in range(3)]

print(solve(N, edge))

from collections import defaultdict
import sys

input = lambda: sys.stdin.readline().rstrip()

M = 998244353
B = pow(10, 18, M)
N = int(input())

def ext_euc(a, b):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, b
  while z1 != 1:
    d, m = divmod(z2,z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1, y1

def inv_mod(a, b, m):
  x, y = ext_euc(a, m)
  return (x * b % m)

def mex(s):
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(e):
  g = {}
  sum_g = defaultdict(int)
  sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M)
  for i in range(N, 0, -1):
    if i not in e:
      continue
    m = mex({g.get(j, 0) for j in e[i]})
    if m:
      g[i] = m
  prev_i = 1
  prev_W = B
  for i in range(1, N+1):
    if i in g:
      W = (W * pow(B, i-prev_i, M)) % M
      sum_g[g[i]] = (sum_g[g[i]] + W) % M
      sum_g[0]  = (sum_g[0] - W) % M
      prev_i, prev_W = i, W
  
  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(list)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].append(b)
  return e

def solve(N, edge):
  sum_g = list(map(calc_grundy, edge))
  ret = 0
  for gx, sx in sum_g[0].items():
    for gy, sy in sum_g[1].items():
      gz = gx^gy
      sz = sum_g[2][gz]
      if sz:
        ret = (ret + sx*sy*sz) % M
  return ret

edge = [read_edge() for i in range(3)]

print(solve(N, edge))

from collections import defaultdict
import sys
input = lambda: sys.stdin.readline().rstrip()

M = 998244353
B = pow(10, 18, M)
N = int(input())

def geometric_mod(a, r, m, n):
  x = a
  for i in range(n):
    yield x
    x = (x*r)%m

BB = list(geometric_mod(1, B, M, N+2))

def ext_euc(a, b):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, b
  while z1 != 1:
    d, m = divmod(z2,z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1, y1

def inv_mod(a, b, m):
  x, y = ext_euc(a, m)
  return (x * b % m)

def mex(s):
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(e):
  g = {}
  sum_g = defaultdict(int)
  sum_g[0] = inv_mod(B-1, pow(B, N+1, M)-B, M)
  for i in range(N, 0, -1):
    if i not in e:
      continue
    m = mex({g.get(j, 0) for j in e[i]})
    if m:
      g[i] = m
      sum_g[g[i]] = (sum_g[g[i]] + BB[i]) % M
      sum_g[0]  = (sum_g[0] - BB[i]) % M

  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(list)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].add(b)
  return e

def solve(N, edge):
  sum_g = list(map(calc_grundy, edge))
  ret = 0
  for gx, sx in sum_g[0].items():
    for gy, sy in sum_g[1].items():
      gz = gx^gy
      sz = sum_g[2][gz]
      if sz:
        ret = (ret + sx*sy*sz) % M
  return ret


edge = [read_edge() for i in range(3)]

print(solve(N, edge))

from collections import defaultdict
import sys

input = lambda: sys.stdin.readline().rstrip()

M = 998244353 # < (1 << 30)

def mod_inv(a, M=M):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, M
  while abs(z1) != 1:
    d, m = divmod(z2, z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1*z1%M

R_bits = 30
R = 1 << R_bits
R_dec = R - 1
R2 = pow(R, 2, M)
M_neg_inv = mod_inv(-M, R)

def MR(x):
  b = (x * M_neg_inv) & R_dec
  t = x + b * M
  c = t >> R_bits
  d = c - M
  return d if 0 <= d else c

def Montgomery(x):
  return MR(x*R2)

def mex(s):
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(edge):
  grundy = [{} for i in range(3)]
  sum_g = [defaultdict(int, {0:M_W_sum}) for i in range(3)]
  M_W = M_W_last
  for i in range(N, 0, -1):
    for g, s, e in zip(grundy, sum_g, edge):
      if i in e:
        m = mex({g.get(j, 0) for j in e[i]})
        if m:
          g[i] = m
          s[g[i]] +=  M_W
          s[0] +=  M - M_W
    M_W = MR(M_W * M_B_inv)
  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(list)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].append(b)
  return e

N = int(input())
B = pow(10, 18, M)
M_B_inv = Montgomery(mod_inv(B))
M_W_last = Montgomery(pow(B, N, M))
M_W_sum = MR((M_W_last + Montgomery(-1)) * Montgomery(B * mod_inv(B-1) % M))

edge = [read_edge() for i in range(3)]
sum_g = calc_grundy(edge)
ans = 0
for gx, sx in sum_g[0].items():
  for gy, sy in sum_g[1].items():
    gz = gx^gy
    sz = sum_g[2][gz]
    if sz:
      ans += Montgomery(MR(sx)*MR(sy)*MR(sz))
print(MR(ans))


from collections import defaultdict
import sys

input = lambda: sys.stdin.readline().rstrip()

def ext_euc(a, b):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, b
  while z1 != 1:
    d, m = divmod(z2,z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1, y1

class mod_int(int):
  def __new__(cls, i=0, *args, **kwargs):
    return int.__new__(cls, i%M, *args, **kwargs)
  
  def __add__(self, x):
    return self.__class__(int.__add__(self, x))
    
  def __sub__(self, x):
    return self.__class__(int.__sub__(self, x))
    
  def __neg__(self):
    return self.__class__(int.__neg__(self, x))
  
  def __mul__(self, x):
    return self.__class__(int.__mul__(self, x))
  
  def __floordiv__(self, x):
    a, b = ext_euc(x, M)
    return self * a
  
  def __inv__(self):
    a, b = ext_euc(self, M)
    return self.__class__(a)
  
  def __pow__(self, x):
    return self.__class__(int.__pow__(self, x, M))

def mex(s):
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(e):
  g = {}
  sum_g = defaultdict(mod_int)
  sum_g[0] = (B**(N+1) - B) // (B-1)
  prev = N
  W = B**N
  for i in range(N, 0, -1):
    if i not in e:
      continue
    m = mex({g.get(j, 0) for j in e[i]})
    if m:
      g[i] = m
      W *= B_inv**(prev-i)
      sum_g[g[i]] +=  W
      sum_g[0]  -= W
      prev, W = i, W
  
  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(list)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].append(b)
  return e

def solve(N, edge):
  sum_g = list(map(calc_grundy, edge))
  ret = mod_int(0)
  for gx, sx in sum_g[0].items():
    for gy, sy in sum_g[1].items():
      gz = gx^gy
      sz = sum_g[2][gz]
      if sz:
        ret += sx*sy*sz
  return ret


M = 998244353
B = mod_int(10) ** 18
B_inv = ~B
N = int(input())

edge = [read_edge() for i in range(3)]

print(solve(N, edge))


from collections import defaultdict
import sys

input = lambda: sys.stdin.readline().rstrip()

def mod_inv(a):
  x1, y1, z1 = 1, 0, a
  x2, y2, z2 = 0, 1, M
  while z1 != 1:
    d, m = divmod(z2, z1)
    x1, x2 = x2-d*x1, x1
    y1, y2 = y2-d*y1, y1
    z1, z2 = m, z1
  return x1%M

def mex(s):
  s = set(s)
  for i in range(N+1):
    if i not in s:
      return i

def calc_grundy(e):
  g = {}
  for i in range(N, 0, -1):
    if i not in e:
      continue
    m = mex(map(lambda x:g.get(x, 0), e[i]))
    if m:
      g[i] = m
  return g

def sum_grundy(grundy):
  sum_g = [defaultdict(int, {0:B_sum}) for i in range(3)]
  W, prev = B, 1
  for i in range(1, N+1):
    for s, g in zip(sum_g, grundy):
      if i in g:
        if prev != i:
          W, prev = W * pow(B, i-prev, M) % M, i
        s[g[i]] =  (s[g[i]] + W) % M
        s[0] =  (s[0] - W) % M
  return sum_g

def read_edge():
  M = int(input())
  e = defaultdict(list)
  for i in range(M):
    a, b = sorted(map(int, input().split()))
    e[a].append(b)
  return e

N = int(input())
M = 998244353
B = pow(10, 18, M)
B_sum = (pow(B, N+1, M) - B) * mod_inv(B-1) % M

edge = [read_edge() for i in range(3)]
grundy = list(map(calc_grundy, edge))
sum_g = sum_grundy(grundy)
ans = 0
for gx, sx in sum_g[0].items():
  for gy, sy in sum_g[1].items():
    gz = gx^gy
    if gz in sum_g[2]:
      ans = (ans + sx*sy*sum_g[2][gz]) % M
print(ans)


Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Wrong Answer

Wrong Answer

Accepted