Optimal modularity and memory capacity of neural networks