// In the name of Allah
// Khodaya khodet komak kon
// Hope is last to die
// Let's go
// Hello today I decided to solve at least 5 great problems per day for 5 months
// 2023/11/24
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
#define pb push_back
#define S second
#define F first
#define mp make_pair
#define smax(x, y) (x) = max((x), (y))
#define smin(x, y) (x) = min((x), (y))
#define all(x) (x).begin(), (x).end()
#define set_dec(x) cout << fixed << setprecision(x);
#define kill(x) cout << x << endl, exit(0)
#define sz(x) ((int)(x).size())
#define file_io freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
const ll maxn = 4e5 + 100, lg = 21, mod = 998244353;
const ll inf = 1e15 + 7;
ll n, m, dist2[maxn * (lg + 1)];
pll dist1[maxn];
vector<ll> adj[maxn];
vector<pair<ll, pll>> from[maxn];
vector<pll> fr[maxn * (lg + 1)];
ll tavan(ll a, ll b){
if(!b) return 1;
ll x = tavan(a, b / 2);
x = (x * x) % mod;
if(b % 2) x = (x * a) % mod;
return x;
}
void dij1(){
memset(dist1, 63, sizeof dist1);
dist1[0] = mp(0, 0);
set<pair<pll, ll>> st;
st.insert(mp(mp(0, 0), 0));
while(sz(st)){
ll v = (*st.begin()).S;
st.erase(st.begin());
for(auto e: from[v]){
pll p = dist1[v];
p.F += e.S.F;
p.S += e.S.S;
if(p < dist1[e.F]){
st.erase(mp(dist1[e.F], e.F));
dist1[e.F] = p;
st.insert(mp(dist1[e.F], e.F));
}
}
}
// cout << dist1[n - 1].F << ' ' << dist1[n - 1].S << '\n';
// cout << dist1[2 * n - 1].F << ' ' << dist1[2 * n - 1].S << '\n';
}
void dij2(){
memset(dist2, 63, sizeof dist2);
set<pll> st;
st.insert(mp(0, 0));
while(sz(st)){
ll v = (*st.begin()).S, d = (*st.begin()).F;
st.erase(st.begin());
if(dist2[v] <= inf) continue;
dist2[v] = d;
for(auto [u, w]: fr[v]){
if(dist2[u] > d + w){
st.insert(mp(d + w, u));
}
}
}
// for(int i = 0; i <= lg; i++) cout << dist2[i * n + n - 1] << '\n';
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++){
ll u, v; cin >> u >> v;
adj[--u].pb(--v);
}
for(int i = 0; i < n; i++){
from[i].pb(mp(i + n, mp(1, 0)));
from[i + n].pb(mp(i, mp(1, 0)));
for(auto u: adj[i]){
from[i].pb(mp(u, mp(0, 1)));
from[u + n].pb(mp(i + n, mp(0, 1)));
}
}
dij1();
ll t1 = dist1[n - 1].F, r1 = dist1[n - 1].S;
ll t2 = dist1[2 * n - 1].F, r2 = dist1[2 * n - 1].S;
if(min(t1, t2) >= 30){
if(t1 == t2){
cout << (tavan(2, t1) - 1 + min(r1, r2)) % mod;
}
else if(t1 < t2){
cout << (tavan(2, t1) - 1 + r1) % mod;
}
else cout << (tavan(2, t2) - 1 + r2) % mod;
return 0;
}
for(int b = 0; b <= lg; b++){
for(int i = 0; i < n; i++){
for(auto u: adj[i]){
if(b % 2 == 0) fr[b * n + i].pb(mp(b * n + u, 1));
else fr[b * n + u].pb(mp(b * n + i, 1));
}
}
if(b != lg){
for(int i = 0; i < n; i++){
fr[b * n + i].pb(mp((b + 1) * n + i, (1ll << b)));
}
}
}
dij2();
ll ans = inf;
for(int i = 0; i <= lg; i++) smin(ans, dist2[i * n + n - 1]);
smin(ans, (1ll << dist1[n - 1].F) + dist1[n - 1].S);
smin(ans, (1ll << dist1[2 * n - 1].F) + dist1[2 * n - 1].S);
cout << (ans % mod);
return 0;
}
821. Shortest Distance to a Character | 1441. Build an Array With Stack Operations |
1356. Sort Integers by The Number of 1 Bits | 922. Sort Array By Parity II |
344. Reverse String | 1047. Remove All Adjacent Duplicates In String |
977. Squares of a Sorted Array | 852. Peak Index in a Mountain Array |
461. Hamming Distance | 1748. Sum of Unique Elements |
897. Increasing Order Search Tree | 905. Sort Array By Parity |
1351. Count Negative Numbers in a Sorted Matrix | 617. Merge Two Binary Trees |
1450. Number of Students Doing Homework at a Given Time | 700. Search in a Binary Search Tree |
590. N-ary Tree Postorder Traversal | 589. N-ary Tree Preorder Traversal |
1299. Replace Elements with Greatest Element on Right Side | 1768. Merge Strings Alternately |
561. Array Partition I | 1374. Generate a String With Characters That Have Odd Counts |
1822. Sign of the Product of an Array | 1464. Maximum Product of Two Elements in an Array |
1323. Maximum 69 Number | 832. Flipping an Image |
1295. Find Numbers with Even Number of Digits | 1704. Determine if String Halves Are Alike |
1732. Find the Highest Altitude | 709. To Lower Case |