Implement a function count_valid_permutations in a file called /app/solution.py. This function should calculate the number of valid permutations for drawing a tree on a circle, ensuring that the edges do not cross each other. The result should be returned modulo 998244353.
Specifications:
Function Signature:
python
def count_valid_permutations(n, edges):
Parameters:
n (int): The number of nodes in the tree. It is guaranteed that n is a positive integer.
edges (list of tuples): A list containing tuples, with each tuple having two integers. Each tuple represents an edge connecting two nodes in the tree. The nodes are 1-indexed.
Returns:
The function should return an integer, which is the number of valid permutations of the tree modulo 998244353.
Input/Output Requirements:
The function should handle cases where:
n is at least 1 and does not exceed typical computational limits for tree structures.
The edges appropriately represent a connected, acyclic graph (a tree). Valid inputs ensure no duplicate edges or self-loops.
Examples:
Input: count_valid_permutations(3, [(1, 2), (1, 3)]) should return the total valid permutations modulo 998244353.
Input: count_valid_permutations(4, [(1, 2), (2, 3), (3, 4)]) should also return the total valid permutations modulo 998244353.
Constraints:
The solution should be efficiently implemented to handle up to a reasonable number of nodes and edges to ensure quick computation.
Implement the above specifications directly in the /app/solution.py file.