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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define MOD 10000
int n,m,dp[150][4][4],plan[150];
int rec(int n,int a,int b)
{
	if(n==0)return 1;
	if(dp[n][a][b]>=0)return dp[n][a][b];
	int res=0;
	if(plan[n]!=0)
	{
		if(a==b&&b==plan[n])return 0;
		else return rec(n-1,b,plan[n]);
	}
	if(a==b&&a!=0)
	{
		for(int i=1;i<=3;i++)
		{
			if(a!=i)
			{
				res+=rec(n-1,b,i);
				res%=MOD;
			}
		}
		return dp[n][a][b]=res;
	}
	else
	{
		for(int i=1;i<=3;i++)
		{
			res+=rec(n-1,b,i);
			res%=MOD;
		}
		return dp[n][a][b]=res;
	}
}
int main()
{
	cin >> n >> m;
	for(int i=0;i<m;i++)
	{
		int d,k;
		cin >> d >> k;
		plan[d]=k;
	}
	memset(dp,-1,sizeof(dp));
	cout << rec(n,0,0) << endl;
	return 0;
}

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

int dp[102][4][4];

int main()
{
  int n,k,a,b[101]={},ans=0;
  cin>>n>>k;
  for(int i=1;i<=k;i++){
    cin>>a;
    cin>>b[a];
  }
  if(b[1]==0){
    for(int j=1;j<=3;j++){
      dp[2][j][0]=1;
    }
  }else {
    dp[2][b[1]][0]=1;
  }
  for(int i=3;i<=n+1;i++){                 //?????\?????\??°
    for(int j=0;j<=3;j++){              //?????\???????????????????¨????
      for(int k=0;k<=3;k++){            //?????\???????????????????¨????
        if(dp[i-1][j][k]!=0){           //??¨??\????????????????£??????????
          if(b[i-1]!=0){                  //?????\????????????????±??????£????????????
            if(!(j==k && b[i]==j)){     //?????\??£?¶????????????????
              dp[i][b[i-1]][j]+=dp[i-1][j][k];
              dp[i][b[i-1]][j]%=10000;
            }
          }else{                        //?????\????????????????±??????£???????????´???
            for(int l=1;l<=3;l++){
              if(!(j==k && l==j)){      //?????\??£?¶????????????????
                dp[i][l][j]+=dp[i-1][j][k];
                dp[i][l][j]%=10000;
              }
            }
          }
        }
      }
    }
  }
  for(int i=1;i<=3;i++){
    for(int j=1;j<=3;j++){
      ans+=dp[n+1][i][j];
      ans%=10000;
    }
  }
  ans%=10000;
  cout<<ans<<endl;
  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<iostream>
#include<functional>
using namespace std;
long long int dp[101][4][4];
int main(void)
{
	int i,j,k,n,m,a,b[101],c;
	long long int sum,x;
	cin>>n>>m;
	for(i=1;i<=n;i++){
		for(j=1;j<=3;j++){
			for(k=0;k<=3;k++) dp[i][j][k]=0;
		}
	}
	for(i=1;i<=n;i++) b[i]=0;
	for(i=1;i<=m;i++){
		cin>>a;
		cin>>b[a];
	}
	if(b[1]>0) dp[1][b[1]][0]=1;
	else{
		for(i=1;i<=3;i++) dp[1][i][0]=1;
	}
	for(i=1;i<n;i++){
		for(j=1;j<=3;j++){
			if(dp[i][j][0]+dp[i][j][1]+dp[i][j][2]+dp[i][j][3]>0){
				for(k=0;k<=3;k++){
					if(b[i+1]>0){
						if(b[i+1]!=k||b[i+1]!=j){
							dp[i+1][b[i+1]][j]+=dp[i][j][k];
							dp[i+1][b[i+1]][j]%=10000;
						}
					}
					else{
						if(k!=1||j!=1){
							dp[i+1][1][j]+=dp[i][j][k];
							dp[i+1][1][j]%=10000;
						}
						if(k!=2||j!=2){
							dp[i+1][2][j]+=dp[i][j][k];
							dp[i+1][2][j]%=10000;
						}
						if(k!=3||j!=3){
							dp[i+1][3][j]+=dp[i][j][k];
							dp[i+1][3][j]%=10000;
						}
					}
				}
			}
		}
	}
	sum=0;
	for(i=1;i<=3;i++){
		for(j=0;j<=3;j++) sum+=dp[n][i][j];
	}
	x=sum%10000;
	cout<<x<<endl;	
	return 0;
}

C++: import Control.Monad
import Control.Applicative
import Data.List

main = do
	[n, k] <- map (read :: String -> Int) . words <$> getLine
	l <- map (map (read :: String -> Int) . words) <$> replicateM k getLine
	print $ solve n l

func :: Int -> [[Int]] -> [(Int, Int)]
func n [] = [(x, -1) | x <- [1..n]]
func n ([a, b]:c) = map (\(x, y) -> if x == a then (x, b) else (x, y)) $ func n c

foldw :: [Int] -> Int
foldw [] = 0
foldw a = foldl1 (+) a

solvew :: Int -> Int -> [Int] -> Int
solvew _ _ [] = 1
solvew a b (c:d)
	| a == b = (foldw [solvew x a d | x <- [1..3], x /= a, x == c || c == -1]) `mod` 10000
	| otherwise = (foldw [solvew x a d | x <- [1..3], x == c || c == -1]) `mod` 10000

solve :: Int -> [[Int]] -> Int
solve n l = solvew (-2) (-2) $ map (\(x, y) -> y) $ func n l