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 <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const ll INF=1ll<<50;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

const int mod=10000;

int Plus(P p){
	return (p.first+p.second)%mod;
}

int n,k;
vi a;
vvp dp;

int main(){
	cin>>n>>k;
	a=vi(n);
	dp=vvp(n,vp(3));
	for(int i=0;i<k;i++){
		int A,B;
		cin>>A>>B;
		a[A-1]=B;
	}
	if(!a[0]){
		dp[0][0].first++;
		dp[0][1].first++;
		dp[0][2].first++;
	}
	else dp[0][a[0]-1].first++;
	for(int i=1;i<n;i++){
		vp p(3);
		for(int j=0;j<3;j++) p[j]=dp[i-1][j];
		if(!a[i]){
			dp[i][0].first=(Plus(p[1])+Plus(p[2]))%mod;
			dp[i][1].first=(Plus(p[2])+Plus(p[0]))%mod;
			dp[i][2].first=(Plus(p[0])+Plus(p[1]))%mod;
			dp[i][0].second=p[0].first;
			dp[i][1].second=p[1].first;
			dp[i][2].second=p[2].first;
		}
		else{
			int I=a[i]-1;
			dp[i][I].first=(Plus(p[0])+Plus(p[1])+Plus(p[2])-Plus(p[I]))%mod;
			dp[i][I].second=p[I].first;
		}
	}
	int ans=0;
	for(int i=0;i<3;i++) ans=(ans+Plus(dp[n-1][i]))%mod;
	cout<<ans<<endl;
}

C++: /*********************************************************************\
                                                                      |
    codeforces = nenuon  ||  Atcoder = nenuon                         |
    mail = keep.my.best.61@gmail.com                                  |
    Tokyo Metropolitan University                                     |
                                                                      |
**********************************************************************/

#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[100+1]={0};
    FOR(i, 1, K+1){
        int hi, pas;
        cin >> hi >> pas;
        pasta[hi] = pas;
    }
    int dp[100+1][4][4]={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;
                    }
                }

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

    cout << ans << endl;
}

C++: #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
int a[110] = {};
int dp[110][3][3] = {};
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, K; cin >> N >> K;
	memset(a, -1, sizeof(a));
	rep(i, 0, K) {
		int x, y; cin >> x >> y;
		a[x] = y - 1;
	}
	dp[0][0][0] = 1;
	rep(i, 0, N) {
		if (a[i] == -1) {
			rep(j, 0, 3) {
				rep(k, 0, 3) {
					rep(l, 0, 3) {
						if (j == 2 && k == l)continue;
						(dp[i + 1][k == l ? j + 1 : 1][k] += dp[i][j][l]) %= 10000;
					}
				}
			}
		}
		else {
			rep(j, 0, 3) {
				rep(l, 0, 3) {
					if (j == 2 && a[i] == l)continue;
					(dp[i + 1][a[i] == l ? j + 1 : 1][a[i]] += dp[i][j][l]) %= 10000;
				}
			}
		}
		dump(dp[i + 1]);
	}
	int ans = 0;
	rep(j, 0, 3)rep(k, 0, 3) {
		ans += dp[N][j][k];
	}
	ans %= 10000;
	cout << ans << endl;
	return 0;
}