What Makes In-context Learning Effective for Mathematical Reasoning: A Theoretical Analysis