#include <bits/stdc++.h>
using namespace std;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
  if (fu) v = -v;
}
const int MO = 998244353;
int n;
long long ans, fac[1000010], inv[1000010];
long long pw(long long x, long long y) {
  long long t = 1;
  for (; y; y >>= 1) {
    if (y & 1) t = t * x % MO;
    x = x * x % MO;
  }
  return t;
}
long long C(int x, int y) { return fac[x] * inv[y] % MO * inv[x - y] % MO; }
int main() {
  cin >> n;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) fac[i] = fac[i - 1] * i % MO;
  inv[n] = pw(fac[n], MO - 2);
  for (int i = n - 1; i >= 0; --i) inv[i] = inv[i + 1] * (i + 1) % MO;
  for (int i = 1; i <= n; ++i)
    if (i & 1)
      (ans += C(n, i) * pw(3, 1ll * n * n - 1ll * i * (n - 1))) %= MO;
    else
      (ans -= C(n, i) * pw(3, 1ll * n * n - 1ll * i * (n - 1))) %= MO;
  ans = ans * 2 % MO;
  for (int i = 0; i <= n - 1; ++i)
    if (i & 1)
      (ans += C(n, i) * (pw(1 - pw(3, i), n) - pw(-pw(3, i), n)) * 3) %= MO;
    else
      (ans -= C(n, i) * (pw(1 - pw(3, i), n) - pw(-pw(3, i), n)) * 3) %= MO;
  ans = (ans % MO + MO) % MO;
  cout << ans << endl;
  return 0;
}
