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 <iostream>

using namespace std;

int type[100];
int cnt[100][3][3];

int main() {

  int N, K;
  cin >> N >> K;

  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    type[a-1] = b;
  }

  if (type[0] == 0 && type[1] == 0) for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) cnt[1][i][j] = 1;
  if (type[0] != 0 && type[1] == 0) for (int i = 0; i < 3; i++) cnt[1][type[0]-1][i] = 1;
  if (type[0] == 0 && type[1] != 0) for (int i = 0; i < 3; i++) cnt[1][i][type[1]-1] = 1;
  if (type[0] != 0 && type[1] != 0) cnt[1][type[0]-1][type[1]-1] = 1;

  for (int index = 1; index < N; index++) {
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        if (type[index] > 0 &&  type[index]-1 != j) {
          cnt[index][i][j] = 0;
          continue;
        }
        if (i != j) {
          cnt[index][i][j] += cnt[index-1][0][i] + cnt[index-1][1][i] + 
            cnt[index-1][2][i];
        } else {
          cnt[index][i][j] += cnt[index-1][(i+1)%3][j] + cnt[index-1][(i+2)%3][j];
        }
        cnt[index][i][j] = cnt[index][i][j] % 10000;
      }
    }
  }

  int ans = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      ans += cnt[N-1][i][j];
    }
  }
  cout << ans % 10000 << endl;

  return 0;

}

C++: #include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF+7;
const LL LLINF = 1e18;

LL dp[101][3][3][3];
//dp[?????\???][?????\][?????\][????????\];
const LL mo = 10000;

int main(void) {
	int N, K;
	cin >> N >> K;
	VI menu(N, -1);
	REP(i, K) {
		int a, b;
		cin >> a >> b;
		menu[a - 1] = b - 1;
	}
	REP(i, 101)REP(j, 3)REP(k, 3)REP(l, 3) dp[i][j][k][l] = 0;
	//0??\???
	if (menu[0] == -1) {
		REP(i, 3) {
			dp[0][i][0][0] = 1;
		}
	} else {
		dp[0][menu[0]][0][0] = 1;
	}
	//1??\???
	if (menu[1] == -1) {
		REP(i, 3) REP(j, 3) {
			dp[1][i][j][0] = dp[0][j][0][0];
		}
	} else {
		REP(i, 3) dp[1][menu[1]][i][0] = dp[0][i][0][0];
	}
	//2??\???
	if (menu[2] == -1) {
		REP(i, 3)REP(j, 3)REP(k, 3) {
			if (i == j && j == k) continue;
			dp[2][i][j][k] = dp[1][j][k][0];
		}
	} else {
		REP(i, 3)REP(j, 3) {
			if (menu[2] == i && i == j) continue;
			dp[2][menu[2]][i][j] = dp[1][i][j][0];
		}
	}
	FOR(day, 3, N) {
		if (menu[day] != -1) {
			REP(i, 3)REP(j, 3) {
				if (menu[day] == i && i == j) dp[day][menu[day]][i][j] = 0;
				else {
					LL num = 0;
					REP(k, 3) num = (num + dp[day - 1][i][j][k]) % mo;
					dp[day][menu[day]][i][j] = num;
				}
			}
		} else {
			REP(i, 3)REP(j, 3)REP(k, 3) {
				if (i == j && j == k) dp[day][i][j][k] = 0;
				else {
					LL num = 0;
					REP(l, 3) num = (num + dp[day - 1][j][k][l]) % mo;
					dp[day][i][j][k] = num;
				}
			}
		}
	}
	LL ans = 0;
	REP(i, 3)REP(j, 3)REP(k, 3) {
		ans = (ans + dp[N - 1][i][j][k]) % mo;
	}
	cout << ans << endl;
}