Fix some $\epsilon>0$ and define for $x\in\mathbb{R}$: $h_1(x)=x$, $h_2(x)=M+\epsilon$ and $h_3(x)=-M-\epsilon$. Let $U_1=(-\epsilon-M,M+\epsilon)$, $U_2=(M,\infty)$ and $U_3=(-\infty,-M)$.
Let $\\{\phi_i,U_i\\}_{i=1}^3$ be a partition of unit associated with $U_i$, i.e.
I - $\phi_i\in C^{\infty}(\mathbb{R})$,
II - $\operatorname{spt}\phi_i\subset U_i$ and $\sum_{i=1}^3\phi_i=1$
Define for $x\in\mathbb{R}$ $$h(x)=\phi_1h_1+\phi_2h_2+\phi_3h_3$$
Note that $h$ is the desired function.
Remark: To make things more clear, note that it is possible to choose $\phi_i$ in such a way that $\phi_1$ and $\phi_3$ are strictly decreasing in $(M,M+\epsilon)$ and $(-\epsilon-M,-M)$ respectively and $\phi_1$ and $\phi_2$ are strictly increasing in $(-\epsilon-M,-M)$ and $(M,M+\epsilon)$ respectively.