#P3412. Hamster’s Random Walk on a Tree

    ID: 16667 Type: Default 1000ms 256MiB

Hamster’s Random Walk on a Tree

Hamster’s Random Walk on a Tree

A little hamster lives in an underground cave system which forms a tree. The cave system is composed of n nodes numbered from 1 to n, and any two adjacent caves are at distance 1. One day the hamster decides to walk from his own bedroom at node a to his friend’s bedroom at node b (note that a and b can be identical). However, having studied too much OI, he forgot the idea of taking a shortest path and instead walks randomly.

The process is as follows. When the hamster is in a node, if he is not yet at his friend’s room (b), he chooses one of the neighboring nodes uniformly at random and moves there. Equivalently, if the current node has degree d, then he takes one step and each neighbor is chosen with probability 1/d. As soon as he reaches node b his walk stops.

Your task is to compute the expected number of steps taken until the hamster reaches his friend’s room. It can be shown that the answer is a rational number which, in irreducible form, is y/x with x not congruent to 0 modulo 998244353. There exists a unique integer z (0 ≤ z < 998244353) such that

xzy(mod998244353),xz\equiv y \pmod{998244353},

and you are required to output z.

Note: The cave system is a tree. Hence, the hamster’s random walk is exactly the standard random walk on an undirected tree: from any node v (other than b) the hamster moves in one step to any adjacent node with probability 1/deg(v), and the process stops when he first reaches b.

If a equals b, then the expected number of steps is 0.

Input Format: See input description below.

inputFormat

The first line contains three integers n, a and b (1 ≤ a, bn) – the number of nodes and the starting and target nodes respectively. Then follow n-1 lines, each containing two integers u and v representing an edge between nodes u and v. It is guaranteed that the given graph is a tree.

outputFormat

Output a single integer z (0 ≤ z < 998244353) which is the expected number of steps modulo 998244353 in the sense described in the statement.

Note: If the expected number of steps is a rational number written in irreducible form as y/x, then z is the unique integer satisfying x*z ≡ y (mod 998244353).

sample

3 1 3
1 2
2 3
4