Problems > Cayley's Forest

Cayley's Forest

Difficulty: 1500 4000 ms 128 MB

combinatorics graphs

Count the distinct labelled trees on N vertices, modulo 109+7 (a tree is a connected acyclic graph; two trees differ if their edge sets differ).

Input: one integer N.

Output: the number of labelled trees mod 109+7.

Constraints:

  • 1 ≤ N ≤ 1018

Sample Tests

Input 1
3
Output 1
3
Input 2
1
Output 2
1
Input 3
2
Output 3
1

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.