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 <bits/stdc++.h>

#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)

using namespace std;

using I = int;
using L = long;
using F = float;
using D = double;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using VVL = vector<vector<long>>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;

const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;

template <class T>
void PrintVector(vector<T> & v, unsigned n) {
  rep(i, 0, n - 1) {
    cout << v[i] << " ";
  }
  cout << endl;
}

const L kMod = 10000;

int main(int argc, char const *argv[]) {
  L n, k;
  VVL f(kMax3, VL(9, 0));
  VL menu(kMax3, 0);
  cin >> n >> k;
  rep(i, 0, k - 1) {
    L a, b;
    cin >> a >> b;
    menu[a] = b;;
  }
  menu.resize(n + 1);
  f.resize(n + 1);
  if (menu[1] == 0 || menu[1] == 1) {
    f[1][1] = 1;
  }
  if (menu[1] == 0 || menu[1] == 2) {
    f[1][5] = 1;
  }
  if (menu[1] == 0 || menu[1] == 3) {
    f[1][6] = 1;
  }
  rep(i, 2, n) {
    rep(j, 0, 8) {
      if (menu[i] == 0 || menu[i] == 1) {
        switch (j) {
          case 0:
            f[i][j] = (f[i - 1][1] + f[i - 1][2]) % kMod;
            break;
          case 1:
            f[i][j] = (f[i - 1][3] + f[i - 1][4] + f[i - 1][5]) % kMod;
            break;
          case 2:
            f[i][j] = (f[i - 1][6] + f[i - 1][7] + f[i - 1][8]) % kMod;
            break;
          default:
            break;
        }
      }
      if (menu[i] == 0 || menu[i] == 2) {
        switch (j) {
          case 3:
            f[i][j] = (f[i - 1][0] + f[i - 1][1] + f[i - 1][2]) % kMod;
            break;
          case 4:
            f[i][j] = (f[i - 1][3] + f[i - 1][5]) % kMod;
            break;
          case 5:
            f[i][j] = (f[i - 1][6] + f[i - 1][7] + f[i - 1][8]) % kMod;
            break;
          default:
            break;
        }
      }
      if (menu[i] == 0 || menu[i] == 3) {
        switch (j) {
          case 6:
            f[i][j] = (f[i - 1][0] + f[i - 1][1] + f[i - 1][2]) % kMod;
            break;
          case 7:
            f[i][j] = (f[i - 1][3] + f[i - 1][4] + f[i - 1][5]) % kMod;
            break;
          case 8:
            f[i][j] = (f[i - 1][6] + f[i - 1][7]) % kMod;
            break;
          default:
            break;
        }
      }
    }
  }

  cout << accumulate(f[n].begin(), f[n].end(), 0) % kMod << endl;
  return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
using namespace std;

#define pb         push_back
#define mp         make_pair
#define ll         long long
#define PI         acos(-1.0)
#define ALL(A)     ((A).begin(), (A).end())
#define vsort(v)   sort(v.begin(),v.end())
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)

int main(){
    int N, K;
    cin >> N >> K;
    int pasta[N+1];
    FOR(i, 0, N+1) pasta[i] = 0;
    FOR(i, 1, K+1){
        int hi, pas;
        cin >> hi >> pas;
        pasta[hi] = pas;
    }
    ll int dp[N+1][4][4];
    FOR(i, 0, N+1){
        FOR(j, 0, 4){
            FOR(k, 0, 4){
                dp[i][j][k] = 0;
            }
        }
    }
    FOR(i, 1, 4){
        if(pasta[1]==0){
            dp[1][i][0] = 1;
        }
        else{
            if(i==pasta[1]) dp[1][i][0] = 1;
        }
    }

    FOR(i, 1, N+1){
        FOR(j, 0, 4){
            FOR(k, 0, 4){
                if(dp[i][j][k]==0) continue;
                FOR(m, 1, 4){
                    if(j==k && m==k) continue;
                    if(pasta[i+1]==0){
                        dp[i+1][m][j] = dp[i+1][m][j] + dp[i][j][k];
                    }
                    else{
                        if(pasta[i+1]==m){
                            dp[i+1][m][j] = dp[i+1][m][j] + dp[i][j][k];
                        }
                        //else dp[i+1][m][j] = 0;
                    }
                }

            }
        }
    }
    ll int ans = 0;
    FOR(i, 1, 4){
        FOR(j, 1, 4){
            ans += dp[N][i][j];
            ans = ans % 10000;
        }
    }
    cout << ans << endl;
}