All problems

Problem 344

Catalan Number Modulo

Medium · 20 pts · Math, Combinatorics, Dynamic Programming

Problem

The Catalan numbers count many structures: the number of valid strings of N pairs of parentheses, the number of binary search trees with N keys, and the number of ways to triangulate a polygon with N + 2 sides. They start 1, 1, 2, 5, 14, 42 for N = 0, 1, 2, 3, 4, 5, and C(N) = (2N)! / ((N + 1)! · N!). Print C(N) modulo 1000000007. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

A single integer N.

Output

Print the N-th Catalan number modulo 1000000007.

Constraints

0 <= N <= 100000

Example input

3

Example output

5

Explanation

There are 5 valid strings of 3 pairs of parentheses: ((())), (()()), (())(), ()(()) and ()()(). So C(3) = 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Josephus Problem Next: Count Multiples in a Range