Language: C++

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= int(b); i++)
#define per(i, a, b) for (int i = (a); i >= int(b); i--)
#define fi first
#define se second
#define int long long
using namespace std;

const int maxn = 2e5, mod = 998244353;
int n, f[maxn + 3], mx[maxn + 3], se[maxn + 3], g[maxn + 3];
int far[maxn + 3], h[maxn + 3][2];
vector<int> G[maxn + 3];

void dfs(int u, int p = 0) {
	rep(i, 0, G[u].size() - 1) {
		int v = G[u][i];
		if (v == p) continue;
		dfs(v, u);
		f[u] = max(f[u], f[v] + 1);
		if (!mx[u] || f[v] >= f[mx[u]]) {
			se[u] = mx[u], mx[u] = v;
		} else if (!se[u] || f[v] >= f[se[u]]) {
			se[u] = v;
		}
	}
}

void down(int u, int p = 0) {
	rep(i, 0, G[u].size() - 1) {
		int v = G[u][i];
		if (v == p) continue;
		if (v == mx[u]) {
			g[v] = max(g[u], f[se[u]] + 1) + 1;
		} else {
			g[v] = max(g[u], f[mx[u]] + 1) + 1;
		}
		down(v, u);
	}
	g[u] = max(f[u], g[u]);
}

void work(int u, int p = 0) {
	rep(i, 0, G[u].size() - 1) {
		int v = G[u][i];
		if (v == p) continue;
		work(v, u);
		far[u] = max(far[u], far[v] + 1);
	}
	if (!far[u]) {
		h[u][0] = h[u][1] = 1; return;
	}
	int A = 1, B = 0, C = 3;
	rep(i, 0, G[u].size() - 1) {
		int v = G[u][i];
		if (v == p) continue;
		int D = 0, E = 0;
		if (far[v] + 1 != far[u]) {
			D = h[v][0] + h[v][1] * 2ll % mod; 
		} else {
			D = h[v][0] + h[v][1], E = h[v][1];
		}
		C = 1ll * C * (h[v][0] + h[v][1] * 2ll) % mod;
		B = (1ll * B * D + 1ll * A * E) % mod;
		A = 1ll * A * D % mod;
	}
	h[u][1] = B, h[u][0] = (C - B * 2) % mod;
	h[u][0] < 0 ? h[u][0] += mod : 0;
}

signed main() {
	scanf("%d", &n);
	if (n == 1) { puts("1"), exit(0); }
	rep(i, 1, n - 1) {
		int u, v; scanf("%d %d", &u, &v);
		G[u].push_back(v), G[v].push_back(u);
	}
	f[0] = -1;
	dfs(1);
	down(1);
	int mn = n;
	rep(i, 1, n) mn = min(mn, g[i]);
	int a = 0, b = 0, c = 0;
	rep(i, 1, n) if (g[i] == mn) {
		if (++c == 1) a = i;
		else if (c == 2) b = i;
		else exit(1);
	}
	if (c == 1) {
		int A = 1, B = 0, C = 0;
		rep(i, 0, G[a].size() - 1) {
			int v = G[a][i];
			work(v, a);
			int D = 0, E = 0;
			if (far[v] + 1 != g[a]) {
				D = h[v][0] + h[v][1] * 2ll % mod;
			} else {
				D = h[v][0], E = h[v][1];
			}
			C = (1ll * C * D + 1ll * B * E) % mod;
			B = (1ll * B * D + 1ll * A * E) % mod;
			A = 1ll * A * D % mod;
		}
		printf("%d\n", C);
	} else {
		work(a, b);
		work(b, a);
		printf("%d\n", int(1ll * h[a][1] * h[b][1] % mod));
	}
	return 0;
}

Compiler output:
a.cc: In function 'void dfs(long long int, long long int)':
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:15:9: note: in expansion of macro 'rep'
   15 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ';' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:15:9: note: in expansion of macro 'rep'
   15 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:15:9: note: in expansion of macro 'rep'
   15 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ')' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:15:9: note: in expansion of macro 'rep'
   15 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:2:26: note: to match this '('
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                          ^
a.cc:15:9: note: in expansion of macro 'rep'
   15 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:15:9: note: in expansion of macro 'rep'
   15 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:15:13: error: 'i' was not declared in this scope
   15 |         rep(i, 0, G[u].size() - 1) {
      |             ^
a.cc:2:53: note: in definition of macro 'rep'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                                     ^
a.cc: In function 'void down(long long int, long long int)':
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:29:9: note: in expansion of macro 'rep'
   29 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ';' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:29:9: note: in expansion of macro 'rep'
   29 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:29:9: note: in expansion of macro 'rep'
   29 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ')' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:29:9: note: in expansion of macro 'rep'
   29 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:2:26: note: to match this '('
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                          ^
a.cc:29:9: note: in expansion of macro 'rep'
   29 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:29:9: note: in expansion of macro 'rep'
   29 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:29:13: error: 'i' was not declared in this scope
   29 |         rep(i, 0, G[u].size() - 1) {
      |             ^
a.cc:2:53: note: in definition of macro 'rep'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                                     ^
a.cc: In function 'void work(long long int, long long int)':
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:43:9: note: in expansion of macro 'rep'
   43 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ';' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:43:9: note: in expansion of macro 'rep'
   43 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:43:9: note: in expansion of macro 'rep'
   43 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ')' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:43:9: note: in expansion of macro 'rep'
   43 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:2:26: note: to match this '('
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                          ^
a.cc:43:9: note: in expansion of macro 'rep'
   43 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:43:9: note: in expansion of macro 'rep'
   43 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:43:13: error: 'i' was not declared in this scope
   43 |         rep(i, 0, G[u].size() - 1) {
      |             ^
a.cc:2:53: note: in definition of macro 'rep'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                                     ^
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:53:9: note: in expansion of macro 'rep'
   53 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ';' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:53:9: note: in expansion of macro 'rep'
   53 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:53:9: note: in expansion of macro 'rep'
   53 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected ')' before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:53:9: note: in expansion of macro 'rep'
   53 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:2:26: note: to match this '('
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                          ^
a.cc:53:9: note: in expansion of macro 'rep'
   53 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:53:9: note: in expansion of macro 'rep'
   53 |         rep(i, 0, G[u].size() - 1) {
      |         ^~~
a.cc: In function 'int main()':
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:2:45: note: in expansion of macro 'int'
    2 | #define rep(i, a, b) for (int i = (a); i <= int(b); i++)
      |                                             ^~~
a.cc:73:9: note: in expansion of macro 'rep'
   73 |         rep(i, 1, n - 1) {
      |         ^~~
a.cc:6:13: error: expected ';' before 'long'
    6 | #define int long long
      |