Problem:
G: Toss Cut Tree
Problem Statement
You are given two trees
T, U
. Each tree has
N
vertices that are numbered
1
through
N
. The
i
-th edge (
1 \leq i \leq N-1
) of tree
T
connects vertices
a_i
and
b_i
. The
i
-th edge (
1 \leq i \leq N-1
) of tree
U
connects vertices
c_i
and
d_i
.
The operation described below will be performed
N
times.
On the
i
-th operation, flip a fair coin.
If it lands heads up, remove vertex
i
and all edges directly connecting to it from tree
T
.
If it lands tails up, remove vertex
i
and all edges directly connecting to it from tree
U
.
After
N
operations, each tree will be divided into several connected components. Let
X
and
Y
be the number of connected components originated from tree
T
and
U
, respectively.
Your task is to compute the expected value of
X \times Y
. More specifically, your program must output an integer
R
described below.
First, you can prove
X \times Y
is a rational number. Let
P
and
Q
be coprime integers that satisfies
\frac{P}{Q} = X \times Y
.
R
is the only integer that satisfies
R \times Q
$\equiv$
P
$ \bmod \;$
998244353, 0 \leq R < 998244353
.
Input
N
a_1
b_1
:
a_{N-1}
b_{N-1}
c_1
d_1
:
c_{N-1}
d_{N-1}
Constraints
1 \leq N \leq 2 \times 10^5
1 \leq a_i, b_i, c_i, d_i \leq N
The given two graphs are trees.
All values in the input are integers.
Output
Print
R
described above in a single line.
Sample Input 1
3
1 2
2 3
1 2
3 1
Output for Sample Input 1
1
A coin is flipped
3
times. Therefore, there are
2^3 = 8
outcomes in total.
Suppose that the outcome was (tail, head, tail) in this order. Vertex
2
of tree
T
and vertex
1
,
3
of tree
U
are removed along with all edges connecting to them.
T
,
U
are decomposed to
2
,
1
connected components, respectively. Thus, the value of
XY
is
2 \times 1 = 2
in this case.
It is possible to calculate
XY
for the other cases in the same way and the expected value of
XY
is
1
.
Sample Input 2
4
1 2
1 4
2 3
1 4
4 2
2 3
Output for Sample Input 2
374341634
The expected number of
XY
is
\frac{13}{8}
.

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1ll << 60;
#define REP(i, n) for(ll i=0; i <ll(n); i++)
template <class T> using V = vector<T>;
template <class A, class B>
bool chmax(A &a, B b) { return a < b && (a=b, true); }
template <class A, class B>
bool chmin(A &a, B b) { return b < a && (a=b, true); }

template <int MD, int g = 3>
struct ModInt {
  using M = ModInt;
  const static inline M G = g;
  unsigned int v;
  ModInt() : v(0) {}
  ModInt(ll w) : v(w % MD + MD) {
    if(v >= MD) v -= MD;
  }
  static M raw(unsigned int v) {
    M res;
    res.v = (v < MD) ? v : v - MD;
    return res;
  }
  explicit operator bool() const {
    return v != 0;
  }
  M operator-() const {
    return M() - *this;
  }
  M operator+(M r) const {
    return raw(v + r.v);
  }
  M operator-(M r) const {
    return raw(v + MD - r.v);
  }
  M operator*(M r) const {
    return raw(ll(v) * r.v % MD);
  }
  M operator/(M r) const {
    return *this * r.inv();
  }
  M& operator+=(M r) {
    return *this = *this + r;
  }
  M& operator-=(M r) {
    return *this = *this - r;
  }
  M& operator*=(M r) {
    return *this = *this * r;
  }
  M& operator/=(M r) {
    return *this = *this / r;
  }
  bool operator==(M r) const {
    return v == r.v;
  }
  M pow(ll n) const {
    M x = *this, r = 1;
    while (n) {
      if (n & 1) r *= x;
      x *= x;
      n >>= 1;
    }
    return r;
  }
  M inv() const {
    return pow(MD - 2);
  }
  friend ostream& operator<<(ostream& os, M r) {
    return os << r.v;
  }
};
using Mint = ModInt<998244353, 3>;


struct Tree {
  V<ll> par;
  V<ll> deg;
  Tree(ll N){
    V<V<ll>> A(N);
    REP(i,N-1){
      ll u,v; cin >> u >> v; u--; v--;
      A[u].push_back(v);
      A[v].push_back(u);
    }
    par.assign(N, -1);
    deg.assign(N, 0);
    auto dfs = [&](auto& dfs, ll v) -> void {
      for(ll w : A[v]) if(w != par[v]){
        par[w] = v;
        dfs(dfs, w);
        deg[v]++;
      }
    };
    dfs(dfs, 0);
  }
};

