For any positive integer $n$, let $f(n)$ be the number of permutations $a_1,\cdots, a_n$ of $1,\cdots, n$ so that $a_1=1$ and $|a_i-a_{i+1}|\leq 2$ for $1\leq i\leq n-1$. Find $f(2022)\pmod 3$.
Edit: apparently it makes more sense for the inequality to hold for $1\leq i\leq n-1$, whereas in the original source for this question, the inequality held for $2\leq i\leq n-1$. The original source is problem 12 from this problem set.
We have $f(1)=1, f(2)=1, f(3)=2$. $f(4) = 6.$ $f(5)$ does not count only those permutations where 2 and 5 are consecutive. There are $12$ such permutations, so $f(5) = 24-12 = 12.$ $f(6)$ does not count those permutations $p_{26}$ where $2$ and $6$ are adjacent, where $2$ and $5$ are adjacent, denoted $p_{25}$, or where $3$ and $6$ are adjacent, denoted $p_{36}$. Let $p_{526}$ denote the number of permutations where $2$ is adjacent to both 6 and 5, $p_{263}$ denote the number of permutations where 2 is adjacent to 6 and 3 is adjacent to 6, $p_{25,36}$ denote the number of permutations where 2 is adjacent to 5 and 3 is adjacent to 6, and let $p_{5263}$ denote the number of permutations where 2 is adjacent to 5 and 6 and 6 is adjacent to 3. One can use the inclusion-exclusion principle to deduce that $f(6) = p_{26}+p_{25}+p_{36}-p_{526}-p_{263} - p_{25,36} + p_{5263}.$ Call an index $i$ where $2\leq i\leq n-1$ invalid if $|a_i-a_{i+1}| > 2.$ Let $g(n)$ be the number of these permutations with $a_n = n$. Then $g(1)=1, g(2)=1, g(3)=1, g(4)=2.$ Clearly in any permutation counted by $g(n), a_{n-1} \ge n-2.$ In general, $a_{n-i}\geq n-2i$ for $1\leq i < n$, though clearly this bound is very rough.
Let $h(n)=f(n)-g(n)$. There should be a recurrence formula for $g(n)$ and $h(n)$, but I'm not sure how to find it.
Clarification: the question from the original source (provided in a comment) used the bounds $2\leq i\leq n-1$, so I assume that's what's intended.