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;
#include <cstring>

int rec(int today,int pasta1,int pasta2);

int n;
int sch[100];//?????±?????\??????????????¢
int memo[101][4][4];

int main(void){

	memset(sch, 0, sizeof(sch));
	memset(memo, -1, sizeof(memo));

	int k, a, b;

	cin >> n >> k;

	for (int i = 0; i < k; i++){
		cin >> a >> b;
		sch[a] = b;
	}

	cout << rec(1,0,0) << endl;
}

int rec(int today,int pasta1,int pasta2){

	if (today > n)return 1;
	if (memo[today][pasta1][pasta2] != -1)return memo[today][pasta1][pasta2];
	if (sch[today] != 0){
		if (pasta1 == pasta2 && pasta1 == sch[today])return 0;
		else return rec(today+1, sch[today], pasta1) % 10000;
	}

	int cnt = 0;
	for (int i = 1; i <= 3; i++){
		if (pasta1 == pasta2 && pasta1 == i)continue;
		cnt += rec(today + 1, i, pasta1);
	}
	return memo[today + 1][pasta1][pasta2] = cnt % 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[1]-1:i)][(in[0]?in[0]-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[1][i][j];cout<<endl;}
}

C++: #include <iostream>
#include <algorithm>
#include <list>
#include <set>
#include <string>
#include <cstring>
using namespace std;
#define MOD 10000

pair<long long,bool> pastas[105];
long long ways[1005][3][2]={0};
int main(){
    long long n,k;
    
    cin>>n>>k;
    for(int i=0; i<=n; i++){
        pastas[i]=make_pair(0, false);
    }
    for(int i=1; i<=k; i++){
        int a,b;
        cin>>a>>b;
        pastas[a]=make_pair(b, true);
    }
    
    if(pastas[1].second){
        ways[1][pastas[1].first][0]=1;
    }else{
        ways[1][1][0]=1;
        ways[1][2][0]=1;
        ways[1][3][0]=1;
    }
    for(int i=2; i<=n; i++){
        if(pastas[i].second){
            if(pastas[i].first==1) ways[i][pastas[i].first][0]+=(ways[i-1][2][0]+ways[i-1][2][1]+ways[i-1][3][0]+ways[i-1][3][1]);
            if(pastas[i].first==2) ways[i][pastas[i].first][0]+=(ways[i-1][1][0]+ways[i-1][1][1]+ways[i-1][3][0]+ways[i-1][3][1]);
            if(pastas[i].first==3) ways[i][pastas[i].first][0]+=(ways[i-1][1][0]+ways[i-1][1][1]+ways[i-1][2][0]+ways[i-1][2][1]);
            ways[i][pastas[i].first][1]+=ways[i-1][pastas[i].first][0];
        }else{
            ways[i][1][0]+=(ways[i-1][2][0]+ways[i-1][2][1]+ways[i-1][3][0]+ways[i-1][3][1]);
            ways[i][2][0]+=(ways[i-1][1][0]+ways[i-1][1][1]+ways[i-1][3][0]+ways[i-1][3][1]);
            ways[i][3][0]+=(ways[i-1][1][0]+ways[i-1][1][1]+ways[i-1][2][0]+ways[i-1][2][1]);
            ways[i][1][1]+=ways[i-1][1][0];
            ways[i][2][1]+=ways[i-1][2][0];
            ways[i][3][1]+=ways[i-1][3][0];
        }
        ways[i][1][0]=ways[i][1][0]%MOD;
        ways[i][1][1]=ways[i][1][1]%MOD;
        ways[i][2][0]=ways[i][2][0]%MOD;
        ways[i][2][1]=ways[i][2][1]%MOD;
        ways[i][3][0]=ways[i][3][0]%MOD;
        ways[i][3][1]=ways[i][3][1]%MOD;
    }
    long long ans=0;
    /*for(int i=1; i<=3; i++){
        for(int j=0; j<=1; j++){
            ans+=ways[n][i][j]%MOD;
        }
    }*/
    ans+=ways[n][1][0];
    ans=ans%MOD;
    ans+=ways[n][1][1];
    ans=ans%MOD;
    ans+=ways[n][2][0];
    ans=ans%MOD;
    ans+=ways[n][2][1];
    ans=ans%MOD;
    ans+=ways[n][3][0];
    ans=ans%MOD;
    ans+=ways[n][3][1];
    ans=ans%MOD;
    
    cout<<ans%10000<<endl;
}