Lets place the stationary mass $M$ a the origin. In both cases the gravitational force is towards the origin, and is given by
$$\vec{F}_{g}=-\frac{GMm}{r^{2}}\hat{r}$$
Thus in the first case, the work done by the gravitational force is
$$W_{r\rightarrow\infty}=\int_{r}^{\infty}\vec{F}_{g}\cdot\vec{dr}=-\int_{r}^{\infty}\frac{GMm}{r^{2}}dr=-\frac{GMm}{r}$$
On the other hand, in the second case we just need to sweep the limits of integration
$$W_{\infty\rightarrow r}=\int_{\infty}^{r}\vec{F}_{g}\cdot\vec{dr}=-\int_{\infty}^{r}\frac{GMm}{r^{2}}dr=\frac{GMm}{r}$$
which is exactly the opposite, and the signs are just what you've anticipated. An important thing to note is that also in both cases
$$\vec{dr}=dr\hat{r}$$
and the sign is set by the integration limits. So in the first case $dr>0$ and in the second $dr<0$ (in a very informal manner). You ignored this fact when you wrote $\cos\left(180^{\circ}\right)$ in the dot product in the second case.
In this video by Flammable Maths, the solution to a similar problem is given.
The only difference is that we just need to include the electrostatic force, besides that the process is exactly the same.
Let's say we have two objects $1$ and $2$ with mass $m_1,m_2$ and charge $q_1,q_2$ respectivey separated by distance $R$ then-
$$\textstyle\displaystyle{F=F_C+F_G=\frac{Gm_1m_2+kq_1q_2}{R^2}}$$
Where $G$ is the Newtonian constant of gravitation and $$\textstyle\displaystyle{k=\frac{1}{4\pi\epsilon_0}}$$
By newton's third law we have $F_{12}=-F_{21}$ so
$$\textstyle\displaystyle{F_{12}=\frac{Gm_1m_2+kq_1q_2}{(r_2-r_1)^2}=m_1\frac{d^2r_1}{dt^2}}$$
$$\textstyle\displaystyle{F_{21}=-\frac{Gm_1m_2+kq_1q_2}{(r_2-r_1)^2}=m_2\frac{d^2r_2}{dt^2}}$$
Where $R=r_2-r_1$
$$\therefore\textstyle\displaystyle{\frac{d^2r_2}{dt^2}-\frac{d^2r_1}{dt^2}}$$
$$\textstyle\displaystyle{=-\frac{Gm_1m_2+kq_1q_2}{(r_2-r_1)^2}\bigg(\frac{1}{m_1}+\frac{1}{m_2}\bigg)}$$
$$\implies\textstyle\displaystyle{\frac{d^2R}{dt^2}=-\frac{\kappa}{R^2}}$$
Now we just need to solve this differential equation-
$$\textstyle\displaystyle{\frac{dv}{dt}=-\frac{\kappa}{R^2}=\frac{dv}{dR}\frac{dR}{dt}}$$
$$\implies\textstyle\displaystyle{-\frac{\kappa}{R^2}=v\frac{dv}{dR}}$$
$$\implies\textstyle\displaystyle{-\kappa\int\frac{1}{R^2}dR=\int vdv}$$
At $t=0$, $R(0)=R_i$ [The initial radius]
$v(0)=0$ [velocity at the beginning]
$$\therefore\textstyle\displaystyle{\int_{0}^{v(t)}vdv=-\kappa\int_{R_i}^{R(t)}R^{-2}dR}$$
$$\implies\textstyle\displaystyle{\frac{v^2}{2}=\kappa\bigg(\frac{1}{R}-\frac{1}{R_i}\bigg)}$$
$$\implies\textstyle\displaystyle{v=\frac{dR}{dt}=\pm\sqrt{2\kappa\bigg(\frac{R_i-R}{R_iR}\bigg)}}$$
$$\implies\textstyle\displaystyle{\int_{0}^{T_c}dt=\pm\int_{R_i}^{0}\frac{1}{\sqrt{2\kappa\bigg(\frac{R_i-R}{R_iR}\bigg)}}dR}$$
$$\implies\textstyle\displaystyle{T_c=\pm\sqrt{\frac{R_i}{2\kappa}}\int_{R_i}^{0}\sqrt{\frac{R}{R_i-R}}dR}$$
Solving the integral is simple, if you would like to see the steps then see here. Noting that time can't be negative, we have-
$$\textstyle\displaystyle{T_c=\frac{\pi}{2}\sqrt{\frac{R^3}{2\kappa}}}$$
Now simply substituting the value for $\kappa$ and $k$ gives us less cleaner formula-
$$\textstyle\displaystyle{T_c=\sqrt{\frac{\pi^3\epsilon_0m_1m_2R^3}{2(m_1+m_2)(4\pi\epsilon_0Gm_1m_2+q_1q_2)}}}$$
Best Answer
It's just convention. The equations are written down to describe phenomena, and the notion is set up in such a way that we understand what the directions mean. If it makes you feel more secure, we could write down $F_{e} = k\frac{q_{1}q_{2}}{r^{2}}{\hat r}$ and $F_{g} = -G\frac{m_{1}m_{2}}{r^{2}}{\hat r}$, but there's no need to, because we all know that gravity is always attractive and that opposite charges attract and that alike charges repel.