## Odd elimination

### Problem 539

Start from an ordered list of all integers from 1 to n. Going from left to right, remove the first number and every other number afterward until the end of the list. Repeat the procedure from right to left, removing the right most number and every other number from the numbers left. Continue removing every other numbers, alternating left to right and right to left, until a single number remains.

Starting with n = 9, we have:__1__ 2 __3__ 4 __5__ 6 __7__ 8 __9__

2 __4__ 6 __8____2__ 6

6

Let `P`(`n`) be the last number left starting with a list of length `n`.

Let $\displaystyle S(n) = \sum_{k=1}^n P(k)$.

You are given P(1)=1, P(9) = 6, P(1000)=510, S(1000)=268271.

Find S(10^{18}) mod 987654321.