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<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip	;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1<<30;
int main(){
	int n,m;
	int MOD=10000;
	cin>>n>>m;
	int dp[110][3][3]={0};
	vi in(n);
	while(m--){
		int a,b;
		cin>>a>>b;
		a--;
		in[a]=b;
	}
	rep(i,3)rep(j,3){
		dp[1][(in[1]?in[0]-1:i)][(in[0]?in[1]-1:j)]=1;
	}
	loop(i,2,n){
		if(in[i]){
			rep(j,3)rep(k,3)if(!(in[i]-1==j&&j==k))
				dp[i][in[i]-1][j]=(dp[i][in[i]-1][j]+dp[i-1][j][k])%MOD;
		}else{
			rep(q,3)rep(j,3)rep(k,3)if(!(q==j&&j==k))
				dp[i][q][j]=(dp[i][q][j]+dp[i-1][j][k])%MOD;
		}
	}
	int co=0;
	rep(i,3)rep(j,3)co=(co+dp[n-1][i][j])%MOD;
	cout<<co<<endl;
//	rep(i,3){rep(j,3)cout<<" "<<dp[2][i][j];cout<<endl;}
}

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

int d1[101], d2[101], d3[101], s1[101], s2[101], s3[101], sta[101];
int N, K;

void search(int n){
	
	if(n == N) return;
	
	if(n == 0){
		if(sta[0] == 1) s1[0] = 1;
		if(sta[0] == 2) s2[0] = 1;
		if(sta[0] == 3) s3[0] = 1;
		if(sta[0] == 0)s1[0] = s2[0] = s3[0] = 1;
		//cout << d1[0] << d2[0] << d3[0] << s1[0] << s2[0] << s3[0] << endl;
		search(1);
		return;
	}
	
	if(sta[n] == 1){
		d1[n] = s1[n - 1] % 10000;
		s1[n] = (d2[n - 1] + d3[n - 1] + s2[n - 1] + s3[n - 1]) % 10000;
		
	}
	if(sta[n] == 2){
		d2[n] = s2[n - 1] % 10000;
		s2[n] = (d1[n - 1] + d3[n - 1] + s1[n - 1] + s3[n - 1]) % 10000;
		
	}
	if(sta[n] == 3){
		d3[n] = s3[n - 1] % 10000;
		s3[n] = (d1[n - 1] + d2[n - 1] + s1[n - 1] + s2[n - 1]) % 10000;
		
	}
	if(sta[n] == 0){
		d1[n] = s1[n - 1] % 10000;
		d2[n] = s2[n - 1] % 10000;
		d3[n] = s3[n - 1] % 10000;
		s1[n] = (d2[n - 1] + d3[n - 1] + s2[n - 1]  + s3[n - 1]) % 10000;
		s2[n] = (d1[n - 1] + d3[n - 1] + s1[n - 1]  + s3[n - 1]) % 10000;
		s3[n] = (d1[n - 1] + d2[n - 1] + s1[n - 1]  + s2[n - 1]) % 10000;
	}
	
	if(sta[n + 1] != 0){
	
		if(sta[n + 1] == sta[n + 2]){
			if(sta[n + 1] == 1) s1[n] = 0; 
			if(sta[n + 1] == 2) s2[n] = 0;
			if(sta[n + 1] == 3) s3[n] = 0;
		}
	}
	//cout << d1[n] << d2[n] << d3[n] << s1[n] << s2[n] << s3[n] << endl;
	search(n + 1);
	return;
}

int main(){
	for(int i = 0;i < 101;i ++) sta[i] = 0;
	d1[0] = d2[0] = d3[0] = s1[0] = s2[0] = s3[0] = 0;
	
	cin >> N >> K;
	int Ai, Bi;
	for(int i = 0;i < K;i ++){
		cin >> Ai >> Bi;
		sta[Ai - 1] = Bi;
	}
	search(0);
	cout << (d1[N - 1] + d2[N - 1] + d3[N - 1] + s1[N - 1] + s2[N - 1] + s3[N - 1]) % 10000<< endl;
	
	return 0;
}

C++: #include <iostream>
using namespace std;
int dp[110][19];//[??\][????????\????????????????????????????\????????????]
int pst[110];
int main(){
	int N,K,tmp,sum;
	cin>>N>>K;
	for(int i=0;i<=N;i++){
		for(int j=0;j<9;j++){
			dp[i][j]=0;
		}
		pst[i]=0;
	}
	for(int i=0;i<K;i++){
		cin>>tmp;
		cin>>pst[tmp];
	}
	switch(pst[1]){
	case 0:
		dp[1][1]=1;
		dp[1][2]=1;
		dp[1][3]=1;
		break;
	case 1:
		dp[1][3]=1;
		break;
	case 2:
		dp[1][1]=1;
		break;
	case 3:
		dp[1][2]=1;
		break;
	}
	
	//dp [i][j]???????????\??????????????¢??¬%10000
	for(int i=1;i<N;i++){
		switch(pst[i+1]){
		case 0:
			dp[i+1][0]=(dp[i][3]+dp[i][6])%10000;
			dp[i+1][1]=(dp[i][0]+dp[i][3]+dp[i][6])%10000;
			dp[i+1][2]=(dp[i][0]+dp[i][3]+dp[i][6])%10000;
			dp[i+1][3]=(dp[i][1]+dp[i][4]+dp[i][7])%10000;
			dp[i+1][4]=(dp[i][1]+dp[i][7])%10000;
			dp[i+1][5]=(dp[i][1]+dp[i][4]+dp[i][7])%10000;
			dp[i+1][6]=(dp[i][2]+dp[i][5]+dp[i][8])%10000;
			dp[i+1][7]=(dp[i][2]+dp[i][5]+dp[i][8])%10000;
			dp[i+1][8]=(dp[i][2]+dp[i][5])%10000;
			break;
		case 1:
			dp[i+1][0]=(dp[i][3]+dp[i][6])%10000;
			dp[i+1][3]=(dp[i][1]+dp[i][4]+dp[i][7])%10000;
			dp[i+1][6]=(dp[i][2]+dp[i][5]+dp[i][8])%10000;
			break;
		case 2:
			dp[i+1][1]=(dp[i][0]+dp[i][3]+dp[i][6])%10000;
			dp[i+1][4]=(dp[i][1]+dp[i][7])%10000;
			dp[i+1][7]=(dp[i][2]+dp[i][5]+dp[i][8])%10000;
			break;
		case 3:
			dp[i+1][2]=(dp[i][0]+dp[i][3]+dp[i][6])%10000;
			dp[i+1][5]=(dp[i][1]+dp[i][4]+dp[i][7])%10000;
			dp[i+1][8]=(dp[i][2]+dp[i][5])%10000;
			break;
		}
	}
	sum=0;
	for(int i=0;i<9;i++){
		sum+=dp[N][i];
	}
	sum%=10000;
	cout<<sum<<endl;

	/*
	for(int i=0;i<=N;i++){
		for(int j=0;j<9;j++){
			cout<<"dp["<<i<<"]["<<j<<"]="<<dp[i][j]<<" ";
		}
		cout<<endl;
	}
	*/

	return 0;
}