To make the product of the ball values as large as possible one needs to split up the 'budget' of $31$ into as many pieces as possible greater than $1$. Why $> 1$? Because balls with $1$ on them contribute to the sum but don't increase the product. Why as many as possible? Because $2 \cdot N \ge 2 + N$ (for $N \ge 2$), i.e. the total product will increase more by an additional factor of $2$ than by one of the factors being increased by $2$.
Edit: With regard to ffao's comment, I should add that there is exactly 1 case where the maximum number of factors is not the optimum, namely $2 \cdot 2 \cdot 2 < 3 \cdot 3$. In the final solution one will see that further 'finetuning' by replacing three $2$s by two $3$s is not possible, because there are already three $3$s (one for each basket) used.
So let's start with filling $2$s into each basket. The product is $8$ so far and the sum is $6$. Continuing with $3$s in each basket we have a product of $216$ and a sum of $15$. Next would be 3 $4$s, resulting in a product of $13824$ and a sum of $27$. But as the rest 'budget' to fill up to $31$ is just $4$, this can not be used to write another number on a ball. Instead one needs to use this to increase the existing numbers.
This increase should be spread among as many numbers as possible because $\left( N_1 + 1 \right) \left( N_2 + 1 \right) > N_1 \left( N_2 + 2 \right)$ for $N_1$, $N_2 \ge 2$. So let's first promote two $4$s to $5$s. Then we have a rest budget of $2$ from which we can promote a $2$ to $4$.
In total we then have in the baskets:
1. basket: $2, 3, 4, 5$
2. basket: $2, 3, 4, 5$
3. basket: $3$
Sum is exactly $31$ and product is $43200$.