#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
inline ostream& operator<<(ostream& os, const pair<A, B>& v) {
  return os << v.first << ' ' << v.second;
}
template <typename A>
inline ostream& operator<<(ostream& os, const vector<A>& v) {
  auto it = v.begin();
  os << *it;
  for (++it; it != v.end(); os << ' ' << *it++)
    ;
  return os;
}
void W() { cout << endl; }
template <typename H, typename... T>
inline void W(H head, T... tail) {
  cout << head << ' ';
  W(tail...);
}
const double eps = 1e-9, PI = acos(-1);
const long long mod = 998244353ll;
const int oo = 0x3f3f3f3f;
mt19937 rnd((unsigned long long)time(0));
long long pow_mod(long long a, long long b) {
  assert(b >= 0);
  long long res = 1;
  for (a %= mod; b; b >>= 1, a = a * a % mod)
    if (b & 1) res = res * a % mod;
  return res;
}
void solve();
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  solve();
  return 0;
}
const int N = 1000000 + 5000;
long long fact[N], inv[N];
inline long long C(long long n, long long m) {
  return ((fact[n] * inv[m]) % mod) * inv[n - m] % mod;
}
long long n;
long long po(long long a) { return pow_mod(3, a); }
void solve() {
  fact[0] = fact[1] = 1;
  for (long long i = 1; i < N; ++i) fact[i] = fact[i - 1] * i % mod;
  for (long long i = 0; i < N; ++i) inv[i] = pow_mod(fact[i], mod - 2);
  cin >> n;
  long long ans = 0;
  long long p = 1;
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) {
    long long x = p * C(n, i) * po(i) % mod * po(n * (n - i)) % mod;
    ans += x;
    ans %= mod;
    ans += x;
    ans %= mod;
    ans = (ans - p * C(n, i) * 3ll % mod * po(n * (n - i))) % mod;
    p *= -1;
  }
  p = 1;
  for (int i = (1), i_end_ = (n + 1); i < i_end_; ++i) {
    ans = (ans + p * C(n, i) * 3ll * pow_mod(po(n - i) - 1, n)) % mod;
    p *= -1;
  }
  ans += mod;
  ans %= mod;
  cout << ans << endl;
  return;
}
