Suppose $f\in C^\infty(\mathbb{R})$ and for any $x\in\mathbb{R}$, there exists $N\in\mathbb{N}$ such that $f^{(N)}(x)=0$. Show that $f$ is a polynomial.

This is from one of the Analysis qualifying exam problems. I can show there exists an interval $(a,b)$ on which $f$ is a polynomial by using Baire category theorem, but I can't extend it to the real line. Any suggestion?

I think I get some new ideas about this problem. First I can find an interval $I=(a,b)$ by using Baire theorem (same idea from the question that Clement C. added.) where $f$ coincides with a polynomial $g$ on $I$. Then we consider $f^{(i)}(a), i\leq N_g$ where $N_g$ is the degree of $g$. We must have $f^{(N_g)}(a)=0$ because $f^{(i)}(a^+)=f^{(i)}(a)=g^{(i)}(a), i\in\mathbb{N}$. So if we apply the Taylor's theorem at $x=a$, we can extend $I=(a,b)$ to $I_\epsilon=(a-\epsilon,b)$ such that $f=g$ on $I_\epsilon$. Following the same idea, we can show that $$\inf\{a:f=g \text{ on } (a,b) \}=-\infty$$ Similarly we can show $f=g$ on $\mathbb{R}$.

Update: I just found that the idea above might not work after I was trying to write down a rigorous proof. The main problem is $C^\infty$ function is not neccesarily analytic. (For example, let $f(x)=0$ on $(-\infty,0]$ and $f(x)=e^{-1/x}$ on $(0,\infty)$). So I can only extend $(a,b)$ to some larger (or equal) interval $(c,d)$ if I only use the $C^\infty$ property. So the claim above $$\inf\{a:f=g \text{ on } (a,b) \}=-\infty$$ might not be true.

I really have no idea how to extend this interval to the real line now.