Let's root the tree at some vertex. Now the intersection vertex is lca for at least one path.
ProofIf the vertex wasn't lca for both paths it would mean that there are either two edges going up (which is impossible in a rooted tree) or they are going up by the same edge, but this would mean that this vertex parent is also an intersection point, so the paths are intersecting in at least $$$2$$$ points, so this is impossible too.
Now there are two types of intersections: with the same lca and different lca. Let's count them independently.
For each path and it's lca let's find the subtrees that path goes to from lca. This would result in a triplet $$$(lca, subtree1, subtree2)$$$ (replace subtree with $$$-1$$$ if there is none) with $$$subtree1 < subtree2$$$ or both of them are $$$-1$$$.
Now to count the intersections of the first type let's use inclusion-exclusion principle. Remember all paths that have same lca. Now we need to calculate the number of pairs so that they have different $$$subtree1$$$ and $$$subtree2$$$ (or $$$-1$$$). The formula is going to be $$$cntpath \cdot (cntpath - 1) / 2 - \sum \left(cnt(subtree1, x) + cnt(x, subtree2) - (cnt(subtree1, subtree2) + 1) \right) / 2$$$ (from inclusion-exclusion principle) where cntpath is the number of paths with this lca and $$$cnt(x, y)$$$ is the number of paths with triplet $$$(lca, x, y)$$$. The situation with $$$-1$$$ is pretty similar, left as an exercise to the reader.
Finding the number of intersections of second type is a bit easier. We just need to calculate the number of all intersections between a path with fixed lca and a vertical path which crosses this lca (the path is not neccessarily vertical, but it contains both lca and it's parent) and then subtract $$$cnt(subtree1)$$$ and $$$cnt(subtree2)$$$, where $$$cnt(x)$$$ is the number of vertical paths that go into subtree $$$x$$$.
After that we just have to print out the sum of this two numbers. Counting all the needed functions can be done using some data structure (small-to-large for example) or some subtree dynamic programming.
The resulting complexity is $$$O(Mlog^2M)$$$ or $$$O(MlogM)$$$ or even $$$O(M)$$$ if you're strong enough.