void testcase(){
  ll N; cin >> N;
  Tree A(N), B(N);
  Mint inv16 = Mint(16).inv();
  Mint ans = inv16 * (N-1) * (N-2);
  ans += inv16 * 4 * (N - 1);
  ans += inv16 * 2 * (A.deg[0] + B.deg[0]);
  for(ll i=0; i<N; i++) ans -= inv16 * A.deg[i] * B.deg[i];
  for(ll i=1; i<N; i++) ans += inv16 * (A.deg[i] + B.deg[i]);
  for(ll i=1; i<N; i++) if(A.par[B.par[i]] == i) ans += inv16;
  for(ll i=1; i<N; i++) if(A.par[i] == B.par[i]) ans += inv16;
  //ans *= 16;
  cout << ans.v << "\n";
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  testcase();
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 32284
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5;
const int mod = 998244353;
const int inv2 = (mod + 1) / 2;
const int inv4 = 1ll * inv2 * inv2 % mod;
const int inv8 = 1ll * inv4 * inv2 % mod;
const int inv16 = 1ll * inv8 * inv2 % mod;
int n, f1[maxn + 10], f2[maxn + 10], ssz[maxn + 10];
int ans;
vector<int> g[maxn + 10], z[maxn + 10];

int getit(int x, int y) {
	int ans = n - 1;
	ans -= z[x].size(); ans -= z[y].size();
	if (f2[x] == y || f2[y] == x) ++ans;
	return ans;
}

void dfs1(int p) {
	for (int i = 0; i < (int)g[p].size(); ++i) {
		int e = g[p][i];
		if (e != f1[p]) {
			f1[e] = p; dfs1(e);
		}
	}
}

void dfs2(int p) {
	for (int i = 0; i < (int)z[p].size(); ++i) {
		int e = z[p][i];
		if (e != f2[p]) {
			++ssz[p];
			f2[e] = p; dfs2(e);
		}
	}
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i < n; ++i) {
		int l, r; scanf("%d%d", &l, &r);
		g[l].push_back(r); g[r].push_back(l);
	}
	for (int i = 1; i < n; ++i) {
		int l, r; scanf("%d%d", &l, &r);
		z[l].push_back(r); z[r].push_back(l);
	}
	dfs1(1);
	dfs2(1);
	for (int i = 2; i <= n; ++i) {
		if (i == f2[f1[i]]) {
			(ans += inv4) %= mod;
			(ans += 1ll * inv8 * (ssz[i] - 1) % mod) %= mod;
		} else {
			if (f1[i] != 1) (ans += inv8) %= mod;
			(ans += 1ll * inv8 * ssz[i] % mod) %= mod;
		}
		(ans += 1ll * inv16 * getit(i, f1[i]) % mod) %= mod;
	}
	(ans += 1ll * inv8 * (n - 1 - (int)z[1].size()) % mod) %= mod;
	(ans += 1ll * inv8 * (n - 1 - (int)g[1].size()) % mod) %= mod;
	(ans += 1ll * inv4 * (int)z[1].size() % mod) %= mod;
	(ans += 1ll * inv4 * (int)g[1].size() % mod) %= mod;
	printf("%d\n", ans);
}
accuracy: 1
time_ms: 100
memory_kb: 36712
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
set<pair<int, int>> s;
int n, ans, d[200005], fac[200005], inv[200005];
int fpow(int x, int y) {
	int s = 1;
	while(y) {
		if(y & 1)
			s = 1ll * s * x % mod;
		x = 1ll * x * x % mod, y >>= 1;
	}
	return s;
}
int c(int n, int m) {
	if(min({n, m, n - m}) < 0)
		return 0;
	return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n, fac[0] = 1;
	for(int i = 1; i <= n; i++)
		fac[i] = 1ll * fac[i - 1] * i % mod;
	inv[n] = fpow(fac[n], mod - 2);
	for(int i = n - 1; ~i; i--)
		inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
	for(int i = 1, x, y; i < n; i++)
		cin >> x >> y, d[x]++, d[y]++, s.insert({x, y}), s.insert({y, x});
	for(int i = 0; i <= n; i++)
		ans = (ans + 1ll * c(n, i) * i % mod * (n - i) % mod) % mod;
	for(int i = 0; i <= n - 2; i++)
		ans = (ans + mod - 2ll * c(n - 2, i) % mod * i % mod * (n - 1) % mod) % mod;
	int cnt = n < 4 ? 0 : fpow(2, n - 4);
	for(int i = 1, x, y; i < n; i++) {
		cin >> x >> y;
		int tmp = n - 1 - d[x] - d[y];
		if(s.find({x, y}) != s.end())
			tmp++;
		ans = (ans + 1ll * tmp * cnt % mod) % mod;
	}
	cout << 1ll * ans * fpow(fpow(2, n), mod - 2) % mod<<endl;
	return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 24556
final_rank: 12

code_snippet: // by xm
// 2024.11.7
#include <bits/stdc++.h>
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout);
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll N = 2e5 + 5, Mod = 998244353;
const ll inv4 = 748683265, inv8 = 873463809, inv16 = 935854081;

ll n, res1, res2, res3, degT[N], degU[N];

struct pair_hash {
	template <class T1, class T2>
	size_t operator () (pair<T1, T2> const &pair) const {
		size_t h1 = hash<T1>()(pair.first), h2 = hash<T2>()(pair.second);
		return h1 ^ h2;
	}
};

