If $f$ counts the number of permutations satisfying some properties, find $f(2022) \pmod 3$. 
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.

 A: I'm going to answer the question,
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$,
which differs from the question as posted in having $1\leq i\leq n-1$ instead of $2\leq i\leq n-1$. The question makes more sense to me, starting at $i=1$ than at $i=2$ – see the comments. Also, the question with $i=1$ is easier, and also, you probably have to understand the $i=1$ question to be able to solve the $i=2$ question. So, here goes.
Note that $a_2$ must be two or three.
Case 1: $a_2=2$. Then $(a_2-1,a_3-1,\dots,a_n-1)$ is a permutation of $1,2,\dots,n-1$ satisfying the conditions, so there are $f(n-1)$ of these.
Case 2: $a_2=3$ and $a_3=2$. Then we must have $a_4=4$, and $(a_4-3,a_5-3,\dots,a_n-3)$ is a permutation of $1,2,\dots,n-3$ satisfying the conditions, so there are $f(n-3)$ of them.
Case 3: $a_2=3$, and $a_3\ne2$. Then $2$ is not next to $1$, and not next to $3$, so it must only be next to $4$, so we must have $a_n=2$, and $a_{n-1}=4$. Then we must have $a_3=5$, and then $a_{n-2}=6$, and so on; the permutation must be $(1,3,5,7,\dots,8,6,4,2)$.
So we have $f(n)=f(n-1)+f(n-3)+1$, with $f(1)=1$, $f(2)=1$, $f(3)=2$.
So we have $f(n)\equiv f(n-1)+f(n-3)+1\bmod3$, with $f(1)=1$, $f(2)=1$, $f(3)=2$. Using this recurrence to compute the first few terms, we get
$$
1,1,2,1,0,0,2,0,1,1,2
$$
and as soon as we see $1,1,2$ repeated, we know the sequence will be periodic, repeating $1,1,2,1,0,0,2,0$ forever (since each term is determined by the previous three terms). This has period eight, and $2022\equiv6\bmod3$, so
$$
f(2022)\equiv f(6)\equiv0\bmod3
$$
