A converse of sorts to the intermediate value theorem, with an additional property I need to solve the following problem:

Suppose $f$ has the intermediate value property, i.e. if $f(a)<c<f(b)$, then there exists a value $d$ between $a$ and $b$ for which $f(d)=c$, and also has the additional property that $f^{-1}(a)$ is closed for every $a$ in a dense subset of $\mathbb{R}$, then $f$ is continuous.

I can see plenty of counterexamples when the second property is not added, but I can't seem to bridge the gap between adding the property and proving $f$ is continuous. I can't get there either directly or by contradiction, because the additional property doesn't seem directly relevant to the property of continuity, so could anyone please tell me how to go about doing this? Thanks!

As noted in the comments, this is a slightly more general version of a problem in Rudin. I assume for simplicity that the dense set is $\mathbb{Q}$.
Solution: Fix $x_0\in\mathbb{R}$, and fix a sequence $\{x_n\}$ converging to $x_0$. By the sequential characterization of continuity, it suffices to show that $f(x_n)\rightarrow f(x_0)$. Suppose not. Then an infinite number of the $f(x_n)$ are not equal to $f(x_0)$, and without loss of generality, we can assume there are infinitely many $n$ so that $f(x_n)>f(x_0)$. Passing to a subsequence, we can assume this is true for all $n$. 
Because the sequence $\{f(x_n\}$ does not converge to $f(x_0)$, there exists $r\in\mathbb{Q}$ with $f(x_n)>r>f(x_0)$ for all $n$. By the intermediate value property, for every $n$, there exists $t_n$ with $f(t_n)=r$ for some $t_n$ between $x_n$ and $x_0$. By the squeeze principle, $t_n\rightarrow x_0$. But the set of all $t$ with $f(t)=r$ is closed, so because $x_0$ is a limit point, $f(x_0)=r$, a contradiction.
Source: W. Rudin, Principles of Mathematical Analysis, Chapter 4, exercise 19.
