Two players are playing a game, alternating turns. There are $k$ piles of stones. On each turn, a player has to choose a pile and replace it with two piles of stones under the following two conditions:

The first player unable to make a valid move loses.
Let $f(n,k)$ be the number of winning positions for the first player, assuming perfect play, when the game is played with $k$ piles each having between $2$ and $n$ stones (inclusively).
$f(10,5)=40085$.

Find $f(10^7,10^{12})$.
Give your answer modulo $987654321$.