void solution() {
 	cin >> n;
 	unordered_set<pll, pair_hash> T, U;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degT[u]++; degT[v]++;
		T.insert(minmax({u, v}));
	}
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degU[u]++; degU[v]++;
		U.insert(minmax({u, v}));
	}
	res1 = n * (n - 1) % Mod;
	
	for(auto edge : T) {
		ll u = edge.first, v = edge.second;
		ll val = n - 1 - degU[u] - degU[v] + (U.count(edge));
		res3 += val;
		res3 %= Mod;
	}
	for(int i = 1; i <= n; i++) {
		res2 += n - 1 - degT[i];
		res2 += n - 1 - degU[i];
		res2 %= Mod;
	}
	
	ll ans = res1 * inv4 - res2 * inv8 + res3 * inv16;
	ans = (ans % Mod + Mod) % Mod;
	cout << ans << "\n";
	
	// cout << res1 % Mod * inv4 % Mod << "\n";
	// cout << res2 % Mod * inv8 % Mod << "\n";
	// cout << res3 % Mod * inv16 % Mod << "\n";
}

double ST, ED;
int main() {
	//file("tree");
	ios::sync_with_stdio(false), cin.tie(nullptr);
	ST = clock(), solution(), ED = clock();
	//cerr << 1000 * (ED - ST) / CLOCKS_PER_SEC << "ms\n";
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 28372
final_rank: 1

code_snippet: // by xm
// 2024.11.7
#include <bits/stdc++.h>
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout);
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll N = 2e5 + 5, Mod = 998244353;
const ll inv4 = 748683265, inv8 = 873463809, inv16 = 935854081;

ll n, res1, res2, res3, degT[N], degU[N];

struct pair_hash {
	template <class T1, class T2>
	size_t operator () (pair<T1, T2> const &pair) const {
		size_t h1 = hash<T1>()(pair.first), h2 = hash<T2>()(pair.second);
		return h1 ^ h2;
	}
};

void solution() {
 	cin >> n;
 	unordered_set<pll, pair_hash> T, U;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degT[u]++; degT[v]++;
		T.insert(minmax({u, v}));
	}
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degU[u]++; degU[v]++;
		U.insert(minmax({u, v}));
	}
	res1 = n * (n - 1) % Mod;
	
	for(auto edge : T) {
		ll u = edge.first, v = edge.second;
		ll val = n - 1 - degU[u] - degU[v] + (U.count(edge));
		res3 += val;
		res3 %= Mod;
	}
	for(int i = 1; i <= n; i++) {
		res2 += n - 1 - degT[i];
		res2 += n - 1 - degU[i];
		res2 %= Mod;
	}
	
	ll ans = res1 * inv4 - res2 * inv8 + res3 * inv16;
	ans = (ans % Mod + Mod) % Mod;
	cout << ans << "\n";
	
	// cout << res1 % Mod * inv4 % Mod << "\n";
	// cout << res2 % Mod * inv8 % Mod << "\n";
	// cout << res3 % Mod * inv16 % Mod << "\n";
}

double ST, ED;
int main() {
	//file("tree");
	//ios::sync_with_stdio(false), cin.tie(nullptr);
	ST = clock(), solution(), ED = clock();
	//cerr << 1000 * (ED - ST) / CLOCKS_PER_SEC << "ms\n";
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 28452
final_rank: 7

code_snippet: // by xm
// 2024.11.7
#include <bits/stdc++.h>
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout);
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll N = 2e5 + 5, Mod = 998244353;
const ll inv4 = 748683265, inv8 = 873463809, inv16 = 935854081;

ll n, res1, res2, res3, degT[N], degU[N];

struct pair_hash {
	template <class T1, class T2>
	size_t operator () (pair<T1, T2> const &pair) const {
		size_t h1 = hash<T1>()(pair.first), h2 = hash<T2>()(pair.second);
		return h1 ^ h2;
	}
};

void solution() {
 	cin >> n;
 	unordered_set<pll, pair_hash> T, U;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degT[u]++; degT[v]++;
		T.insert(minmax({u, v}));
	}
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degU[u]++; degU[v]++;
		U.insert(minmax({u, v}));
	}
	res1 = n * (n - 1) % Mod;
	
	for(auto edge : T) {
		ll u = edge.first, v = edge.second;
		ll val = n - 1 - degU[u] - degU[v] + (U.count(edge));
		res3 += val;
		res3 %= Mod;
	}
	for(int i = 1; i <= n; i++) {
		res2 += n - 1 - degT[i];
		res2 += n - 1 - degU[i];
		res2 %= Mod;
	}
	
	ll ans = res1 * inv4 - res2 * inv8 + res3 * inv16;
	ans = (ans % Mod + Mod) % Mod;
	cout << ans << "\n";
	
	// cout << res1 % Mod * inv4 % Mod << "\n";
	// cout << res2 % Mod * inv8 % Mod << "\n";
	// cout << res3 % Mod * inv16 % Mod << "\n";
}

double ST, ED;
int main() {
	//file("tree");
	//ios::sync_with_stdio(false), cin.tie(nullptr);
	//ST = clock(), solution(), ED = clock();
	solution(); clock();
	//cerr << 1000 * (ED - ST) / CLOCKS_PER_SEC << "ms\n";
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 28460
final_rank: 8

code_snippet: // by xm
// 2024.11.7
#include <bits/stdc++.h>
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout);
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

const ll N = 2e5 + 5, Mod = 998244353;
const ll inv4 = 748683265, inv8 = 873463809, inv16 = 935854081;

ll n, res1, res2, res3, degT[N], degU[N];

