Solution (source code)

= Solution

Using <Einstein notation> and the <Kronecker delta>,
$$
\operatorname{tr}M=M_{ii}=\delta_{ii}-\hat n_i\hat n_i=3-\|\hat{\mathbf n}\|^2=\boxed{2}.
$$
The <unit vector> condition is essential in the last step.