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 <functional>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

namespace _{struct _{_(){ // initialize
  cin.tie(0); ios::sync_with_stdio(false);
}}_;}
template <typename T, size_t size>
inline size_t len(const T (&)[size]) { return size; }
#define println(s) cout << s << '\n';
#define str to_string
#define rep(i, n) for (int i=0; i<int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 110

int N, K;
int table[MAX_N];

bool invalid(int x) {
  return x != 0 && x != 1 && x != 2;
}

int isConst(int offset) {
  return table[offset] != -1;
}
// x??\??????2???
pair<int, int> els(int x) {
  if (x == 0) return pair<int, int>(1, 2);
  if (x == 1) return pair<int, int>(0, 2);
  if (x == 2) return pair<int, int>(0, 1);
  return pair<int, int>(-1, -1);
}
// (offset) A B ? ...
int solve(int offset, int A, int B) {
  // cout << "solve(" << offset <<", " << A <<", " << B <<")" << '\n';
  // x | x x ...
  if (offset >= 1) {
    int prev = table[offset-1];
    // fail
    if (prev == A && A == B) return 0;
  }
  // | x y ? [wall]
  if (offset + 2 >= N) return 1; // goal
  if (offset >= 0 && (invalid(A) || invalid(B))) {
    cout << A << " " << B << endl;
    cout << "?!" << endl;
    exit(-1);
  }

  // A B C ...
  // shift
  if (isConst(offset+2)) return solve(offset+1, B, table[offset+2]) % 10000;

  // A B ? ...
  else {
    // x x [^x] ...
    if (A == B) {
      pair<int, int> p = els(A);
      return (solve(offset+1, B, p.first) + solve(offset+1, B, p.second)) % 10000;
    }
    // x y (any) ...
    else {
      return (solve(offset+1, B, 0) + solve(offset+1, B, 1) + solve(offset+1, B, 2)) % 10000;
    }
  }
}



int main() {
  cin >> N >> K;
  // initialize (-1 means ????¢????)
  rep(i, MAX_N) table[i] = -1;
  // input
  rep(i, K) {
    int a, b;
    cin >> a >> b;
    table[a] = b-1;
  }
  // rep(i, N) cout << "#" << i << ": " << table[i] << '\n'; // DEBUG
  // main
  cout << (solve(-2, -11, -12) % 10000) << '\n';
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10000;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { 1, -1, 0,  0 };
static const int dy[] = { 0,  0, 1, -1 };

int dp[ 101 ][ 4 ][ 3 ];
int n, m, a, ans;
int ar[ 101 ] = {};
int solve( int now, int b, int d ) {
	if ( d == 3 ) {
		return 0;
	}
	if ( now == n ) {
		return 1;
	}
	if ( dp[ now ][ b ][ d ] >= 0 ) {
		return dp[ now ][ b ][ d ];
	}
	int res = 0;
	if ( ar[ now ] != 0 ) {
		if ( b == ar[ now ] ) {
			res = solve( now + 1, ar[ now ], d + 1 );
			res %= mod;
		}
		else {
			res = solve( now + 1, ar[ now ], 1 );
			res %= mod;
		}
	}
	else {
		for ( int i = 1; i <= 3; i++ ) {
			if ( b == i ) {
				res += solve( now + 1, i, d + 1 );
				res %= mod;
			}
			else {
				res += solve( now + 1, i, 1 );
				res %= mod;
			}
		}
	}
	return dp[ now ][ b ][ d ] = res;
}

int main() {
	scanf( "%d %d", &n, &m );
	memset( dp, -1, sizeof( dp ) );
	for ( int i = 0; i < m; i++ ) {
		scanf( "%d", &a );
		scanf( "%d", &ar[ a - 1 ] );
	}
	ans = solve( 0, 0, 0 );
	printf( "%d\n", ans );
	return 0;
}