Skyline

Time limit1sMemory limit128 MB

Summary
Count permutations of 1..N with no increasing subsequence of length 3, modulo 1,000,000, for each N up to 1,000.
Level

Medium5 of 10

Topics
Dynamic programming, Combinatorics, Math
Solved
No attempts yet

Problem

The director of a new movie needs to build a scaled-down set for the film. The set will contain NN skyscrapers whose heights are distinct integers from 11 to NN meters. The skyline is the sequence of the buildings' heights read from left to right, so it is a permutation of the integers from 11 to NN.

The director is extremely meticulous and wants to avoid one particular rising pattern: she does not want ANY three buildings at positions i,j,ki, j, k with i<j<ki < j < k such that the height of building ii is smaller than that of building jj and the height of building jj is smaller than that of building kk.

For a given number of buildings, determine how many distinct skyline orderings avoid the rising pattern she dislikes.

Input

The input contains several test cases. Each test case is a single line containing one integer NN (3≤N≤1,000)(3 \le N \le 1{,}000), the number of skyscrapers. Their heights are assumed to be 1,2,3,…,N1, 2, 3, \dots, N. The input ends with a line containing a single 00.

Output

For each test case, output a single integer: the number of good skylines — those that avoid the rising pattern the director dislikes — modulo 1,000,0001{,}000{,}000. Print each integer on its own line with no spaces, and do not print any blank lines between answers.

Examples3

  1. Example 1

    Input
    3
    4
    0
    
    Expected output
    5
    14
    
  2. Example 2

    Input
    5
    6
    7
    8
    9
    10
    0
    
    Expected output
    42
    132
    429
    1430
    4862
    16796
    
  3. Example 3

    Input
    3
    0
    
    Expected output
    5