struct pair_hash {
	template <class T1, class T2>
	size_t operator () (pair<T1, T2> const &pair) const {
		size_t h1 = hash<T1>()(pair.first), h2 = hash<T2>()(pair.second);
		return h1 ^ h2;
	}
};

void solution() {
 	cin >> n;
 	unordered_set<pll, pair_hash> T, U;
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degT[u]++; degT[v]++;
		T.insert(minmax({u, v}));
	}
	for(int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		degU[u]++; degU[v]++;
		U.insert(minmax({u, v}));
	}
	res1 = n * (n - 1) % Mod;
	
	for(auto edge : T) {
		ll u = edge.first, v = edge.second;
		ll val = n - 1 - degU[u] - degU[v] + (U.count(edge));
		res3 += val;
		res3 %= Mod;
	}
	for(int i = 1; i <= n; i++) {
		res2 += n - 1 - degT[i];
		res2 += n - 1 - degU[i];
		res2 %= Mod;
	}
	
	ll ans = res1 * inv4 - res2 * inv8 + res3 * inv16;
	ans = (ans % Mod + Mod) % Mod;
	cout << ans << "\n";
	
	// cout << res1 % Mod * inv4 % Mod << "\n";
	// cout << res2 % Mod * inv8 % Mod << "\n";
	// cout << res3 % Mod * inv16 % Mod << "\n";
}

