How is the attention mechanism mathematically formulated, and how does it prevent the vanishing gradient problem?