n = int(input())
s = input()
d1 = 0
d2 = 0
d3 = 0
counter = 1
m = pow(10, 9) + 7
for i in range(n):
if s[i] == 'a':
d1 = (d1 + counter) % m
elif s[i] == 'b':
d2 = (d2 + d1) % m
elif s[i] == 'c':
d3 = (d3 + d2) % m
else:
d3 = (d3*3 + d2) % m
d2 = (d2*3 + d1) % m
d1 = (d1*3 + counter) % m
counter = counter * 3 % m
print(d3%m)
/*
Problem: 1426F
Date: 24-01-2024 06:09 PM
*/
#include <bits/stdc++.h>
#define ll long long
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define pii pair<int, int>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template<int m>
struct mod {
long long x;
mod() : x(0) {}
mod(long long xx) : x(xx) {
if(abs(x) >= m) x %= m;
if(x < 0) x += m;
}
mod operator+(const mod &a) const {
mod n;
n.x = x + a.x;
if(n.x >= m) n.x -= m;
return n;
}
mod operator-(const mod &a) const {
mod n;
n.x = x - a.x;
if(n.x < 0) n.x += m;
return n;
}
mod operator*(const mod &a) const {
return x * a.x;
}
mod operator+=(const mod &a) {
x += a.x;
if(x >= m) x -= m;
return *this;
}
mod operator-=(const mod &a) {
x -= a.x;
if(x < 0) x += m;
return *this;
}
mod operator*=(const mod &a) {
x = (x * a.x) % m;
return *this;
}
mod pow(long long b) const {
mod ans = 1;
mod a = *this;
while(b > 0) {
if(b & 1) ans *= a;
a *= a;
b /= 2;
}
return ans;
}
mod inv() const {
return pow(m - 2);
}
mod operator/(const mod &a) const {
return (*this) * a.inv();
}
mod operator/=(const mod &a) {
return (*this) *= a.inv();
}
bool operator==(const mod &o) const {
return x == o.x;
}
bool operator!=(const mod &o) const {
return x != o.x;
}
long long operator()() const {
return x;
}
};
template<int m>
istream &operator>>(istream &is, mod<m> &n) {
long long x;
is >> x;
n = x;
return is;
}
template<int m>
ostream &operator<<(ostream &os, const mod<m> &n) {
return os << n();
}
const int M = 1e9 + 7;
using base = mod<M>;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s;
cin >> n >> s;
s = "$" + s;
n++;
vector<base> dpa(n), dpab(n), dpabc(n), dpaq(n), dpabq(n), dpabqq(n), dpabcq(n), dpabcqq(n), dpabcqqq(n), pow3(n);
pow3[0] = 1;
int k = 0;
rep(i, 1, n) {
dpa[i] = dpa[i - 1] + (s[i] == 'a');
dpab[i] = dpab[i - 1] + (s[i] == 'b' ? dpa[i - 1] : 0);
dpabc[i] = dpabc[i - 1] + (s[i] == 'c' ? dpab[i - 1] : 0);
dpaq[i] = dpaq[i - 1] + (s[i] == '?');
dpabq[i] = dpabq[i - 1] + (s[i] == 'b' ? dpaq[i - 1] : 0) + (s[i] == '?' ? dpa[i - 1] : 0);
dpabqq[i] = dpabqq[i - 1] + (s[i] == '?' ? dpaq[i - 1] : 0);
dpabcq[i] = dpabcq[i - 1] + (s[i] == 'c' ? dpabq[i - 1] : 0) + (s[i] == '?' ? dpab[i - 1] : 0);
dpabcqq[i] = dpabcqq[i - 1] + (s[i] == 'c' ? dpabqq[i - 1] : 0) + (s[i] == '?' ? dpabq[i - 1] : 0);
dpabcqqq[i] = dpabcqqq[i - 1] + (s[i] == '?' ? dpabqq[i - 1] : 0);
pow3[i] = pow3[i - 1] * 3;
k += (s[i] == '?');
}
n--;
base ans = dpabc[n] * pow3[k];
if(k > 0) ans += dpabcq[n] * pow3[k - 1];
if(k > 1) ans += dpabcqq[n] * pow3[k - 2];
if(k > 2) ans += dpabcqqq[n] * pow3[k - 3];
cout << ans << '\n';
}
214A - System of Equations | 287A - IQ Test |
1108A - Two distinct points | 1064A - Make a triangle |
1245C - Constanze's Machine | 1005A - Tanya and Stairways |
1663F - In Every Generation | 1108B - Divisors of Two Integers |
1175A - From Hero to Zero | 1141A - Game 23 |
1401B - Ternary Sequence | 598A - Tricky Sum |
519A - A and B and Chess | 725B - Food on the Plane |
154B - Colliders | 127B - Canvas Frames |
107B - Basketball Team | 245A - System Administrator |
698A - Vacations | 1216B - Shooting |
368B - Sereja and Suffixes | 1665C - Tree Infection |
1665D - GCD Guess | 29A - Spit Problem |
1097B - Petr and a Combination Lock | 92A - Chips |
1665B - Array Cloning Technique | 1665A - GCD vs LCM |
118D - Caesar's Legions | 1598A - Computer Game |