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 main()
{
 int n,k,a,b,p[100];
 cin>>n>>k;
 for(int i=0;i<n;i++){p[i]=0;}
 
 for(int i=0;i<k;i++){
  cin>>a>>b;
  p[a-1]=b;
 }
 
 int total=1,sum1=1,sum2=1,sum3=1;
 
 //round 1
 if(!p[0]){total*=3;sum2*=3;sum3*=3;}
 
 //round 2
 if(!p[1]){total*=3;sum3*=3;}
 
 //round from 3 to n-2
 for(int i=0;i<n-2;i++){
  if(p[i+2]==0){total*=3;}
  int c[4];
  for(int j=0;j<4;j++){c[j]=0;}
  for(int j=0;j<3;j++){c[p[i+j]]++;}
  if(c[0]==3){total-=sum1*3;}
  else if(c[0]==2){total-=sum1;}
  else if(c[0]==1&&(c[1]==2||c[2]==2||c[3]==2))
   {total-=sum1;}
   
  sum1=sum2;sum2=sum3;
  if(p[i+2]==0){sum3*=3;}
  
  if(total>100000){
   total%=100000;
   sum1%=10000;
   sum2%=10000;
   sum3%=10000;
  }
 }
 
 cout<<total%10000<<endl;
 return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>

#define rep(X, Y) for( int (X) = 0; (X) < (Y); ++(X) )
#define rept(X, Y, Z) for( int (X) = (Y); (X) < (Z); ++(X) )
#define MOD 10000

int N, K, tmp[2];
int memo[101][4][4];
int def[101];

int func(int d, int op, int tp){
	int sum = 0;

	if( d == N ) return 1;

	rept( i, 1, 4 ){
		if( def[d] != 0 && def[d] != i || op == tp && i == op ) continue;

		sum += func( d + 1, i, op ) % MOD;
	}

	return sum % MOD;
}

int main(){
	scanf( "%d%d", &N, &K );

	rep( i, K ){
		scanf("%d%d", tmp, tmp + 1);

		def[tmp[0] - 1] = tmp[1];
	}

	std::cout << func(0, 0, 0) % MOD << std::endl;

	return 0;
}