Solution (source code)

= Solution

Choose $b\in N^m$ and an $L$-formula $\varphi(\bar x;y)$ such that
$$
X=\{\bar a\in N^n:N\models\varphi(\bar a;b)\}.
$$
Let $p(y)=\operatorname{tp}(b/M)$. Stability and the <Fundamental theorem of stability> make this a <definable type>. Apply its definition to the formula $\psi(y;\bar x)=\varphi(\bar x;y)$. There is an $M$-formula $d_p\psi(\bar x)$ such that, for every $\bar a\in M^n$,
$$
N\models\varphi(\bar a;b)
\quad\Longleftrightarrow\quad
\psi(y;\bar a)\in p
\quad\Longleftrightarrow\quad
M\models d_p\psi(\bar a).
$$
Thus $d_p\psi$ defines $X\cap M^n$ in $M$, which is the <stable trace of a definable set> property.