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 N 101
using namespace std;
  int n,k,a,b;
int dp[N][4][4],d[N];

int dfs(int x,int d1,int d2){
  if(x==n+1) return 1;
  if(d[x]>0){
    if(d1==d[x] && d2==d[x])return 0;
    dp[x][d1][d2]+=dfs(x+1,d[x],d1);
  }
  else{
    for(int i=1;i<=3;i++){
    if(d1==i && d2==i) continue;
    dp[x][d1][d2]+=dfs(x+1,i,d1);
    }
  }
    
}


int main(){
  cin>>n>>k;
  for(int i=0;i<k;i++) cin>>a>>b,d[a]=b;
  
  dfs(1,0,0);
  int ans=0;
  for(int i=1;i<4;i++)
    for(int j=1;j<4;j++)ans+=dp[n][i][j];
      cout<<ans%10000<<endl;
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int N, K, A, B;

int dfs(vector< vector< vector<int> > >& dp,vector<bool>& dec, vector<int>& pasta,int pppasta,int ppasta,int n)
{
	if (n == N + 1)
		return 1;
	if (dp[pppasta][ppasta][n] != -1) {
		return dp[pppasta][ppasta][n];
	}
	dp[pppasta][ppasta][n] = 0;
	if (dec[n]) {
		if (n - 2 >= 0 && pppasta == ppasta && ppasta == pasta[n])
			return 0;
		else
			return dp[pppasta][ppasta][n] = dfs(dp, dec, pasta, ppasta, pasta[n], n + 1);
	}
	for (int i = 1; i <= 3; i++) {
		pasta[n] = i;
		if (n - 2 >= 0 && pppasta == ppasta && ppasta == pasta[n])
			continue;
		dp[pppasta][ppasta][n] += dfs(dp, dec, pasta, ppasta, pasta[n], n + 1);
	}
	dp[pppasta][ppasta][n] %= 10000;
	return dp[pppasta][ppasta][n];
}

int main()
{
	cin >> N >> K;
	vector<bool> dec(N + 1, false);
	vector<int> pasta(N + 1, 0);
	vector< vector< vector<int> > > dp(4, vector< vector<int> >(4, vector<int>(N + 1, -1)));
	for (int i = 0; i < K; i++) {
		cin >> A >> B;
		dec[A] = true;
		pasta[A] = B;
	}
	cout << dfs(dp, dec, pasta, 0, 0, 1) << endl;
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;

#define TT template<typename T>
#define TTF template<typename T, typename F>
#define ET(T) typename T::value_type
TT inline T sq(T x){return x*x;}
TT inline T In(){T x;cin>>x;return x;}
TT inline void Out(T&x){cout<<x;}
TT inline void sort(T&v){sort(begin(v),end(v));}
TT inline void revs(T&v){reverse(begin(v),end(v));}
TT inline void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT inline int ubnd(T&v,ET(T)&x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT inline int lbnd(T&v,ET(T)&x){return lower_bound(begin(v),end(v),x)-begin(v);}
TTF inline void inpt(T&v,int n,F f){for(v.reserve(n);n--;v.emplace_back(f()));}
TTF inline void show(T&v,F f,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),f(x);cout<<e;}
#undef TT
#undef TTF
#undef ET

inline void fast_io(){ios::sync_with_stdio(0);cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline ll pow_mod(ll a,ll k,ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a,ll p){return pow_mod(a,p-2,p);}

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

int dp[128][4][4][4] = {};
int decided[128] = {};

int main()
{
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++){
    int d = in();
    decided[d] = in();
  }

  dp[0][0][0][0] = 1;
  for (int d = 1; d <= n; d++){
    for (int u = 1; u < 4; u++){
      if (decided[d] && decided[d] != u) continue;
      for (int i = 0; i < 4; i++){
        for (int j = 0; j < 4; j++){
          if (u == i && u == j) continue;
          for (int k = 0; k < 4; k++){
            dp[d][u][i][j] = (dp[d][u][i][j] + dp[d - 1][i][j][k]) % Mod;
          }
        }
      }
    }
  }
  int tot = 0;
  for (int i = 0; i < 4; i++){
    for (int j = 0; j < 4; j++){
      for (int k = 0; k < 4; k++){
        tot = (tot + dp[n][i][j][k]) % Mod;
      }
    }
  }
  cout << tot << endl;
  return (0);
}

// priority_queue queue deque front stringstream max_element min_element insert count make_tuple