Same problem but I will ask how many valid ways to build $$$M$$$ cubes after $$$N$$$ moves mod $$$10^9 + 7$$$ and $$$N \le 10^5$$$.I know how to solve it when $$$N \le 10^3$$$ , that is through DP but how when $$$10^5$$$ ? Is it related with Catalan numbers??