#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define int long long 
const int inf = 1e12;
#define D long double
#define NeedForSpeed ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int MOD = 1000000007;
#define fi first
#define se second
#define endl '\n'
// random_device rd;
// mt19937 rng(rd());
// int getRand(int l,int r){ // get random number in the range [l,r]
//   uniform_int_distribution<int> uid(l,r);
//   return uid(rng);
// }
const int N = 2e5 + 10;
int n , a[N] ;
vector<pair<int,int>> graph[N];
int subtree[N]={0};
int bits[100][2] = {0};
int power[100];

void dfs(int u , int p = 0){
    for(auto v : graph[u]){
        if(v.fi==p)continue;
        subtree[v.fi] = subtree[u]^v.se;
        dfs(v.fi,u);
    }
}

void go(){
   cin >> n ;    
   for(int i = 0 ; i < n-1 ; i++){
       int u , v , w ; cin >> u >> v >> w ; 
       graph[u].push_back({v,w});
       graph[v].push_back({u,w});
   }
   subtree[1] = 0;
   subtree[0] = 0;
   dfs(1);
   for(int i = 1 ; i <= n ; i++){
       for(int j = 0 ; j < 60 ; j++){
           bits[j][(subtree[i]>>j)&1]++;
       }
   }
   int ans = 0;
   for(int i = 0 ; i < 60 ; i++){
       int l = bits[i][0] , r  = bits[i][1];
       int cur = l*r;
       cur %= MOD ; cur += MOD ; cur %= MOD;
       cur *= power[i];
       cur %= MOD ; cur += MOD ; cur %= MOD;
       ans += cur;
       ans %= MOD; ans += MOD; ans %= MOD;
   }
   cout << ans << endl;
} 

signed main() {
        // freopen("input.txt", "r", stdin);
        // freopen("output.txt", "w", stdout);
        NeedForSpeed
        int t = 1;
        cout << fixed << setprecision(12);
        power[0] = 1;
        for(int i = 1 ; i < 62 ; i++)power[i] = power[i-1]*2;
        for(int i = 0 ; i < 62 ; i++){
            power[i] %= MOD ; 
            power[i] += MOD ; 
            power[i] %= MOD ;
        }
        // cin >> t;
        while(t--){
            // cout << "Case #" << cases << ": ";
            go();
        }

}
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &x){
	x = 0; int f = 1; char ch = getchar();
	while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
	while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
	x *= f;
}
inline void write(int x){if (x > 9) write(x/10); putchar(x%10+'0'); }
int n,ans[11];
const int P = 1e9+  7;
char caa,cab,cba,cbb;
unordered_map<string,int>mp;
inline void dfs(string s){
	if (mp.count(s)) return;
	++ans[s.size()]; 
	mp[s] = 1; if (s.size() ==10){ return; }
	for (int i = 0; i < s.size()-1; ++i){
		bool s1 = s[i] == 'a' ? 1 : 0,s2 = s[i+1] == 'a' ? 1 : 0;
		if (s1&&s2) dfs(s.substr(0,i+1) + caa + s.substr(i+1,s.size()-i-1));
		else if (s1&&!s2) dfs(s.substr(0,i+1) + cab + s.substr(i+1,s.size()-i-1));
		else if (!s1&&!s2) dfs(s.substr(0,i+1) + cbb + s.substr(i+1,s.size()-i-1));
		else if (!s1&&s2) dfs(s.substr(0,i+1) + cba + s.substr(i+1,s.size()-i-1));
	}
}
inline int all1(){
	for (int i = 2; i <= 10; ++i) if (ans[i] ^ 1) return 0;
	return 1;
}
inline int pw2(){
	for (int i = 2; i <= 3; ++i) if (ans[i] ^ 1) return 0;
	for (int i = 4,now = 2; i <= 10; ++i,now *= 2) if (ans[i] ^ now) return 0;
	return 1;
}
int fib[1005];
int main(){
//	caa = 'b';
//	cab = 'b';
//	cba = 'a';
//	cbb = 'b';
	fib[2] = 1,fib[3] = 1;
	for (int i = 4; i <= 1000; ++i) fib[i] = (fib[i-1] + fib[i-2]) %P;
	cin>>n>>caa>>cab>>cba>>cbb;
	if (n<=3)cout<<1<<'\n',exit(0);
	caa -= 'A'-'a';
	cab -= 'A'-'a';
	cba -= 'A'-'a';
	cbb -= 'A'-'a';

	dfs("ab");
//	for (int i = 2; i <= 10; ++i) cout<<i<<' '<<ans[i]<<'\n';
	if (all1()) cout<<1<<'\n',exit(0);
	if (pw2()){
		int ans=  1;
		for (int i = 1; i <= n-3; ++i) ans=ans*2%P;
		cout<<ans<<'\n';exit(0);
	}
	cout<<fib[n]<<'\n';
	return 0;
}