Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

var scanner = bufio.NewScanner(os.Stdin)

// 文字列を1行取得
func StrStdin() (stringReturned string) {
//	scanner := bufio.NewScanner(os.Stdin)

	scanner.Scan()
	stringInput := scanner.Text()

	stringReturned = strings.TrimSpace(stringInput)
	return
}

// 整数値1つ取得
func IntStdin() (int, error) {
	stringInput := StrStdin()
	return strconv.Atoi(strings.TrimSpace(stringInput))
}

// 空白や空文字だけの値を除去したSplit()
func SplitWithoutEmpty(stringTargeted string, delim string) (stringReturned []string) {
	stringSplited := strings.Split(stringTargeted, delim)

	for _, str := range stringSplited {
		if str != "" {
			stringReturned = append(stringReturned, str)
		}
	}

	return
}

// デリミタで分割して文字列スライスを取得
func SplitStrStdin(delim string) (stringReturned []string) {
	// 文字列で1行取得
	stringInput := StrStdin()

	// 空白で分割
	// stringSplited := strings.Split(stringInput, delim)
	stringReturned = SplitWithoutEmpty(stringInput, delim)

	return
}

// デリミタで分割して整数値スライスを取得
func SplitIntStdin(delim string) (intSlices []int, err error) {
	// 文字列スライスを取得
	stringSplited := SplitStrStdin(" ")

	// 整数スライスに保存
	for i := range stringSplited {
		var iparam int
		iparam, err = strconv.Atoi(stringSplited[i])
		if err != nil {
			return
		}
		intSlices = append(intSlices, iparam)
	}
	return
}

func main() {
	nk, _ := SplitIntStdin(" ")
	n, k := nk[0], nk[1]
	kind := make([]int, n)
	for i := 0; i < k; i++ {
		var tmp []int
		var err error
		if tmp, err = SplitIntStdin(" "); err != nil {
			panic(err)
		}
		kind[tmp[0]-1] = tmp[1]
	}
	dp := make([][3][3]int, n)
	if kind[0] > 0 {
		dp[0][0][kind[0]-1] = 1
	} else {
		for i := 0; i < 3; i++ {
			dp[0][0][i] = 1
		}
	}
	if kind[1] > 0 {
		for i := 0; i < 3; i++ {
			dp[1][i][kind[1]-1] = dp[0][0][i]
		}
	} else {
		for i := 0; i < 3; i++ {
			for j := 0; j < 3; j++ {
				dp[1][i][j] = dp[0][0][i]
			}
		}
	}
	for i := 1; i < n-1; i++ {
		if kind[i+1] > 0 {
			for j := 0; j < 3; j++ {
				if j == kind[i+1]-1 {
					for k := 0; k < 3; k++ {
						if j != k {
							dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
							dp[i+1][j][kind[i+1]-1] %= 10000
						}
					}
				} else {
					for k := 0; k < 3; k++ {
						dp[i+1][j][kind[i+1]-1] += dp[i][k][j]
						dp[i+1][j][kind[i+1]-1] %= 10000
					}
				}
			}
		} else {
			for j := 0; j < 3; j++ {
				for k := 0; k < 3; k++ {
					for l := 0; l < 3; l++ {
						if j != k || k != l {
							dp[i+1][k][j] += dp[i][l][k]
							dp[i+1][k][j] %= 10000
						}
					}
				}
			}
		}
	}
	result := 0
	for i := 0; i < 3; i++ {
		for j := 0; j < 3; j++ {
			result += dp[n-1][i][j]
		}
	}
	// fmt.Println(dp)
	// fmt.Println(kind)
	fmt.Println(result % 10000)
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end())
#define VRSORT(v) sort(v.rbegin(), v.rend())
#define ll int64_t
#define pb(a) push_back(a)
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

typedef vector<unsigned int>vec;
typedef vector<vec> mat;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

ll dp[101][4][4];
int d[101];

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,K;
    cin>>N>>K;
    REP(i,K){
        int a,b;
        cin>>a>>b;
        d[a-1] = b;
    }
    if(d[0]!=0) dp[0][0][d[0]]=1;
    else{
        dp[0][0][1]=1;
        dp[0][0][2]=1;
        dp[0][0][3]=1;
    }
    for(int i=1;i<N;i++){
        for(int j=0;j<4;j++){
            for(int l=1;l<4;l++){
                if(d[i]!=0){
                    if(!(j==l && l==d[i])) dp[i][l][d[i]] += dp[i-1][j][l] % 10000;
                }
                else{
                    for(int m=1;m<4;m++){
                        if(j==l && l==m) continue;
                        dp[i][l][m] += dp[i-1][j][l] % 10000;
                    }
                }
            }
        }
    }
    int ans = 0;
    for(int j=0;j<4;j++){
        for(int l=0;l<4;l++) ans += dp[N-1][j][l];
    }
    cout<<ans%10000<<endl;
}

C++: #include<iostream>
#include<vector>

int n,k,v[101],dp[103][3][3];

int calc(int i,int back1,int back2){
	if(i==n)return 1;
	if(dp[i][back1][back2]!=0)return dp[i][back1][back2];
	if(v[i]!=0){
		if(back1==v[i]&&v[i]==back2)return 0;
		else return calc(i+1,v[i],back1);
	}else{
		int ans=0;
		for(int k=1;k<=3;k++){
			if(k!=back1||k!=back2)ans+=calc(i+1,k,back1);
			ans%=10000;
		}
		return dp[i][back1][back2]=ans;
	}
}

int main(){
	std::cin>>n>>k;
	for(int i=0,a,b;i<k;i++){
		std::cin>>a>>b;
		v[a-1]=b;
	}
	std::cout<<calc(0,0,0)<<std::endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

ll dp[101][4][4];

int main() {
  int n, m;
  cin >> n >> m;
  int y[100] = {};
  rep(i, 0, m) {
    int a, b;
    cin >> a >> b;
    y[a - 1] = b;
  }

  rep(i, 1, 4) rep(j, 1, 4) {
    int a = y[0] ? y[0] : i, b = y[1] ? y[1] : j;
    dp[1][b][a] = 1;
  }

  rep(i, 2, n) rep(j, 1, 4) rep(k, 1, 4){
    if(y[i]) {
      if(!(y[i] == j && j == k))dp[i][y[i]][j] += dp[i - 1][j][k];
      continue;
    }
    rep(l, 1, 4) if(!(j == k && k == l)) dp[i][j][k] += dp[i - 1][k][l];
    dp[i + 1][j][k] %= 10000;
  }

  int ans = 0;
  rep(i, 1, 4) rep(j, 1, 4) ans += dp[n - 1][i][j];
  cout << ans % 10000 << endl;
}