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>
#include <iomanip>
#include <string>
#include <vector>
#include <array>
#include <queue>
#include <algorithm>
#include <utility>
#include <cmath>
#include <map>
#include <set>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	int N, K;
	map<int, int> d;
	int dp[101][3][3] = {0};
	cin >> N >> K;
	REP(i, K){
		int a, b;
		cin >> a >> b;
		d[a] = b;
	}
	REP(i, 3){
		REP(j, 3){
			dp[2][i][j] = 1;
		}
	}
	if(d.find(0) != d.end()){
		REP(i, 3){
			if(i != d[0]-1){
				REP(j, 3){
					dp[2][j][i] = 0;
				}
			}
		}
	}
	if(d.find(1) != d.end()){
		REP(i, 3){
			if(i != d[1]-1){
				REP(j, 3){
					dp[2][i][j] = 0;
				}
			}
		}
	}
	if(d.find(2) != d.end()){
		dp[2][d[2]-1][d[2]-1] = 0;
	}

	for(int i = 2;i < N;i++){
		REP(j, 3){ // 2??????
			REP(k, 3){ // 1?????? 
				if(d.find(i) != d.end()){
					if(!(j == k && k == d[i]-1)){
						dp[i+1][d[i]-1][k] += dp[i][k][j];
						dp[i+1][d[i]-1][k] %= 10000;
					}
				}else{
					REP(s, 3){
						if(!(j == k && k == s)){
							dp[i+1][s][k] += dp[i][k][j] ;
							dp[i+1][s][k] %= 10000;
						}
					}
				}
			}
		}
	}
	int res = 0;
	REP(i, 3){
		REP(j, 3){
			res = (res + dp[N][i][j]) % 10000;
		}
	}
	cout << res << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int a[101] = { 0 };
	int n, m = 1;
	int i, f = 1;
	int j = 0;
	cin >> n >> i;
	for (int v = 0; v < i; v++){
		int y, x;
		cin >> y >> x;
		a[y] = { x };

	}
	for (int v = 1; v <= n; v++){
		if (a[v] = { 0 }){
			if (a[v - 1] = a[v + 1] = 0){
				f = (f * 3) - 1;
			}
			if (a[v - 1] = 0){
				if (a[v + 1] != 0){
					f = f * 2;
				}
			}
			if (a[v + 1] = 0){
				if (a[v - 1] != 0){
					f = f * 2;
				}
			}
			if (a[v + 1] = a[v - 1]){
				if (a[v + 1] != 0){
					f = f * 2;
				}
			}
			if (a[v - 1] = a[v - 2] != 0){
				if (a[v+1] != 0){
					if (a[v + 1] != a[v - 1]){
						if (a[v + 1] != a[v + 2]){
							f = f * 2;
						}
					}
				}
					  }
			if (a[v + 1] = a[v + 2] != 0){
				if (a[v - 1] != 0){
					if (a[v - 1] != a[v+1]!=a[v-2]){
						f = f * 2;
					}
				}
			}

					}
				}
	cout << f % 10000 << endl;
			}

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair< int, int > P;
typedef pair< int, P > PP;

//------------------------------------------------

const int MOD = 10000;
int n, k;
int taste[105];

int DP(int snd, int fst) {
	int dp[n][4][4] = {};
	dp[1][snd][fst] = 1;
	FOR(i, 2, n) {
		FOR(j, 1, 4) {
			FOR(k, 1, 4) {
				if (taste[i]) {
					if (j == k && k == taste[i]) continue;
					(dp[i][taste[i]][k] += dp[i - 1][k][j]) %= MOD;
				}
				else {
					FOR(l, 1, 4) {
						if (j == k && k == l) continue;
						(dp[i][l][k] += dp[i - 1][k][j]) %= MOD;
					}
				}
			}
		}
	}
	int res = 0;
	FOR(i, 1, 4) FOR(j, 1, 4) (res += dp[n - 1][i][j]) %= MOD;
	//cout << "(fst, snd, res) = " << fst << "," << snd << "," << res << endl;
	return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

	cin >> n >> k;
    REP(i, k) {
		int a, b;
		cin >> a >> b;
		taste[a - 1] = b;
	}
	int ans = 0;
	if (taste[0]) {
		if (taste[1]) {
			ans = DP(taste[1], taste[0]);
		}
		else {
			REP(i, 3) {
				ans += DP(i + 1, taste[0]);
			}
		}
	}
	else {
		REP(i, 3) {
			if (taste[1]) {
				ans += DP(taste[1], i + 1);
			}
			else {
				REP(j, 3) {
					ans += DP(j + 1, i + 1);
				}
			}
		}
	}
	cout << ans % MOD << endl;

	return 0;
}