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

#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
const int N = 300007, P = 998244353, i2 = P - P / 2;
int m, root, len, fa[N], dia[N], f[N][3][3], g[3][3];
std::vector<int> e[N];
void inc(int &a, int b) { a += b - P, a += a >> 31 & P; }
int mul(int a, int b) { return 1ll * a * b % P; }
int read() {
  int x = 0, c = getchar();
  while (isspace(c))
    c = getchar();
  while (isdigit(c))
    (x *= 10) += c & 15, c = getchar();
  return x;
}
void dfs(int u, int dep) {
  if (dep > len)
    len = dep, root = u;
  for (int v : e[u])
    if (v ^ fa[u])
      fa[v] = u, dfs(v, dep + 1);
}
void dp(int u, int fa, int d) {
  (d == m / 2 ? f[u][1][1] : f[u][0][0]) = 1;
  for (int v : e[u]) {
    if (v == fa)
      continue;
    dp(v, u, d + 1), memset(g, 0, 36);
    for (int x = 0; x < 3; ++x)
      for (int y = 0; y < 3; ++y)
        for (int p = 0; p < 3; ++p)
          for (int q = 0; q < 3; ++q)
            for (int k = 0, r, t; k < 3; ++k)
              r = std::min(2, x + p * (k == 1)),
              t = std::min(2, y + q * (k == 2)),
              inc(g[r][t], mul(f[u][x][y], f[v][p][q]));
    memcpy(f[u], g, 36);
  }
}
int main() {
  int n = read(), p, q;
  for (int i = 1, u, v; i < n; ++i)
    u = read(), v = read(), e[u].push_back(v), e[v].push_back(u);
  dfs(1, 1), memset(fa + 1, 0, n << 2), len = 0, dfs(root, 1);
  for (int i = root; i; i = fa[i])
    dia[++m] = i;
  if (m & 1)
    p = dia[m / 2 + 1], dp(p, 0, 0), printf("%d", mul(i2, f[p][1][1]));
  else
    p = dia[m / 2], q = dia[m / 2 + 1], dp(p, q, 1), dp(q, p, 1),
    printf("%d", mul((1ll * f[p][1][0] + f[p][1][1] + f[p][1][2]) % P,
                     (1ll * f[q][1][0] + f[q][1][1] + f[q][1][2]) % P));
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define nn 114514 * 2
#define mod 998244353
#define int long long
void add(int &x, int y) { x = (x + y) % mod; }
int dp[nn][4][4], n, diam, c1, c2;
vector<int> v[nn];
int chk[nn];
int dep[nn], pa[nn];
void dfs(int x, int p, int slg = 0) {
  if (!p or slg)
    dep[x] = 0;
  else
    dep[x] = dep[p] + 1;
  pa[x] = p;
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != p)
      dfs(v[x][i], x);
}
int walk(int x, int d) {
  if (d == 0)
    return x;
  return walk(pa[x], d - 1);
}
int mst() {
  int ret = 0;
  for (int i = 1; i <= n; i++)
    ret = max(ret, dep[i]);
  for (int i = 1; i <= n; i++)
    if (dep[i] == ret)
      return i;
}
int f[2][4];
int tmp[4][4];

void copy(int x) {
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++)
      tmp[i][j] = dp[x][i][j], dp[x][i][j] = 0;
}

void efs(int x, int p) {
  dp[x][3 * (dep[x] == diam / 2)][0] = 1;
  int leaf = 1;
  for (int i = 0; i < v[x].size(); i++)
    if (v[x][i] != p) {
      efs(v[x][i], x);
      leaf = 0;
      copy(x);
      for (int j = 0; j < 16; j++)
        for (int k = 0; k < 16; k++)
          add(dp[x][(j % 4) | (k % 4)][(j / 4) | (k / 4) | ((j % 4) & (k % 4))],
              tmp[k % 4][k / 4] * dp[v[x][i]][j % 4][j / 4]);
    }

  if (dep[x] != diam / 2 and leaf and 0)
    dp[x][0][0] = 3;
  else if (x != c1 and x != c2) {
    copy(x);
    for (int k = 0; k < 16; k++)
      for (int j = 0; j <= 2; j++)
        add(dp[x][(k % 4) & j][(k / 4) & j], tmp[k % 4][k / 4]);
  }
}

signed main() {
  scanf("%d", &n);
  if (n <= 2)
    return puts("1") * 0;

  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[x].push_back(y);
    v[y].push_back(x);
  }

  dfs(1, 0);
  int x = mst();
  dfs(x, 0);
  int y = mst();
  diam = dep[y];
  if (diam % 2 == 0) {
    c1 = walk(y, diam / 2);
    int now = 0;
    f[now][0] = 1;
    dfs(c1, 0);
    efs(c1, 0);
    /*		for(int i=0;i<v[c1].size();i++) {
                            efs(v[c1][i],c1);
                            now^=1;memset(f[now],0,sizeof f[now]);
                            for(int j=0;j<4;j++) for(int k=0;k<4;k++)
       add(f[now][j|k],dp[v[c1][i]][j][0]*f[now^1][k]);
                    }
                    cout<<f[now][3];*/
    cout << dp[c1][3][0] * (mod + 1) / 2 % mod;
  } else {
    c1 = walk(y, diam / 2);
    dfs(y, 0);
    c2 = walk(x, diam / 2);

    dfs(c1, c2, 1);
    dfs(c2, c1, 1);
    efs(c1, c2);
    efs(c2, c1);

    cout << dp[c1][1][0] * dp[c2][2][0] % mod;
  }

  return 0;
}