double ST, ED;
int main() {
	//file("tree");
	//ios::sync_with_stdio(false), cin.tie(nullptr);
	//ST = clock(), solution(), ED = clock();
	solution();
	//cerr << 1000 * (ED - ST) / CLOCKS_PER_SEC << "ms\n";
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 28436
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
set<pair<int, int>> s;
int n, ans, d[200005], fac[200005], inv[200005];
int fpow(int x, int y) {
	int s = 1;
	while(y) {
		if(y & 1)
			s = 1ll * s * x % mod;
		x = 1ll * x * x % mod, y >>= 1;
	}
	return s;
}
int c(int n, int m) {
	if(min({n, m, n - m}) < 0)
		return 0;
	return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n, fac[0] = 1;
	for(int i = 1; i <= n; i++)
		fac[i] = 1ll * fac[i - 1] * i % mod;
	inv[n] = fpow(fac[n], mod - 2);
	for(int i = n - 1; ~i; i--)
		inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
	for(int i = 1, x, y; i < n; i++)
		cin >> x >> y, d[x]++, d[y]++, s.insert({x, y}), s.insert({y, x});
	for(int i = 0; i <= n; i++)
		ans = (ans + 1ll * c(n, i) * i % mod * (n - i) % mod) % mod;
	for(int i = 0; i <= n - 2; i++)
		ans = (ans + mod - 2ll * c(n - 2, i) % mod * i % mod * (n - 1) % mod) % mod;
	int cnt = n < 4 ? 0 : fpow(2, n - 4);
	for(int i = 1, x, y; i < n; i++) {
		cin >> x >> y;
		int tmp = n - 1 - d[x] - d[y];
		if(s.find({x, y}) != s.end())
			tmp++;
		ans = (ans + 1ll * tmp * cnt % mod) % mod;
	}
	cout << 1ll * ans * fpow(fpow(2, n), mod - 2) % mod << '\n';
	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 24420
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll > pll;
const int mod=998244353;
const int MAXN=200011;
set<ll> a[MAXN],b[MAXN];
ll fac[MAXN],inv[MAXN];
ll n;
ll read() {
	ll x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9')
		x = x * 10 + ch - '0', ch = getchar();
	return x * f;
}
ll max(ll a,ll b){
	return a>b?a:b;
}
ll min(ll a,ll b){
	return a<b?a:b;
}
void umax(ll &a,ll t){
	if(t>a) a=t; 
}
void umin(ll &a,ll t){
	if(t<a) a=t;
}
ll Qpow(ll a,ll p=mod-2){
	if(p<0) return 0;
	ll res=1;
	while(p){
		if(p&1) res=res*a%mod;
		a=a*a%mod;
		p>>=1;
	}
	return res;
}
void add(ll &x,ll y){
	x=(x+y)%mod;
}
ll C(ll n,ll m){
	return n<m?0:fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int main(){
//	freopen("tree.in","r",stdin);
//	freopen("tree.out","w",stdout);
	n=read();
	if(n==1) return puts("0"),0;
	fac[0]=1;
	inv[0]=1;
	for(ll i=1;i<=n;i++){
		fac[i]=fac[i-1]*i%mod;
		inv[i]=Qpow(fac[i]);
	}
	for(ll i=1;i<n;i++){
		ll u=read(),v=read();
		a[u].insert(v),a[v].insert(u);
	}
	for(ll i=1;i<n;i++){
		ll u=read(),v=read();
		b[u].insert(v),b[v].insert(u);
	}
	ll ans=0;
	for(ll i=1;i<=n;i++){
		add(ans,C(n,i)*i%mod*(n-i));
		add(ans,2*(mod-(C(n-2,i)*i%mod*(n-1)%mod)));
	}
	ll ctrb=(n<4?0:Qpow(2,n-4));
	for(ll u=1;u<=n;u++){
		for(auto v:a[u]){
			if(u>v) continue;
			add(ans,ctrb*(n-1-b[u].size()-b[v].size()+b[u].count(v))%mod);
		}
		
	}
	for(ll i=1;i<=n;i++){
		ans=ans*inv[2]%mod;
	}
	printf("%lld\n",ans);
	return 0;
}
accuracy: 1
time_ms: 220
memory_kb: 62940
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(v) v.begin(), v.end()
#define rng(i, l, r) for(int i = int(l); i < int(r); i++)
#define rep(i, n) rng(i, 0, n)
#define sz(v) int(v.size())
#define foa(s, v) for(auto& s : v)

constexpr ll mod = 998244353;

using pii = pair<int, int>;

// using mint = long double;

struct mint {
	ll x;
	mint(ll y = 0) : x(y % mod) {
		if(x < 0) x += mod;
	}

	mint operator+(mint lef) { return x + lef.x; }
	mint operator*(mint lef) { return x * lef.x; }
	mint operator-(mint lef) { return x - lef.x; }
	mint operator-() { return -x; }

	mint& operator+=(mint y) { return *this = *this + y; }
	mint& operator-=(mint y) { return *this = *this - y; }
	mint& operator*=(mint y) { return *this = *this * y; }
	mint& operator/=(mint y) { return *this = *this / y; }

	mint pow(ll n) {
		mint ret = 1;
		mint mult = x;
		while(n) {
			if(n & 1) ret *= mult;
			n >>= 1;
			mult *= mult;
		}
		return ret;
	}

	mint inv() { return pow(mod - 2); }

	mint operator/(mint lef) {
		assert(lef.x);
		return lef.inv() * x;
	}
};

// ostream& operator<<(ostream& os, mint x) { return os << x.x; }

#ifdef LOCAL
#define debug(x) cerr << #x << ":, " << x << endl
#else
#define debug(x) void(0)
#endif

constexpr int clen = 7123456;

vector<mint> fac(clen, 1);
auto inv = fac, finv = fac;
mint comb(int n, int k) {
	if(k < 0 or n < k) return 0;
	return fac.at(n) * finv.at(k) * finv.at(n - k);
}

int n;

void solve() {
	vector<vector<int>> g(n), h(n);
	rep(i, n - 1) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		g[a].push_back(b);
		g[b].push_back(a);
	}

	rep(i, n - 1) {
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		h[a].push_back(b);
		h[b].push_back(a);
	}

	constexpr int rt = 0;

	auto make_rt = [&rt](vector<vector<int>> graph) {
		vector<vector<int>> ret(n);

		auto dfs = [&](int now, auto self, int par) -> void {
			foa(e, graph[now]) {
				if(e != par) {
					self(e, self, now);
					ret[now].push_back(e);
				}
			}
			return;
		};
		dfs(rt, dfs, -1);
		return ret;
	};
	// todo n=1 baaiwake

	g = make_rt(g);
	h = make_rt(h);
	vector<int> pg(n, -1), ph(n, -1);
	set<pii> es;
	mint res = 0;
	rep(i, n) {
		foa(to, g[i]) {
			if(i == 0) {
				res += mint(1) / 4;
			} else {
				res += mint(1) / 8;
			}
			pg[to] = i;
		}
	}
	rep(i, n) {
		foa(to, h[i]) {
			if(i == 0) {
				res += mint(1) / 4;
			} else {
				res += mint(1) / 8;
			}
			ph[to] = i;
			es.insert({i, to});
		}
	}
	rng(i, 1, n) {
		ll al = n - 2;
		mint tmp = 0;
		auto p = pg[i];
		if(p > 0) {
			if(es.count({i, p})) {
				tmp += mint(1) / 4;
			} else {
				tmp += mint(1) / 8;
			}
			al--;
		}
		{
			int oth2 = sz(h[i]) - es.count({i, p});
			al -= oth2;
			tmp += mint(oth2) / 8;
		}
		int oth = sz(h[p]) - es.count({p, i});
		al -= oth;
		tmp += mint(al) / 16;
		res += tmp;
	}
	std::cout << res.x << endl;
}

int main() {
	rng(i, 2, clen) {
		fac[i] = fac[i - 1] * i;
		inv[i] = -inv[mod % i] * (mod / i);
		finv[i] = finv[i - 1] * inv[i];
	}
	while(cin >> n) solve();
}
accuracy: 1
time_ms: 670
memory_kb: 228424
final_rank: 19

code_snippet: #include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;
const int N = 2e5 + 10, mod = 998244353;
typedef long long ll;
ll n, ans;
ll deg[N];
set<int> s[N];
ll quick_pow(ll a, ll b) {
  ll ret = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      ret = ret * a % mod;
    a = a * a % mod;
  }
  return ret;
}
int main() {
  cin >> n;
  rep(i, 1, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    deg[x]++, deg[y]++;
    s[x].insert(y), s[y].insert(x);
  }
  ll inv2 = quick_pow(2, mod - 2), inv4 = quick_pow(4, mod - 2);
  ans = n * (n - 1) % mod * inv4 % mod;
  ans =
      (ans - 2 * (n - 1) * (n - 2) % mod * inv2 % mod * inv4 % mod + mod) % mod;
  rep(i, 1, n - 1) {
    int x, y;
    scanf("%d%d", &x, &y);
    ll t = s[x].size() + s[y].size() - (s[x].find(y) != s[x].end());
    ans = (ans + (n - 1 - t) * inv4 % mod * inv4 % mod) % mod;
  }
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 32896
final_rank: 11

code_snippet: #include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;

const int N = 2e5 + 10, mod = 998244353;
typedef long long ll;
ll n, ans;
ll deg[N];
set<int> s[N];

ll quick_pow(ll a, ll b) {
    ll ret = 1;
    for (; b; b >>= 1) {
        if (b & 1) ret = ret * a % mod;
        a = a * a % mod;
    } return ret;
}

int main() {
    cin >> n;
    rep(i, 1, n - 1) {
        int x, y; scanf("%d%d", &x, &y);
        deg[x]++, deg[y]++;
        s[x].insert(y), s[y].insert(x);
    }
    ll inv2 = quick_pow(2, mod - 2), inv4 = quick_pow(4, mod - 2);
    ans = n * (n - 1) % mod * inv4 % mod;
    ans = (ans - 2 * (n - 1) * (n - 2) % mod * inv2 % mod * inv4 % mod + mod) % mod;
    rep(i, 1, n - 1) {
        int x, y; scanf("%d%d", &x, &y);
        ll t = s[x].size() + s[y].size() - (s[x].find(y) != s[x].end());
        ans = (ans + (n - 1 - t) * inv4 % mod * inv4 % mod) % mod;
    }
    printf("%lld\n", ans);
    return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 32904
final_rank: 9

code_snippet: #include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
typedef long long ll;
typedef unsigned un;
typedef std::string str;
typedef std::pair<ll,ll> pll;
ll read(){ll x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return f*x;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
void umax(ll& a,ll t){if(t>a)a=t;}
void umin(ll& a,ll t){if(t<a)a=t;}
const ll yg=998244353;
ll Qpow(ll a,ll p=yg-2)
{
	if(p<0)return 0;
	ll res=1;
	while(p)
	{
		if(p&1)res=res*a%yg;
		a=a*a%yg,p>>=1;
	}
	return res;
}
#define MAXN 200011
std::set<ll>a[MAXN],b[MAXN];
void add(ll& x,ll y){x=(x+y)%yg;}
ll fac[MAXN],inv[MAXN];
ll C(ll n,ll m){return n<m?0:fac[n]*inv[m]%yg*inv[n-m]%yg;}
int main()
{
	ll n=read();
	if(n==1)return puts("0"),0;
	fac[0]=1,inv[0]=1;
	for(ll i=1;i<=n;++i)fac[i]=fac[i-1]*i%yg,inv[i]=Qpow(fac[i]);
	for(ll i=1;i<n;++i){ll u=read(),v=read();a[u].insert(v),a[v].insert(u);}
	for(ll i=1;i<n;++i){ll u=read(),v=read();b[u].insert(v),b[v].insert(u);}
	ll ans=0;//(V1-E1)*(V2-E2)
	for(ll i=1;i<=n;++i)
		add(ans,C(n,i)*i%yg*(n-i)),add(ans,2*(yg-(C(n-2,i)*i%yg*(n-1)%yg)));
	ll ctrb=(n<4?0:Qpow(2,n-4));
	for(ll u=1;u<=n;++u)
		for(auto v:a[u])
		{
			if(u>v)continue;
			add(ans,ctrb*(n-1-b[u].size()-b[v].size()+b[u].count(v))%yg);
		}
	for(ll i=1;i<=n;++i)ans=ans*inv[2]%yg;
	printf("%lld\n",ans);
	return 0;
}
accuracy: 1
time_ms: 310
memory_kb: 62572
final_rank: 17

code_snippet: /*program by mangoyang*/
#pragma GCC optimize("Ofast", "inline")
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
    int ch = 0, f = 0; x = 0;
    for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
    for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    if(f) x = -x;
}
const int N = 200005, mod = 998244353, inv2 = (mod + 1) / 2;
map<int, int> mp[N];
vector<pair<int, int> > vec;
int deg[N], dd[N], n;
inline void up(int &x, int y){
    x = x + y >= mod ? x + y - mod : x + y;
}
int main(){
    read(n);
    for(int i = 1, x, y; i < n; i++){
        read(x), read(y);
        dd[x]++, dd[y]++;
        vec.push_back(make_pair(x, y));
    }
    for(int i = 1, x, y; i < n; i++){
        read(x), read(y);
        deg[x]++, deg[y]++;
        mp[x][y] = mp[y][x] = 1;
    }
    int ans = 1ll * n * (n - 1) % mod * inv2 % mod * inv2 % mod;
    for(int i = 1; i <= n; i++){
        int tmp = 1ll * (n - 1 - deg[i]) * inv2 % mod;
        tmp = 1ll * tmp * inv2 % mod;
        up(ans, mod - 1ll * tmp * inv2 % mod);
        tmp = 1ll * (n - 1 - dd[i]) * inv2 % mod;
        tmp = 1ll * tmp * inv2 % mod;
        up(ans, mod - 1ll * tmp * inv2 % mod);
    }
    for(auto now : vec){
        int x = now.first, y = now.second;
        int tmp = 1ll * (n - 1 - (deg[x] + deg[y] - mp[x][y])) * inv2 % mod;
        tmp = 1ll * tmp * inv2 % mod * inv2 % mod * inv2 % mod;
        up(ans, tmp);
    }
    cout << ans << endl;
    //V1 * V2 = n * (n - 1) / 4;
    //V1 * E1 = \sum (n - 1 - deg[x]) / 8;
    //E1 * E2 = \sum (n -  1 - (deg[x] + deg[y] - mp[x][y])) / 16; 
    return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 43420
final_rank: 14

code_snippet: #include<bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

template <typename T> bool chkmax(T &x,T y){return x<y?x=y,true:false;}
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,true:false;}

int readint(){
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}

const int cys=998244353;
int n;
int du1[200005],du2[200005];
map<int,int> adj1[200005];
vector<int> adj2[200005];

ll qpow(ll x,ll p){
	ll ret=1;
	for(;p;p>>=1,x=x*x%cys) if(p&1) ret=ret*x%cys;
	return ret;
}

int main(){
	n=readint();
	int x,y;
	for(int i=1;i<n;i++){
		x=readint(); y=readint();
		adj1[x][y]=adj1[y][x]=1;
		du1[x]++,du1[y]++;
	}
	for(int i=1;i<n;i++){
		x=readint(); y=readint();
		adj2[x].pb(y),adj2[y].pb(x);
		du2[x]++,du2[y]++;
	}
	ll inv2=qpow(2,cys-2),inv4=qpow(4,cys-2),ans=0;
	for(int i=1;i<=n;i++) ans=(ans+((n-1)*inv2%cys-(n-du1[i]-1)*inv4%cys)*inv2)%cys;
	for(int i=1;i<=n;i++){
		for(auto v:adj2[i]){
			if(i<v) continue;
			if(adj1[i].count(v)) ans=(ans-((n-2)*inv2%cys-(n-du1[i]-du1[v])*inv4%cys)*inv4)%cys;
			else ans=(ans-((n-2)*inv2%cys-(n-du1[i]-du1[v]-1)*inv4%cys)*inv4)%cys;
		}
	}
	printf("%lld\n",(ans+cys)%cys);
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 44496
final_rank: 13

code_snippet: #include <bits/stdc++.h>
#define ll long long
#define maxn 200005 /*rem*/
#define mod 998244353
#define db double
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define fi first
#define se second
using namespace std;
ll ksm(ll a, ll b) {
   if (!b) return 1;
   ll ns = ksm(a, b >> 1);
   ns = ns * ns % mod;
   if (b & 1) ns = ns * a % mod;
   return ns;
}
ll b2 = (mod + 1) / 2;
int d[2][maxn];
map<pi, int> hv[2];
int u[2][maxn], v[2][maxn];
ll p[10];
int main() {
	int n;
	cin >> n;
	for (int j = 0; j < 2; j++)
		for (int k = 0; k < n - 1; k++) {
			int a, b;
			scanf("%d%d", &a, &b);
			u[j][k] = a, v[j][k] = b;
			hv[j][mp(a, b)] = 1;
			hv[j][mp(b, a)] = 1;
			d[j][a]++, d[j][b]++;
		}
	p[0] = 1;
	for (int i = 1; i < 10; i++)
		p[i] = p[i - 1] * b2 % mod;
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		// 遍数
		ll cu = d[0][i] - 1;
		ll nr = p[1] + 1ll * p[2] * (n - 2 - (d[1][i] - 1)) - 1ll * p[3] * (n - 1 - d[1][i]);
		nr %= mod;
		ans = (ans + cu * nr) % mod;
	}
	for (int i = 0; i < n - 1; i++) {
		int a = u[0][i], b = v[0][i];
		ll cu = -1;
		int es = n - 1 - d[1][a] - d[1][b];
		if (hv[1][mp(a, b)]) es++;
		ll nr = p[2] + 1ll * p[3] * (n - 2 - (d[1][a] - 1) - (d[1][b] - 1)) - 1ll * p[4] * es;
		nr %= mod;
		ans = (ans + cu * nr) % mod;
	}
	ll cu = 1;
	ll nr = p[0] + p[1] * (n - 2) - p[2] * (n - 1);
	nr %= mod;
	ans = (ans + cu * nr) % mod;
	if (ans < 0) ans += mod;
	cout << ans << endl; 
	return 0;
}
accuracy: 1
time_ms: 550
memory_kb: 70324
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
mt19937 mrand(random_device{}()); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=201000;

int n,u,v,deg2[N];
vector<PII> e1;
set<PII> e2;
int main() {
	scanf("%d",&n);
	rep(i,1,n) {
		scanf("%d%d",&u,&v);
		if (u>v) swap(u,v);
		e1.pb(mp(u,v));
	}
	rep(i,1,n) {
		scanf("%d%d",&u,&v);
		deg2[u]++; deg2[v]++;
		if (u>v) swap(u,v);
		e2.insert(mp(u,v));
	}
	int x=(n-1)*powmod(2,mod-2)%mod;
	ll y=0;
	for (auto f:e1) {
		y+=n-1-deg2[f.fi]-deg2[f.se]+e2.count(f);
	}
	y=y%mod;
	x=(x+y*powmod(16,mod-2))%mod;
	printf("%d\n",x);
}
accuracy: 1
time_ms: 150
memory_kb: 14712
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
mt19937 mrand(random_device{}()); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=201000;

int n,u,v,deg2[N];
vector<PII> e1;
set<PII> e2;
int main() {
	scanf("%d",&n);
	rep(i,1,n) {
		scanf("%d%d",&u,&v);
		if (u>v) swap(u,v);
		e1.pb(mp(u,v));
	}
	rep(i,1,n) {
		scanf("%d%d",&u,&v);
		deg2[u]++; deg2[v]++;
		if (u>v) swap(u,v);
		e2.insert(mp(u,v));
	}
	int x=(n-1)*powmod(2,mod-2)%mod;
	int y=0;
	for (auto f:e1) {
		y+=n-1-deg2[f.fi]-deg2[f.se]+e2.count(f);
	}
	
	x=(x+y*powmod(16,mod-2))%mod;
	printf("%d\n",x);
}
accuracy: 0.16
time_ms: 70
memory_kb: 10224
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T, class U> using Pa = pair<T, U>;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;

constexpr ll mod = 998244353;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    mint two = 2;
    mint tinv = two.inv();
    vec<int> degX(N,0),degY(N,0);
    set<Pa<int,int>> X,Y;
    for(int i=0;i<N-1;i++){
        int a,b;
        cin >> a >> b;
        degX[--a]++; degX[--b]++;
        X.insert(minmax({a,b}));
    }
    for(int i=0;i<N-1;i++){
        int a,b;
        cin >> a >> b;
        degY[--a]++; degY[--b]++;
        Y.insert(minmax({a,b}));
    }
    mint ans = tinv.pow(2)*N*(N-1);
    mint cnt = 0;
    for(auto& e:X){
        int a = e.first,b = e.second;
        ll c = N-1-degY[a]-degY[b]+(Y.count(e));
        cnt += c;
    }
    ans += tinv.pow(4)*cnt;
    cnt = 0;
    for(int i=0;i<N;i++){
        cnt += N-1-degY[i];
        cnt += N-1-degX[i];
    }
    ans -= tinv.pow(3)*cnt;
    cout << ans << "\n";
}
accuracy: 1
time_ms: 190
memory_kb: 23168
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using PLL = pair<ll, ll>;
template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;

const ll mod = 998244353;

ll mod_pow(ll n, ll k) { return k ? (k & 1 ? n : 1) * mod_pow(n * n % mod, k / 2) % mod : 1; }

ll mod_inv(ll n) { return mod_pow(n, mod - 2); }

ll N;

void dfs(ll cur, ll pre, V<ll> &parents, const VV<ll> &edges) {
    parents[cur] = pre;
    for(ll nxt : edges[cur]) if(nxt != pre) dfs(nxt, cur, parents, edges);
}

void input(VV<ll> &edges) {
    for(ll i = 1; i < N; i++) {
        ll a, b;
        cin >> a >> b;
        a--; b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
}

int main() {
    cin >> N;
    VV<ll> tedges(N), uedges(N);
    V<ll> tpar(N), upar(N);
    input(tedges);
    input(uedges);

    dfs(0, -1, tpar, tedges);
    dfs(0, -1, upar, uedges);

    ll inv2 = mod_inv(2);
    ll inv4 = inv2 * inv2 % mod;
    
    ll ans = 0;
    ans += uedges[0].size() * inv2 % mod;
    ans += (N - (1 + uedges[0].size())) * inv4 % mod;
    ans = ans % mod * inv2 % mod;

    for(ll i = 0; i < N; i++) {
        sort(uedges[i].begin(), uedges[i].end());
        auto tmp = move(uedges[i]);
        uedges[i].clear();
        for(ll e : tmp) if(e != upar[i]) uedges[i].push_back(e);
    }

    // // cerr << inv2 << ", " << inv4 << endl;
    // // cerr << ans << endl;

    for(ll i = 1; i < N; i++) {
        ll p = tpar[i];
        ll add_v = 0;
        ll oth = N - (uedges[i].size() + uedges[p].size());
        
        {
            ll a[] = { i, p };
            ll b[] = { 0, i, p };
            sort(b, b + 3);
            auto *ptr = unique(b, b + 3);
            ll upper = ptr - b;
            oth -= upper;  // root, i, p
            for(ll j = 0; j < 2; j++) for(ll k = 0; k < upper; k++) {
                auto &v = uedges[a[j]];
                auto ite = lower_bound(v.begin(), v.end(), b[k]);
                if(ite != v.end() && *ite == b[k]) oth++;
            }
        }

        //oth=count_nodes(i,p);
        // cerr << i << ", " << p << ", " << oth << endl;

        // p
        if(upar[p] == i || p == 0) add_v += 1;
        else add_v += inv2;
        
        // root
        if(p) add_v += inv2;

        // cerr << add_v << endl;

        // i's children
        add_v += (uedges[i].size() - (upar[p] == i)) * inv2 % mod;
        
        // cerr << uedges[i].size() << ", " << add_v << endl;

        // oth
        add_v += oth * inv4 % mod;
        
        // cerr << add_v << endl;

        add_v = add_v % mod * inv4 % mod;  // i
        // // cerr << add_v << endl;
        (ans += add_v) %= mod;
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 330
memory_kb: 39780
final_rank: 16