#include <bits/stdc++.h>
using namespace std;
long long int mod = 998244353, c_root = 3;
namespace NTT {
long long int fexp(long long int base, long long int e) {
  long long int ans = 1;
  while (e > 0) {
    if (e & 1) ans = ans * base % mod;
    base = base * base % mod;
    e >>= 1;
  }
  return ans;
}
long long int inv_mod(long long int base) { return fexp(base, mod - 2); }
void ntt(vector<long long int> &a, bool inv) {
  int n = (int)a.size();
  if (n == 1) return;
  for (int i = 0, j = 0; i < n; i++) {
    if (i > j) {
      swap(a[i], a[j]);
    }
    for (int l = n / 2; (j ^= l) < l; l >>= 1)
      ;
  }
  for (int sz = 1; sz < n; sz <<= 1) {
    long long int delta = fexp(c_root, (mod - 1) / (2 * sz));
    if (inv) {
      delta = inv_mod(delta);
    }
    for (int i = 0; i < n; i += 2 * sz) {
      long long int w = 1;
      for (int j = 0; j < sz; j++) {
        long long int u = a[i + j], v = w * a[i + j + sz] % mod;
        a[i + j] = (u + v + mod) % mod;
        a[i + j] = (a[i + j] + mod) % mod;
        a[i + j + sz] = (u - v + mod) % mod;
        a[i + j + sz] = (a[i + j + sz] + mod) % mod;
        w = w * delta % mod;
      }
    }
  }
  if (inv) {
    long long int inv_n = inv_mod(n);
    for (int i = 0; i < n; i++) {
      a[i] = a[i] * inv_n % mod;
    }
  }
  for (int i = 0; i < n; i++) {
    a[i] %= mod;
    a[i] = (a[i] + mod) % mod;
  }
}
void multiply(vector<long long int> &a, vector<long long int> &b,
              vector<long long int> &ans) {
  int lim = (int)max(a.size(), b.size());
  int n = 1;
  while (n < lim) n <<= 1;
  n <<= 1;
  a.resize(n);
  b.resize(n);
  ans.resize(n);
  ntt(a, false);
  ntt(b, false);
  for (int i = 0; i < n; i++) {
    ans[i] = a[i] * b[i] % mod;
  }
  ntt(ans, true);
}
};  // namespace NTT
const int N = 1000001;
long long int fat[N], ifat[N];
long long int n;
long long int fexp(long long int base, long long int e) {
  base = (base % mod + mod) % mod;
  long long int ans = 1;
  while (e) {
    if (e & 1) ans = ans * base % mod;
    base = base * base % mod;
    e >>= 1;
  }
  return (ans % mod + mod) % mod;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  long long int ans = 0;
  fat[0] = 1;
  ifat[0] = 1;
  for (int i = 1; i <= n; i++) {
    fat[i] = 1ll * i * fat[i - 1] % mod;
    ifat[i] = fexp(fat[i], mod - 2);
  }
  vector<long long int> comb(n + 1);
  for (int i = 0; i <= n; i++) {
    comb[i] = fat[n] * ifat[i] % mod * ifat[n - i] % mod;
  }
  for (int i = 1; i <= n; i++) {
    long long int cur =
        2ll * comb[n - i] % mod * fexp(3, n * (n - i) + i) % mod;
    if (i & 1)
      ans = (ans + cur) % mod;
    else
      ans = (ans - cur + mod) % mod;
  }
  for (int i = 0; i < n; i++) {
    long long int base = fexp(3, i);
    long long int cur = fexp(1 - base, n) - fexp(0 - base, n);
    cur = (cur % mod + mod) % mod;
    cur = cur * comb[i] % mod;
    cur = 3ll * cur % mod;
    if (i & 1) {
      ans = (ans + cur) % mod;
    } else {
      ans = (ans - cur + mod) % mod;
    }
  }
  cout << ans << '\n';
  return 0;
}
