#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 1000001;
long long jc[maxn], ny[maxn], pw[maxn];
long long n;
long long fpow(long long di, long long top) {
  long long ret = 1;
  while (top) {
    if (top % 2) ret = ret * di % mod;
    di = di * di % mod;
    top /= 2;
  }
  return ret;
}
void pre() {
  pw[0] = jc[0] = 1;
  for (int i = 1; i < maxn; ++i) {
    pw[i] = pw[i - 1] * 3 % mod;
    jc[i] = jc[i - 1] * i % mod;
  }
  ny[maxn - 1] = fpow(jc[maxn - 1], mod - 2);
  for (int i = maxn - 2; i >= 0; --i) {
    ny[i] = ny[i + 1] * (i + 1) % mod;
  }
}
long long c(int num, int from) {
  if (num + from == 0) return 0;
  if (num == from || num == 0) return 1;
  long long ret = 1;
  ret *= jc[from];
  ret *= ny[from - num];
  ret %= mod;
  ret *= ny[num];
  ret %= mod;
  return ret;
}
namespace TLE {
long long work() {
  long long ans = 0;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    long long nval;
    nval = c(i, n) * pw[i] % mod * fpow(3, 1ll * n * (n - i)) % mod;
    if (i % 2 == 1)
      ans += nval;
    else
      ans -= nval;
    if (ans < 0) ans += mod;
    ans %= mod;
  }
  ans *= 2;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      long long nval;
      nval =
          c(i, n) * c(j, n) % mod * 3 * fpow(3, 1ll * (n - i) * (n - j)) % mod;
      if ((i + j) % 2 == 0)
        ans -= nval;
      else
        ans += nval;
      if (ans < 0) ans += mod;
      ans %= mod;
    }
  }
  return ans;
}
}  // namespace TLE
namespace AC {
long long work() {
  long long ans = 0;
  cin >> n;
  ans = pw[n] - 3;
  if (ans < 0) ans += mod;
  ans = fpow(ans, n);
  ans -= fpow(3, n * n);
  if (ans < 0) ans += mod;
  ans %= mod;
  ans *= -2;
  for (int i = 1; i <= n; ++i) {
    long long nval;
    nval = pw[n - i] - 1;
    nval = fpow(nval, n);
    nval -= fpow(3, 1ll * n * (n - i));
    if (nval < 0) nval += mod;
    nval %= mod;
    nval *= 3;
    nval *= c(i, n);
    nval %= mod;
    if (i % 2 == 1)
      ans += nval;
    else
      ans -= nval;
  }
  while (ans < 0) ans += mod;
  ans %= mod;
  return ans;
}
}  // namespace AC
int main() {
  pre();
  cout << AC::work();
  return 0;
}
